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

(-)src/org/eclipse/jdt/core/tests/model/ClasspathTests.java (-1 / +31 lines)
Lines 39-44 Link Here
39
import org.eclipse.core.runtime.IStatus;
39
import org.eclipse.core.runtime.IStatus;
40
import org.eclipse.core.runtime.Path;
40
import org.eclipse.core.runtime.Path;
41
import org.eclipse.core.runtime.Platform;
41
import org.eclipse.core.runtime.Platform;
42
import org.eclipse.core.runtime.content.IContentDescription;
42
import org.eclipse.jdt.core.IAccessRule;
43
import org.eclipse.jdt.core.IAccessRule;
43
import org.eclipse.jdt.core.IClasspathAttribute;
44
import org.eclipse.jdt.core.IClasspathAttribute;
44
import org.eclipse.jdt.core.IClasspathContainer;
45
import org.eclipse.jdt.core.IClasspathContainer;
Lines 2333-2339 Link Here
2333
 * Ensures that a source folder that contains character that must be encoded can be written.
2334
 * Ensures that a source folder that contains character that must be encoded can be written.
2334
 * (regression test for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=70193)
2335
 * (regression test for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=70193)
2335
 */
2336
 */
2336
public void testEncoding() throws CoreException {
2337
public void testEncoding1() throws CoreException {
2337
	try {
2338
	try {
2338
		createJavaProject("P", new String[] {"src\u3400"}, "bin");
2339
		createJavaProject("P", new String[] {"src\u3400"}, "bin");
2339
		IFile file = getFile("/P/.classpath");
2340
		IFile file = getFile("/P/.classpath");
Lines 2351-2356 Link Here
2351
	}
2352
	}
2352
}
2353
}
2353
/*
2354
/*
2355
 * Ensures that a .classpath encoded with a BOM can be read
2356
 * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=240034 )
2357
 */
2358
public void testEncoding2() throws Exception {
2359
	try {
2360
		IJavaProject p = createJavaProject("P", new String[] {"src"}, "bin");
2361
		IFile file = getFile("/P/.classpath");
2362
		byte[] contents = org.eclipse.jdt.internal.core.util.Util.getResourceContentsAsByteArray(file);
2363
		FileOutputStream output = null;
2364
		try {
2365
			output = new FileOutputStream(file.getLocation().toFile());
2366
			output.write(IContentDescription.BOM_UTF_8); // UTF-8 BOM
2367
			output.write(contents);
2368
			output.flush();
2369
		} finally {
2370
			if (output != null)
2371
				output.close();
2372
		}
2373
		file.refreshLocal(IResource.DEPTH_ONE, null);
2374
		
2375
		assertClasspathEquals(
2376
			p.getResolvedClasspath(false), 
2377
			"/P/src[CPE_SOURCE][K_SOURCE][isExported:false]"
2378
		);
2379
	} finally {
2380
		deleteProject("P");
2381
	}
2382
}
2383
/*
2354
 * Ensures that a source classpath entry can be encoded and decoded.
2384
 * Ensures that a source classpath entry can be encoded and decoded.
2355
 */
2385
 */
