diff options
| author | Manoj Palat | 2021-04-30 05:38:25 +0000 |
|---|---|---|
| committer | Manoj Palat | 2021-04-30 05:40:08 +0000 |
| commit | 2685ba0b578528628dc5926cdffb1c0ebcdf7335 (patch) | |
| tree | 1919b82d62e56726bda6ecaafd27d0997de3f74b | |
| parent | ef189e229c5c78d589b7544bfbf1894d96f173af (diff) | |
| download | eclipse.jdt.core-2685ba0b578528628dc5926cdffb1c0ebcdf7335.tar.gz eclipse.jdt.core-2685ba0b578528628dc5926cdffb1c0ebcdf7335.tar.xz eclipse.jdt.core-2685ba0b578528628dc5926cdffb1c0ebcdf7335.zip | |
Revert "Bug 572277 - [performance] improve Util.getResourceContentsAsCharArray"I20210430-1800
This reverts commit 141932dc82a2da30d55c83ad27de394755693779.
Change-Id: Ib7005e634583e3b8cb2b9f96d2c6c0a0523c2263
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/180025
Tested-by: Manoj Palat <manpalat@in.ibm.com>
Reviewed-by: Manoj Palat <manpalat@in.ibm.com>
9 files changed, 61 insertions, 15 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 6e9dc05f32..5a6292da86 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, null); + sourceChars = Util.getInputStreamAsCharArray(inputStream, (int)entry.getSize(), 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 acf250ad8f..0f8dd863e6 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, null)); + return new String(org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(inputStream, -1, 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 9202f7ca6c..82315c9a3e 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,8 +722,7 @@ public class EncodingTests extends ModifyingResourceTests { return result; } }; - char[] result = org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(in, - org.eclipse.jdt.internal.compiler.util.Util.UTF_8); + char[] result = org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(in, (int) file.getLocation().toFile().length(), "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 d906d7bb4b..126409d443 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, this.encoding); + contents = Util.getInputStreamAsCharArray(stream, -1, 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 48014f82bb..79119d3734 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, Util.UTF_8); + char[] chars = Util.getInputStreamAsCharArray(inputStream, -1, 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 c5227433dd..3e489858f4 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), encoding); + return getInputStreamAsCharArray(new ByteArrayInputStream(bytes), bytes.length, encoding); } @@ -420,7 +420,7 @@ public class Util implements SuffixConstants { InputStream stream = null; try { stream = new FileInputStream(file); - return getInputStreamAsCharArray(stream, encoding); + return getInputStreamAsCharArray(stream, (int) file.length(), encoding); } finally { if (stream != null) { try { @@ -538,14 +538,37 @@ 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, String encoding) + public static char[] getInputStreamAsCharArray(InputStream stream, int length, String encoding) throws IOException { - //XXX java.nio.file.Files.readString().toCharArray() is faster on recent JDKs BufferedReader reader = null; try { reader = encoding == null @@ -555,12 +578,21 @@ public class Util implements SuffixConstants { // encoding is not supported reader = new BufferedReader(new InputStreamReader(stream)); } - char[] contents = CharOperation.NO_CHAR; + char[] contents; 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 38f8857d9a..629c80b587 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, UTF_8); + char[] chars = getInputStreamAsCharArray(inputStream, -1, 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 dab92a8979..97dd9efe33 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 @@ -1190,6 +1190,21 @@ public class Util { // 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; @@ -1199,7 +1214,7 @@ public class Util { throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST); } try { - return org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(stream, encoding); + return org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(stream, (int) length, 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 722e116411..5bbb6446d5 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, UTF_8); + char[] chars = getInputStreamAsCharArray(inputStream, -1, UTF_8); resourceFile.setManifestContent(chars); } |
