View | Details | Raw Unified | Return to bug 110576
Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/util/Util.java (-2 / +3 lines)
Lines 28-33 Link Here
28
	}
28
	}
29
29
30
	private static final int DEFAULT_READING_SIZE = 8192;
30
	private static final int DEFAULT_READING_SIZE = 8192;
31
	public final static String UTF_8 = "UTF-8";	//$NON-NLS-1$
31
	public static String LINE_SEPARATOR = System.getProperty("line.separator"); //$NON-NLS-1$
32
	public static String LINE_SEPARATOR = System.getProperty("line.separator"); //$NON-NLS-1$
32
	
33
	
33
	/**
34
	/**
Lines 219-225 Link Here
219
220
220
			// Do not keep first character for UTF-8 BOM encoding
221
			// Do not keep first character for UTF-8 BOM encoding
221
			int start = 0;
222
			int start = 0;
222
			if (contentsLength > 0 && "UTF-8".equals(encoding)) { //$NON-NLS-1$
223
			if (contentsLength > 0 && UTF_8.equals(encoding)) { //$NON-NLS-1$
223
				if (contents[0] == 0xFEFF) { // if BOM char then skip
224
				if (contents[0] == 0xFEFF) { // if BOM char then skip
224
					contentsLength--;
225
					contentsLength--;
225
					start = 1;
226
					start = 1;
Lines 246-252 Link Here
246
			}
247
			}
247
			// Do not keep first character for UTF-8 BOM encoding
248
			// Do not keep first character for UTF-8 BOM encoding
248
			int start = 0;
249
			int start = 0;
249
			if (length > 0 && "UTF-8".equals(encoding)) { //$NON-NLS-1$
250
			if (length > 0 && UTF_8.equals(encoding)) { //$NON-NLS-1$
250
				if (contents[0] == 0xFEFF) { // if BOM char then skip
251
				if (contents[0] == 0xFEFF) { // if BOM char then skip
251
					len--;
252
					len--;
252
					start = 1;
253
					start = 1;
(-)model/org/eclipse/jdt/internal/core/Buffer.java (-3 / +22 lines)
Lines 20-25 Link Here
20
import org.eclipse.core.runtime.IProgressMonitor;
20
import org.eclipse.core.runtime.IProgressMonitor;
21
import org.eclipse.core.runtime.ISafeRunnable;
21
import org.eclipse.core.runtime.ISafeRunnable;
22
import org.eclipse.core.runtime.Platform;
22
import org.eclipse.core.runtime.Platform;
23
import org.eclipse.core.runtime.content.IContentDescription;
23
import org.eclipse.jdt.core.*;
24
import org.eclipse.jdt.core.*;
24
import org.eclipse.jdt.internal.core.util.Util;
25
import org.eclipse.jdt.internal.core.util.Util;
25
26
Lines 334-339 Link Here
334
		
335
		
335
	// use a platform operation to update the resource contents
336
	// use a platform operation to update the resource contents
336
	try {
337
	try {
338
		String stringContents = this.getContents();
339
		if (stringContents == null) return;
340
341
		// Get encoding
337
		String encoding = null;
342
		String encoding = null;
338
		try {
343
		try {
339
			encoding = this.file.getCharset();
344
			encoding = this.file.getCharset();
Lines 341-353 Link Here
341
		catch (CoreException ce) {
346
		catch (CoreException ce) {
342
			// use no encoding
347
			// use no encoding
343
		}
348
		}
344
		String stringContents = this.getContents();
349
		
345
		if (stringContents == null) return;
350
		// Create bytes array
346
		byte[] bytes = encoding == null 
351
		byte[] bytes = encoding == null 
347
			? stringContents.getBytes() 
352
			? stringContents.getBytes() 
348
			: stringContents.getBytes(encoding);
353
			: stringContents.getBytes(encoding);
349
		ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
350
354
355
		// Special case for UTF-8 BOM files
356
		// see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=110576
357
		if (encoding.equals(org.eclipse.jdt.internal.compiler.util.Util.UTF_8)) { //$NON-NLS-1$
358
			IContentDescription description = this.file.getContentDescription();
359
			if (description != null && description.getProperty(IContentDescription.BYTE_ORDER_MARK) != null) {
360
				int bomLength= IContentDescription.BOM_UTF_8.length;
361
				byte[] bytesWithBOM= new byte[bytes.length + bomLength];
362
				System.arraycopy(IContentDescription.BOM_UTF_8, 0, bytesWithBOM, 0, bomLength);
363
				System.arraycopy(bytes, 0, bytesWithBOM, bomLength, bytes.length);
364
				bytes= bytesWithBOM;
365
			}
366
		}
367
		
368
		// Set file contents
369
		ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
351
		if (this.file.exists()) {
370
		if (this.file.exists()) {
352
			this.file.setContents(
371
			this.file.setContents(
353
				stream, 
372
				stream, 
(-)model/org/eclipse/jdt/internal/core/JavaProject.java (-3 / +3 lines)
Lines 2113-2119 Link Here
2113
		if (rscFile.exists()) {
2113
		if (rscFile.exists()) {
2114
			byte[] bytes = Util.getResourceContentsAsByteArray(rscFile);
2114
			byte[] bytes = Util.getResourceContentsAsByteArray(rscFile);
2115
			try {
2115
			try {
2116
				property = new String(bytes, "UTF-8"); //$NON-NLS-1$ // .classpath always encoded with UTF-8
2116
				property = new String(bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8); // .classpath always encoded with UTF-8
2117
			} catch (UnsupportedEncodingException e) {
2117
			} catch (UnsupportedEncodingException e) {
2118
				Util.log(e, "Could not read .classpath with UTF-8 encoding"); //$NON-NLS-1$
2118
				Util.log(e, "Could not read .classpath with UTF-8 encoding"); //$NON-NLS-1$
2119
				// fallback to default
2119
				// fallback to default
Lines 2132-2138 Link Here
2132
					return null;
2132
					return null;
2133
				}
2133
				}
2134
				try {
2134
				try {
2135
					property = new String(bytes, "UTF-8"); //$NON-NLS-1$ // .classpath always encoded with UTF-8
2135
					property = new String(bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8); // .classpath always encoded with UTF-8
2136
				} catch (UnsupportedEncodingException e) {
2136
				} catch (UnsupportedEncodingException e) {
2137
					Util.log(e, "Could not read .classpath with UTF-8 encoding"); //$NON-NLS-1$
2137
					Util.log(e, "Could not read .classpath with UTF-8 encoding"); //$NON-NLS-1$
2138
					// fallback to default
2138
					// fallback to default
Lines 2888-2894 Link Here
2888
		IFile rscFile = this.project.getFile(key);
2888
		IFile rscFile = this.project.getFile(key);
2889
		byte[] bytes = null;
2889
		byte[] bytes = null;
2890
		try {
2890
		try {
2891
			bytes = value.getBytes("UTF-8"); //$NON-NLS-1$ // .classpath always encoded with UTF-8
2891
			bytes = value.getBytes(org.eclipse.jdt.internal.compiler.util.Util.UTF_8); // .classpath always encoded with UTF-8
2892
		} catch (UnsupportedEncodingException e) {
2892
		} catch (UnsupportedEncodingException e) {
2893
			Util.log(e, "Could not write .classpath with UTF-8 encoding "); //$NON-NLS-1$
2893
			Util.log(e, "Could not write .classpath with UTF-8 encoding "); //$NON-NLS-1$
2894
			// fallback to default
2894
			// fallback to default

Return to bug 110576