Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDani Megert2004-06-17 10:39:17 +0000
committerDani Megert2004-06-17 10:39:17 +0000
commit379322902a61bc5ea9666c68d82ee7e8d21d52eb (patch)
tree7349bae339b76446fb9aab546ae2903f8efbe9d4 /org.eclipse.core.filebuffers
parentc125355791e993f446fbb49b3a136739ffbd64e3 (diff)
downloadeclipse.platform.text-379322902a61bc5ea9666c68d82ee7e8d21d52eb.tar.gz
eclipse.platform.text-379322902a61bc5ea9666c68d82ee7e8d21d52eb.tar.xz
eclipse.platform.text-379322902a61bc5ea9666c68d82ee7e8d21d52eb.zip
Fixed bug 65347: [encoding] Save external file should not change encoding
Diffstat (limited to 'org.eclipse.core.filebuffers')
-rw-r--r--org.eclipse.core.filebuffers/src/org/eclipse/core/internal/filebuffers/FileBuffersMessages.properties1
-rw-r--r--org.eclipse.core.filebuffers/src/org/eclipse/core/internal/filebuffers/JavaTextFileBuffer.java45
-rw-r--r--org.eclipse.core.filebuffers/src/org/eclipse/core/internal/filebuffers/ResourceTextFileBuffer.java63
3 files changed, 41 insertions, 68 deletions
diff --git a/org.eclipse.core.filebuffers/src/org/eclipse/core/internal/filebuffers/FileBuffersMessages.properties b/org.eclipse.core.filebuffers/src/org/eclipse/core/internal/filebuffers/FileBuffersMessages.properties
index fec7a6f3b..271f2e84f 100644
--- a/org.eclipse.core.filebuffers/src/org/eclipse/core/internal/filebuffers/FileBuffersMessages.properties
+++ b/org.eclipse.core.filebuffers/src/org/eclipse/core/internal/filebuffers/FileBuffersMessages.properties
@@ -14,7 +14,6 @@ ExtensionsRegistry.error.contentTypeDoesNotExist= The content type with id "{0}"
FileBuffer.error.fileDoesNotExist= The file does not exist.
FileBuffer.error.outOfSync= The file is not synchronized with the the local file system.
-FileBuffer.error.wrongByteOrderMark= Wrong byte order mark for UTF-8 encoding
FileBuffer.status.ok= OK
FileBuffer.status.error= Error
diff --git a/org.eclipse.core.filebuffers/src/org/eclipse/core/internal/filebuffers/JavaTextFileBuffer.java b/org.eclipse.core.filebuffers/src/org/eclipse/core/internal/filebuffers/JavaTextFileBuffer.java
index 5a1504779..6320eccd8 100644
--- a/org.eclipse.core.filebuffers/src/org/eclipse/core/internal/filebuffers/JavaTextFileBuffer.java
+++ b/org.eclipse.core.filebuffers/src/org/eclipse/core/internal/filebuffers/JavaTextFileBuffer.java
@@ -83,7 +83,7 @@ public class JavaTextFileBuffer extends JavaFileBuffer implements ITextFileBuffe
* Constant denoting UTF-8 encoding.
*/
private static final String CHARSET_UTF_8= "UTF-8"; //$NON-NLS-1$
-
+
/** The element's document */
protected IDocument fDocument;
@@ -96,16 +96,11 @@ public class JavaTextFileBuffer extends JavaFileBuffer implements ITextFileBuffe
*/
private String fExplicitEncoding;
/**
- * BOM if encoding is UTF-8.
- * <p>
- * XXX:
- * This is a workaround for a corresponding bug in Java readers and writer,
- * see: http://developer.java.sun.com/developer/bugParade/bugs/4508058.html
- * </p>
+ * Tells whether the file on disk has a BOM.
*/
- private byte[] fUTF8BOM;
-
+ private boolean fHasBOM;
+
public JavaTextFileBuffer(TextFileBufferManager manager) {
super(manager);
}
@@ -256,6 +251,8 @@ public class JavaTextFileBuffer extends JavaFileBuffer implements ITextFileBuffe
protected void initializeFileBufferContent(IProgressMonitor monitor) throws CoreException {
try {
fDocument= fManager.createEmptyDocument(getLocation());
+ fEncoding= null;
+ fHasBOM= false;
InputStream stream= getFileContents(monitor);
try {
@@ -263,14 +260,10 @@ public class JavaTextFileBuffer extends JavaFileBuffer implements ITextFileBuffe
IContentDescription description= Platform.getContentTypeManager().getDescriptionFor(stream, fFile.getName(), options);
if (description != null) {
fEncoding= description.getCharset();
- if (CHARSET_UTF_8.equals(fEncoding)) {
- fUTF8BOM= (byte[]) description.getProperty(IContentDescription.BYTE_ORDER_MARK);
- if (fUTF8BOM != null && fUTF8BOM != IContentDescription.BOM_UTF_8)
- throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, FileBuffersMessages.getString("FileBuffer.error.wrongByteOrderMark"), null)); //$NON-NLS-1$
- }
+ fHasBOM= description.getProperty(IContentDescription.BYTE_ORDER_MARK) != null;
}
} catch (IOException e) {
- fEncoding= null;
+ // do nothing
} finally {
try {
stream.close();
@@ -300,10 +293,11 @@ public class JavaTextFileBuffer extends JavaFileBuffer implements ITextFileBuffe
* This is a workaround for a corresponding bug in Java readers and writer,
* see: http://developer.java.sun.com/developer/bugParade/bugs/4508058.html
*/
- if (fUTF8BOM != null && CHARSET_UTF_8.equals(encoding)) {
- byte[] bytesWithBOM= new byte[bytes.length + 3];
- System.arraycopy(fUTF8BOM, 0, bytesWithBOM, 0, 3);
- System.arraycopy(bytes, 0, bytesWithBOM, 3, bytes.length);
+ if (fHasBOM && CHARSET_UTF_8.equals(encoding)) {
+ int bomLength= IContentDescription.BOM_UTF_8.length;
+ byte[] bytesWithBOM= new byte[bytes.length + bomLength];
+ System.arraycopy(IContentDescription.BOM_UTF_8, 0, bytesWithBOM, 0, bomLength);
+ System.arraycopy(bytes, 0, bytesWithBOM, bomLength, bytes.length);
bytes= bytesWithBOM;
}
@@ -369,9 +363,9 @@ public class JavaTextFileBuffer extends JavaFileBuffer implements ITextFileBuffe
}
}
- // Use UTF-8 BOM if there was any
- if (fUTF8BOM != null)
- return CHARSET_UTF_8;
+ // Use file's encoding if the file has a BOM
+ if (fHasBOM)
+ return fEncoding;
// Use global default
return fManager.getDefaultEncoding();
@@ -398,11 +392,8 @@ public class JavaTextFileBuffer extends JavaFileBuffer implements ITextFileBuffe
* see: http://developer.java.sun.com/developer/bugParade/bugs/4508058.html
* </p>
*/
- if (fUTF8BOM != null && CHARSET_UTF_8.equals(encoding)) {
- contentStream.read();
- contentStream.read();
- contentStream.read();
- }
+ if (fHasBOM && CHARSET_UTF_8.equals(encoding))
+ contentStream.read(new byte[IContentDescription.BOM_UTF_8.length]);
in= new BufferedReader(new InputStreamReader(contentStream, encoding), BUFFER_SIZE);
StringBuffer buffer= new StringBuffer(BUFFER_SIZE);
diff --git a/org.eclipse.core.filebuffers/src/org/eclipse/core/internal/filebuffers/ResourceTextFileBuffer.java b/org.eclipse.core.filebuffers/src/org/eclipse/core/internal/filebuffers/ResourceTextFileBuffer.java
index fdf2aee4a..a2b2ad37e 100644
--- a/org.eclipse.core.filebuffers/src/org/eclipse/core/internal/filebuffers/ResourceTextFileBuffer.java
+++ b/org.eclipse.core.filebuffers/src/org/eclipse/core/internal/filebuffers/ResourceTextFileBuffer.java
@@ -101,14 +101,9 @@ public class ResourceTextFileBuffer extends ResourceFileBuffer implements ITextF
*/
private String fExplicitEncoding;
/**
- * BOM if encoding is UTF-8.
- * <p>
- * XXX:
- * This is a workaround for a corresponding bug in Java readers and writer,
- * see: http://developer.java.sun.com/developer/bugParade/bugs/4508058.html
- * </p>
+ * Tells whether the file on disk has a BOM.
*/
- private byte[] fUTF8BOM;
+ private boolean fHasBOM;
public ResourceTextFileBuffer(TextFileBufferManager manager) {
@@ -142,11 +137,12 @@ public class ResourceTextFileBuffer extends ResourceFileBuffer implements ITextF
public void setEncoding(String encoding) {
fEncoding= encoding;
fExplicitEncoding= encoding;
+ fHasBOM= false;
try {
fFile.setCharset(encoding);
if (encoding == null)
fEncoding= fFile.getCharset();
- readUTF8BOM();
+ setHasBOM();
} catch (CoreException x) {
handleCoreException(x);
}
@@ -259,7 +255,7 @@ public class ResourceTextFileBuffer extends ResourceFileBuffer implements ITextF
fEncoding= fFile.getCharset();
}
- readUTF8BOM();
+ setHasBOM();
fDocument= fManager.createEmptyDocument(fFile.getLocation());
setDocumentContent(fDocument, fFile.getContents(), fEncoding);
@@ -271,27 +267,16 @@ public class ResourceTextFileBuffer extends ResourceFileBuffer implements ITextF
fStatus= x.getStatus();
}
}
-
+
/**
- * Reads the file's UTF-8 BOM if any and stores it.
- * <p>
- * XXX:
- * This is a workaround for a corresponding bug in Java readers and writer,
- * see: http://developer.java.sun.com/developer/bugParade/bugs/4508058.html
- * </p>
- * @throws CoreException if
- * - reading of file's content description fails
- * - byte order mark is not valid for UTF-8
+ * Sets whether the underlying file has a BOM.
+ *
+ * @throws CoreException if reading of file's content description fails
*/
- protected void readUTF8BOM() throws CoreException {
- if (CHARSET_UTF_8.equals(fEncoding)) {
- IContentDescription description= fFile.getContentDescription();
- if (description != null) {
- fUTF8BOM= (byte[]) description.getProperty(IContentDescription.BYTE_ORDER_MARK);
- if (fUTF8BOM != null && fUTF8BOM != IContentDescription.BOM_UTF_8)
- throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, FileBuffersMessages.getString("FileBuffer.error.wrongByteOrderMark"), null)); //$NON-NLS-1$
- }
- }
+ protected void setHasBOM() throws CoreException {
+ fHasBOM= false;
+ IContentDescription description= fFile.getContentDescription();
+ fHasBOM= description != null && description.getProperty(IContentDescription.BYTE_ORDER_MARK) != null;
}
/*
@@ -326,10 +311,11 @@ public class ResourceTextFileBuffer extends ResourceFileBuffer implements ITextF
* This is a workaround for a corresponding bug in Java readers and writer,
* see: http://developer.java.sun.com/developer/bugParade/bugs/4508058.html
*/
- if (fUTF8BOM != null && CHARSET_UTF_8.equals(encoding)) {
- byte[] bytesWithBOM= new byte[bytes.length + 3];
- System.arraycopy(fUTF8BOM, 0, bytesWithBOM, 0, 3);
- System.arraycopy(bytes, 0, bytesWithBOM, 3, bytes.length);
+ if (fHasBOM && CHARSET_UTF_8.equals(encoding)) {
+ int bomLength= IContentDescription.BOM_UTF_8.length;
+ byte[] bytesWithBOM= new byte[bytes.length + bomLength];
+ System.arraycopy(IContentDescription.BOM_UTF_8, 0, bytesWithBOM, 0, bomLength);
+ System.arraycopy(bytes, 0, bytesWithBOM, bomLength, bytes.length);
bytes= bytesWithBOM;
}
@@ -408,9 +394,9 @@ public class ResourceTextFileBuffer extends ResourceFileBuffer implements ITextF
}
}
- // Use UTF-8 BOM if there was any
- if (fUTF8BOM != null)
- return CHARSET_UTF_8;
+ // Use file's encoding if the file has a BOM
+ if (fHasBOM)
+ return fEncoding;
// Use parent chain
try {
@@ -493,11 +479,8 @@ public class ResourceTextFileBuffer extends ResourceFileBuffer implements ITextF
* This is a workaround for a corresponding bug in Java readers and writer,
* see: http://developer.java.sun.com/developer/bugParade/bugs/4508058.html
*/
- if (fUTF8BOM != null && CHARSET_UTF_8.equals(encoding)) {
- contentStream.read();
- contentStream.read();
- contentStream.read();
- }
+ if (fHasBOM && CHARSET_UTF_8.equals(encoding))
+ contentStream.read(new byte[IContentDescription.BOM_UTF_8.length]);
in= new BufferedReader(new InputStreamReader(contentStream, encoding), BUFFER_SIZE);
StringBuffer buffer= new StringBuffer(BUFFER_SIZE);

Back to the top