Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndre Weinand2003-11-17 22:43:00 +0000
committerAndre Weinand2003-11-17 22:43:00 +0000
commit8c3fe25250b06f74272244c11e8080988eca8640 (patch)
treeadacd292f485d7ca32642f1e0a781012b81505c1
parent762f06caa63ea5c57387b5d6b43a8332c990c29a (diff)
downloadeclipse.platform.team-8c3fe25250b06f74272244c11e8080988eca8640.tar.gz
eclipse.platform.team-8c3fe25250b06f74272244c11e8080988eca8640.tar.xz
eclipse.platform.team-8c3fe25250b06f74272244c11e8080988eca8640.zip
fixed #46805v20031117
-rw-r--r--bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/Utilities.java124
-rw-r--r--bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/Utilities.java124
2 files changed, 166 insertions, 82 deletions
diff --git a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/Utilities.java b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/Utilities.java
index 51f66ea49..4f22d265d 100644
--- a/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/Utilities.java
+++ b/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/Utilities.java
@@ -27,6 +27,7 @@ import org.eclipse.jface.viewers.*;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
@@ -35,6 +36,8 @@ import org.eclipse.core.runtime.Status;
import org.eclipse.ui.*;
import org.eclipse.compare.CompareConfiguration;
+import org.eclipse.compare.IStreamContentAccessor;
+import org.eclipse.compare.IStreamContentAccessorExtension2;
/**
* Convenience and utility methods.
@@ -186,46 +189,6 @@ public class Utilities {
return bos.toByteArray();
}
- /**
- * Returns null if an error occurred.
- */
- public static String readString(InputStream is) {
- if (is == null)
- return null;
- BufferedReader reader= null;
- try {
- StringBuffer buffer= new StringBuffer();
- char[] part= new char[2048];
- int read= 0;
- reader= new BufferedReader(new InputStreamReader(is, ResourcesPlugin.getEncoding()));
-
- while ((read= reader.read(part)) != -1)
- buffer.append(part, 0, read);
-
- return buffer.toString();
-
- } catch (IOException ex) {
- // NeedWork
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException ex) {
- // silently ignored
- }
- }
- }
- return null;
- }
-
- public static byte[] getBytes(String s) {
- try {
- return s.getBytes(ResourcesPlugin.getEncoding());
- } catch (UnsupportedEncodingException e) {
- return s.getBytes();
- }
- }
-
public static String getIconPath(Display display) {
return "icons/full/"; //$NON-NLS-1$
}
@@ -547,5 +510,84 @@ public class Utilities {
result.add(status);
result.add(entry);
return result;
- }
+ }
+
+ // encoding
+
+ /**
+ * Returns null if an error occurred.
+ */
+ public static String readString(InputStream is, String encoding) {
+ if (is == null)
+ return null;
+ BufferedReader reader= null;
+ try {
+ StringBuffer buffer= new StringBuffer();
+ char[] part= new char[2048];
+ int read= 0;
+ reader= new BufferedReader(new InputStreamReader(is, encoding));
+
+ while ((read= reader.read(part)) != -1)
+ buffer.append(part, 0, read);
+
+ return buffer.toString();
+
+ } catch (IOException ex) {
+ // NeedWork
+ } finally {
+ if (reader != null) {
+ try {
+ reader.close();
+ } catch (IOException ex) {
+ // silently ignored
+ }
+ }
+ }
+ return null;
+ }
+
+ public static String getCharset(IResource resource) {
+ if (resource != null) {
+ /*
+ try {
+ return resource.getCharset();
+ } catch (CoreException ex) {
+ }
+ */
+ }
+ return ResourcesPlugin.getEncoding();
+ }
+
+ public static byte[] getBytes(String s, String encoding) {
+ byte[] bytes= null;
+ if (s != null) {
+ try {
+ bytes= s.getBytes(encoding); //$NON-NLS-1$
+ } catch (UnsupportedEncodingException e) {
+ bytes= s.getBytes();
+ }
+ }
+ return bytes;
+ }
+
+ public static String guessCharset(String path) {
+ //System.err.println("Utilities.guessCharset: " + path);
+ int dot= path.lastIndexOf('.');
+ if (dot >= 0) {
+ String extension= path.substring(dot);
+ if (extension.equalsIgnoreCase(".xml")) //$NON-NLS-1$
+ return "UTF-8"; //$NON-NLS-1$
+ }
+ return ResourcesPlugin.getEncoding();
+ }
+
+ public static String readString(IStreamContentAccessor sa) throws CoreException {
+ InputStream is= sa.getContents();
+ String encoding= null;
+ if (sa instanceof IStreamContentAccessorExtension2)
+ encoding= ((IStreamContentAccessorExtension2)sa).getCharset();
+ if (encoding == null)
+ encoding= ResourcesPlugin.getEncoding();
+ return Utilities.readString(is, encoding);
+ }
}
diff --git a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/Utilities.java b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/Utilities.java
index 51f66ea49..4f22d265d 100644
--- a/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/Utilities.java
+++ b/bundles/org.eclipse.compare/plugins/org.eclipse.compare/compare/org/eclipse/compare/internal/Utilities.java
@@ -27,6 +27,7 @@ import org.eclipse.jface.viewers.*;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
@@ -35,6 +36,8 @@ import org.eclipse.core.runtime.Status;
import org.eclipse.ui.*;
import org.eclipse.compare.CompareConfiguration;
+import org.eclipse.compare.IStreamContentAccessor;
+import org.eclipse.compare.IStreamContentAccessorExtension2;
/**
* Convenience and utility methods.
@@ -186,46 +189,6 @@ public class Utilities {
return bos.toByteArray();
}
- /**
- * Returns null if an error occurred.
- */
- public static String readString(InputStream is) {
- if (is == null)
- return null;
- BufferedReader reader= null;
- try {
- StringBuffer buffer= new StringBuffer();
- char[] part= new char[2048];
- int read= 0;
- reader= new BufferedReader(new InputStreamReader(is, ResourcesPlugin.getEncoding()));
-
- while ((read= reader.read(part)) != -1)
- buffer.append(part, 0, read);
-
- return buffer.toString();
-
- } catch (IOException ex) {
- // NeedWork
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException ex) {
- // silently ignored
- }
- }
- }
- return null;
- }
-
- public static byte[] getBytes(String s) {
- try {
- return s.getBytes(ResourcesPlugin.getEncoding());
- } catch (UnsupportedEncodingException e) {
- return s.getBytes();
- }
- }
-
public static String getIconPath(Display display) {
return "icons/full/"; //$NON-NLS-1$
}
@@ -547,5 +510,84 @@ public class Utilities {
result.add(status);
result.add(entry);
return result;
- }
+ }
+
+ // encoding
+
+ /**
+ * Returns null if an error occurred.
+ */
+ public static String readString(InputStream is, String encoding) {
+ if (is == null)
+ return null;
+ BufferedReader reader= null;
+ try {
+ StringBuffer buffer= new StringBuffer();
+ char[] part= new char[2048];
+ int read= 0;
+ reader= new BufferedReader(new InputStreamReader(is, encoding));
+
+ while ((read= reader.read(part)) != -1)
+ buffer.append(part, 0, read);
+
+ return buffer.toString();
+
+ } catch (IOException ex) {
+ // NeedWork
+ } finally {
+ if (reader != null) {
+ try {
+ reader.close();
+ } catch (IOException ex) {
+ // silently ignored
+ }
+ }
+ }
+ return null;
+ }
+
+ public static String getCharset(IResource resource) {
+ if (resource != null) {
+ /*
+ try {
+ return resource.getCharset();
+ } catch (CoreException ex) {
+ }
+ */
+ }
+ return ResourcesPlugin.getEncoding();
+ }
+
+ public static byte[] getBytes(String s, String encoding) {
+ byte[] bytes= null;
+ if (s != null) {
+ try {
+ bytes= s.getBytes(encoding); //$NON-NLS-1$
+ } catch (UnsupportedEncodingException e) {
+ bytes= s.getBytes();
+ }
+ }
+ return bytes;
+ }
+
+ public static String guessCharset(String path) {
+ //System.err.println("Utilities.guessCharset: " + path);
+ int dot= path.lastIndexOf('.');
+ if (dot >= 0) {
+ String extension= path.substring(dot);
+ if (extension.equalsIgnoreCase(".xml")) //$NON-NLS-1$
+ return "UTF-8"; //$NON-NLS-1$
+ }
+ return ResourcesPlugin.getEncoding();
+ }
+
+ public static String readString(IStreamContentAccessor sa) throws CoreException {
+ InputStream is= sa.getContents();
+ String encoding= null;
+ if (sa instanceof IStreamContentAccessorExtension2)
+ encoding= ((IStreamContentAccessorExtension2)sa).getCharset();
+ if (encoding == null)
+ encoding= ResourcesPlugin.getEncoding();
+ return Utilities.readString(is, encoding);
+ }
}

Back to the top