Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoerg Kubitz2021-03-25 09:01:14 +0000
committerFabrice Tiercelin2021-06-08 04:25:15 +0000
commite1b011bac4b199f0885077ad6276f5ee0ca86099 (patch)
tree1160dc27b142a0d095225aa60ef9121ffcbeb0aa
parentfefa153826029c3ce091c710cc0eaea4a7543f73 (diff)
downloadeclipse.jdt.core-e1b011bac4b199f0885077ad6276f5ee0ca86099.tar.gz
eclipse.jdt.core-e1b011bac4b199f0885077ad6276f5ee0ca86099.tar.xz
eclipse.jdt.core-e1b011bac4b199f0885077ad6276f5ee0ca86099.zip
Bug 572277 - [performance] improve Util.getResourceContentsAsCharArray
Length is ignored anyway because the size in bytes is meaningless when reading variable sized char encodings (Bug 149028) Change-Id: I56ab8d4512073bd6466fdfd1e1c3a4c63e82ef7a Signed-off-by: Joerg Kubitz <jkubitz-eclipse@gmx.de> Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/178352 Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/180028 Reviewed-by: Sravan Kumar Lakkimsetti <sravankumarl@in.ibm.com> Reviewed-by: Fabrice Tiercelin <fabrice.tiercelin@yahoo.fr> Tested-by: JDT Bot <jdt-bot@eclipse.org>
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/APIDocumentationTests.java2
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java2
-rw-r--r--org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/EncodingTests.java3
-rw-r--r--org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathSourceJar.java2
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/ManifestAnalyzer.java2
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/Util.java44
-rw-r--r--org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathEntry.java2
-rw-r--r--org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Util.java21
-rw-r--r--org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/indexer/Indexer.java2
9 files changed, 15 insertions, 65 deletions
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/APIDocumentationTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/APIDocumentationTests.java
index 5a6292da86..6e9dc05f32 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/APIDocumentationTests.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/APIDocumentationTests.java
@@ -168,7 +168,7 @@ public void testJavaCoreAPI() throws CoreException, IllegalArgumentException, Il
try (ZipFile zipFile = new ZipFile(jarFile)) {
ZipEntry entry = zipFile.getEntry(PATH_JAVA_CORE_JAVA);
try (InputStream inputStream = zipFile.getInputStream(entry)) {
- sourceChars = Util.getInputStreamAsCharArray(inputStream, (int)entry.getSize(), null);
+ sourceChars = Util.getInputStreamAsCharArray(inputStream, null);
}
}
}
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java
index 0f8dd863e6..acf250ad8f 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java
@@ -151,7 +151,7 @@ public class FormatterRegressionTests extends AbstractJavaModelTests {
zipFile = new ZipFile(fileName);
ZipEntry zipEntry = zipFile.getEntry(zipEntryName);
inputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry));
- return new String(org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(inputStream, -1, null));
+ return new String(org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(inputStream, null));
} catch (IOException e) {
} finally {
try {
diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/EncodingTests.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/EncodingTests.java
index 82315c9a3e..9202f7ca6c 100644
--- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/EncodingTests.java
+++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/EncodingTests.java
@@ -722,7 +722,8 @@ public class EncodingTests extends ModifyingResourceTests {
return result;
}
};
- char[] result = org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(in, (int) file.getLocation().toFile().length(), "UTF-8");
+ char[] result = org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(in,
+ org.eclipse.jdt.internal.compiler.util.Util.UTF_8);
assertSourceEquals(
"Unexpected source",
"abcdefghijklmn",
diff --git a/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathSourceJar.java b/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathSourceJar.java
index 126409d443..d906d7bb4b 100644
--- a/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathSourceJar.java
+++ b/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/ClasspathSourceJar.java
@@ -44,7 +44,7 @@ public class ClasspathSourceJar extends ClasspathJar {
char[] contents = null;
try {
stream = this.zipFile.getInputStream(sourceEntry);
- contents = Util.getInputStreamAsCharArray(stream, -1, this.encoding);
+ contents = Util.getInputStreamAsCharArray(stream, this.encoding);
} finally {
if (stream != null)
stream.close();
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/ManifestAnalyzer.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/ManifestAnalyzer.java
index 79119d3734..48014f82bb 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/ManifestAnalyzer.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/ManifestAnalyzer.java
@@ -43,7 +43,7 @@ public class ManifestAnalyzer {
* @throws IOException if an exception occurs while analyzing the file
*/
public boolean analyzeManifestContents(InputStream inputStream) throws IOException {
- char[] chars = Util.getInputStreamAsCharArray(inputStream, -1, Util.UTF_8);
+ char[] chars = Util.getInputStreamAsCharArray(inputStream, Util.UTF_8);
return analyzeManifestContents(chars);
}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/Util.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/Util.java
index 3e489858f4..c5227433dd 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/Util.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/util/Util.java
@@ -363,7 +363,7 @@ public class Util implements SuffixConstants {
*/
public static char[] bytesToChar(byte[] bytes, String encoding) throws IOException {
- return getInputStreamAsCharArray(new ByteArrayInputStream(bytes), bytes.length, encoding);
+ return getInputStreamAsCharArray(new ByteArrayInputStream(bytes), encoding);
}
@@ -420,7 +420,7 @@ public class Util implements SuffixConstants {
InputStream stream = null;
try {
stream = new FileInputStream(file);
- return getInputStreamAsCharArray(stream, (int) file.length(), encoding);
+ return getInputStreamAsCharArray(stream, encoding);
} finally {
if (stream != null) {
try {
@@ -538,37 +538,14 @@ public class Util implements SuffixConstants {
return contents;
}
- /*
- * NIO support to get input stream as char array.
- * Not used as with JDK 1.4.2 this support is slower than standard IO one...
- * Keep it as comment for future in case of next JDK versions improve performance
- * in this area...
- public static char[] getInputStreamAsCharArray(FileInputStream stream, int length, String encoding)
- throws IOException {
-
- FileChannel channel = stream.getChannel();
- int size = (int)channel.size();
- if (length >= 0 && length < size) size = length;
- Charset charset = encoding==null?systemCharset:Charset.forName(encoding);
- if (charset != null) {
- MappedByteBuffer bbuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, size);
- CharsetDecoder decoder = charset.newDecoder();
- CharBuffer buffer = decoder.decode(bbuffer);
- char[] contents = new char[buffer.limit()];
- buffer.get(contents);
- return contents;
- }
- throw new UnsupportedCharsetException(SYSTEM_FILE_ENCODING);
- }
- */
/**
* Returns the given input stream's contents as a character array.
- * If a length is specified (i.e. if length != -1), this represents the number of bytes in the stream.
* Note this doesn't close the stream.
* @throws IOException if a problem occured reading the stream.
*/
- public static char[] getInputStreamAsCharArray(InputStream stream, int length, String encoding)
+ public static char[] getInputStreamAsCharArray(InputStream stream, String encoding)
throws IOException {
+ //XXX java.nio.file.Files.readString().toCharArray() is faster on recent JDKs
BufferedReader reader = null;
try {
reader = encoding == null
@@ -578,21 +555,12 @@ public class Util implements SuffixConstants {
// encoding is not supported
reader = new BufferedReader(new InputStreamReader(stream));
}
- char[] contents;
+ char[] contents = CharOperation.NO_CHAR;
int totalRead = 0;
- if (length == -1) {
- contents = CharOperation.NO_CHAR;
- } else {
- // length is a good guess when the encoding produces less or the same amount of characters than the file length
- contents = new char[length]; // best guess
- }
while (true) {
int amountRequested;
- if (totalRead < length) {
- // until known length is met, reuse same array sized eagerly
- amountRequested = length - totalRead;
- } else {
+ {
// reading beyond known length
int current = reader.read();
if (current < 0) break;
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathEntry.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathEntry.java
index 629c80b587..38f8857d9a 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathEntry.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/ClasspathEntry.java
@@ -1038,7 +1038,7 @@ public class ClasspathEntry implements IClasspathEntry {
return null;
}
inputStream = zip.getInputStream(manifest);
- char[] chars = getInputStreamAsCharArray(inputStream, -1, UTF_8);
+ char[] chars = getInputStreamAsCharArray(inputStream, UTF_8);
return chars;
} finally {
if (inputStream != null) {
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Util.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Util.java
index 97dd9efe33..9f602c6ebc 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Util.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/Util.java
@@ -1187,25 +1187,6 @@ public class Util {
}
public static char[] getResourceContentsAsCharArray(IFile file, String encoding) throws JavaModelException {
- // Get file length
- // workaround https://bugs.eclipse.org/bugs/show_bug.cgi?id=130736 by using java.io.File if possible
- IPath location = file.getLocation();
- long length;
- if (location == null) {
- // non local file
- try {
- URI locationURI = file.getLocationURI();
- if (locationURI == null)
- throw new CoreException(new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, Messages.bind(Messages.file_notFound, file.getFullPath().toString())));
- length = EFS.getStore(locationURI).fetchInfo().getLength();
- } catch (CoreException e) {
- throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST);
- }
- } else {
- // local file
- length = location.toFile().length();
- }
-
// Get resource contents
InputStream stream= null;
try {
@@ -1214,7 +1195,7 @@ public class Util {
throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST);
}
try {
- return org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(stream, (int) length, encoding);
+ return org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(stream, encoding);
} catch (IOException e) {
throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
} finally {
diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/indexer/Indexer.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/indexer/Indexer.java
index 5bbb6446d5..722e116411 100644
--- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/indexer/Indexer.java
+++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/nd/indexer/Indexer.java
@@ -750,7 +750,7 @@ public final class Indexer {
if (fileName.equals(TypeConstants.META_INF_MANIFEST_MF)) {
try (InputStream inputStream = zipFile.getInputStream(member)) {
- char[] chars = getInputStreamAsCharArray(inputStream, -1, UTF_8);
+ char[] chars = getInputStreamAsCharArray(inputStream, UTF_8);
resourceFile.setManifestContent(chars);
}

Back to the top