2356
public void testEncodeDecodeEntry01() {
2386
public void testEncodeDecodeEntry01() {
(-)model/org/eclipse/jdt/internal/core/JavaProject.java (-17 / +26 lines)
Lines 41-46 Link Here
41
import org.eclipse.core.runtime.Path;
41
import org.eclipse.core.runtime.Path;
42
import org.eclipse.core.runtime.Preferences;
42
import org.eclipse.core.runtime.Preferences;
43
import org.eclipse.core.runtime.QualifiedName;
43
import org.eclipse.core.runtime.QualifiedName;
44
import org.eclipse.core.runtime.content.IContentDescription;
44
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
45
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
45
import org.eclipse.core.runtime.preferences.IScopeContext;
46
import org.eclipse.core.runtime.preferences.IScopeContext;
46
import org.eclipse.jdt.core.IClasspathContainer;
47
import org.eclipse.jdt.core.IClasspathContainer;
Lines 2024-2029 Link Here
2024
	public int hashCode() {
2025
	public int hashCode() {
2025
		return this.project.hashCode();
2026
		return this.project.hashCode();
2026
	}
2027
	}
2028
	
2029
	private boolean hasUTF8BOM(byte[] bytes) {
2030
		if (bytes.length > IContentDescription.BOM_UTF_8.length) {
2031
			for (int i = 0, length = IContentDescription.BOM_UTF_8.length; i < length; i++) {
2032
				if (IContentDescription.BOM_UTF_8[i] != bytes[i])
2033
					return false;
2034
			}
2035
			return true;
2036
		}
2037
		return false;
2038
	}
2027
2039
2028
	/**
2040
	/**
2029
	 * Answers true if the project potentially contains any source. A project which has no source is immutable.
2041
	 * Answers true if the project potentially contains any source. A project which has no source is immutable.
Lines 2354-2370 Link Here
2354
	 * Throws exceptions if the file cannot be accessed or is malformed.
2366
	 * Throws exceptions if the file cannot be accessed or is malformed.
2355
	 */
2367
	 */
2356
	public IClasspathEntry[] readFileEntriesWithException(Map unknownElements) throws CoreException, IOException, ClasspathEntry.AssertionFailedException {
2368
	public IClasspathEntry[] readFileEntriesWithException(Map unknownElements) throws CoreException, IOException, ClasspathEntry.AssertionFailedException {
2357
		String xmlClasspath;
2358
		IFile rscFile = this.project.getFile(JavaProject.CLASSPATH_FILENAME);
2369
		IFile rscFile = this.project.getFile(JavaProject.CLASSPATH_FILENAME);
2370
		byte[] bytes;
2359
		if (rscFile.exists()) {
2371
		if (rscFile.exists()) {
2360
			byte[] bytes = Util.getResourceContentsAsByteArray(rscFile);
2372
			bytes = Util.getResourceContentsAsByteArray(rscFile);
2361
			try {
2362
				xmlClasspath = new String(bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8); // .classpath always encoded with UTF-8
2363
			} catch (UnsupportedEncodingException e) {
2364
				Util.log(e, "Could not read .classpath with UTF-8 encoding"); //$NON-NLS-1$
2365
				// fallback to default
2366
				xmlClasspath = new String(bytes);
2367
			}
2368
		} else {
2373
		} else {
2369
			// when a project is imported, we get a first delta for the addition of the .project, but the .classpath is not accessible
2374
			// when a project is imported, we get a first delta for the addition of the .project, but the .classpath is not accessible
2370
			// so default to using java.io.File
2375
			// so default to using java.io.File
Lines 2375-2381 Link Here
2375
			File file = Util.toLocalFile(location, null/*no progress monitor available*/);
2380
			File file = Util.toLocalFile(location, null/*no progress monitor available*/);
2376
			if (file == null)
2381
			if (file == null)
2377
				throw new IOException("Unable to fetch file from " + location); //$NON-NLS-1$
2382
				throw new IOException("Unable to fetch file from " + location); //$NON-NLS-1$
2378
			byte[] bytes;
2379
			try {
2383
			try {
2380
				bytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(file);
2384
				bytes = org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(file);
2381
			} catch (IOException e) {
2385
			} catch (IOException e) {
Lines 2383-2395 Link Here
2383
					return defaultClasspath();
2387
					return defaultClasspath();
2384
				throw e;
2388
				throw e;
2385
			}
2389
			}
2386
			try {
2390
		}
2387
				xmlClasspath = new String(bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8); // .classpath always encoded with UTF-8
2391
		if (hasUTF8BOM(bytes)) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=240034
2388
			} catch (UnsupportedEncodingException e) {
2392
			int length = bytes.length-IContentDescription.BOM_UTF_8.length;
2389
				Util.log(e, "Could not read .classpath with UTF-8 encoding"); //$NON-NLS-1$
2393
			System.arraycopy(bytes, IContentDescription.BOM_UTF_8.length, bytes = new byte[length], 0, length);
2390
				// fallback to default
2394
		}
2391
				xmlClasspath = new String(bytes);
2395
		String xmlClasspath;
2392
			}
2396
		try {
2397
			xmlClasspath = new String(bytes, org.eclipse.jdt.internal.compiler.util.Util.UTF_8); // .classpath always encoded with UTF-8
2398
		} catch (UnsupportedEncodingException e) {
2399
			Util.log(e, "Could not read .classpath with UTF-8 encoding"); //$NON-NLS-1$
2400
			// fallback to default
2401
			xmlClasspath = new String(bytes);
2393
		}
2402
		}
2394
		return decodeClasspath(xmlClasspath, unknownElements);
2403
		return decodeClasspath(xmlClasspath, unknownElements);
2395
	}
2404
	}

Return to bug 240034