Skip to content

Commit

Permalink
[performance] Use new IFile API: IFile.readAllBytes() / readAllChars()
Browse files Browse the repository at this point in the history
Instead of streaming IFile.getContents(true)
  • Loading branch information
EcljpseB0T authored and jukzi committed Jun 13, 2024
1 parent 5fade18 commit 18d9a53
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 157 deletions.
2 changes: 1 addition & 1 deletion org.eclipse.jdt.apt.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Export-Package: com.sun.mirror.apt,
org.eclipse.jdt.apt.core.util
Require-Bundle: org.eclipse.jdt.core;bundle-version="[3.36.0,4.0.0)",
org.eclipse.core.runtime;bundle-version="[3.29.0,4.0.0)",
org.eclipse.core.resources;bundle-version="[3.6.0,4.0.0)",
org.eclipse.core.resources;bundle-version="[3.21.0,4.0.0)",
org.apache.ant;bundle-version="1.6.5"
Bundle-Activator: org.eclipse.jdt.apt.core.internal.AptPlugin
Bundle-Vendor: %providerName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
Expand Down Expand Up @@ -126,32 +126,18 @@ public static void makeDerivedParentFolders (IContainer container) throws CoreEx
}

/**
* Returns the contents of a file as a string in UTF8 format
* Returns the contents of a IFile as a string in UTF8 format
*/
public static String getContentsOfIFile(IFile file) throws IOException, CoreException {
return getContents(file.getContents(true));
}

public static String getContentsOfFile(File file) throws IOException {
return getContents(new FileInputStream(file));
}
public static String getContentsOfIFile(IFile file) throws IOException, CoreException {
return new String(file.readAllBytes(), StandardCharsets.UTF_8);
}

private static String getContents(InputStream in) throws IOException {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[512];
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.close();
String s = new String(out.toByteArray(), "UTF8"); //$NON-NLS-1$
return s;
}
finally {
try {in.close();} catch (IOException ioe) {}
}
}
/**
* Returns the contents of a file as a string in UTF8 format
*/
public static String getContentsOfFile(File file) throws IOException {
return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
}

/**
* Stores a string into an Eclipse file in UTF8 format. The file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageN
return null; // most common case

try (InputStream inputStream = jfo.openInputStream()) {
ClassFileReader reader = ClassFileReader.read(inputStream, qualifiedBinaryFileName);
ClassFileReader reader = ClassFileReader.read(inputStream.readAllBytes(), qualifiedBinaryFileName);
if (reader != null) {
return new NameEnvironmentAnswer(reader, fetchAccessRestriction(qualifiedBinaryFileName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ public static ClassFileReader read(File file, boolean fullyInitialize) throws Cl
return classFileReader;
}

public static ClassFileReader read(InputStream stream, String fileName) throws ClassFormatException, IOException {
return read(stream, fileName, false);
public static ClassFileReader read(byte[] classFileBytes, String fileName) throws ClassFormatException, IOException {
return read(classFileBytes, fileName, false);
}

public static ClassFileReader read(InputStream stream, String fileName, boolean fullyInitialize) throws ClassFormatException, IOException {
byte classFileBytes[] = Util.getInputStreamAsByteArray(stream);
public static ClassFileReader read(byte[] classFileBytes, String fileName, boolean fullyInitialize)
throws ClassFormatException {
ClassFileReader classFileReader = new ClassFileReader(classFileBytes, fileName.toCharArray());
if (fullyInitialize) {
classFileReader.initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,10 @@ public static char[] getInputStreamAsCharArray(InputStream stream, String encod
throws IOException {
byte[] byteContents = getInputStreamAsByteArray(stream);

return getBytesAsCharArray(byteContents, encoding);
}

public static char[] getBytesAsCharArray(byte[] byteContents, String encoding) {
Charset charset;
try {
charset = Charset.forName(encoding);
Expand Down
4 changes: 2 additions & 2 deletions org.eclipse.jdt.core.tests.builder/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.jdt.core.tests.builder; singleton:=true
Bundle-Version: 3.12.400.qualifier
Bundle-Version: 3.12.500.qualifier
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Export-Package: org.eclipse.jdt.core.tests.builder
Require-Bundle: org.junit;bundle-version="3.8.1",
org.eclipse.jdt.core;bundle-version="[3.36.0,4.0.0)",
org.eclipse.jdt.core.tests.compiler;bundle-version="[3.4.0,4.0.0)",
org.eclipse.core.resources;bundle-version="[3.2.0,4.0.0)",
org.eclipse.core.resources;bundle-version="[3.21.0,4.0.0)",
org.eclipse.core.runtime;bundle-version="[3.29.0,4.0.0)",
org.eclipse.test.performance;bundle-version="[3.1.0,4.0.0)",
org.eclipse.jdt.annotation;bundle-version="[1.1.0,2.0.0)";resolution:=optional,
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.jdt.core.tests.builder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<relativePath>../tests-pom/</relativePath>
</parent>
<artifactId>org.eclipse.jdt.core.tests.builder</artifactId>
<version>3.12.400-SNAPSHOT</version>
<version>3.12.500-SNAPSHOT</version>
<packaging>eclipse-test-plugin</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,21 +423,10 @@ public void test0107() throws JavaModelException {

assertEquals("Wrong type", IResource.FILE, resources[0].getType());
IFile classFile = (IFile) resources[0];
InputStream stream = null;
try {
stream = classFile.getContents();
ClassFileReader.read(stream, "C.java");
ClassFileReader.read(classFile.readAllBytes(), "C.java");
} catch (Exception e) {
e.printStackTrace();
assertTrue("Should not happen", false);
} finally {
if (stream != null) {
try {
stream.close();
} catch(IOException e) {
// ignore
}
}
throw new AssertionError(e);
}
}
private String getResourceOuput(IResource[] resources) {
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.jdt.core.tests.compiler/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Require-Bundle: org.junit;bundle-version="3.8.1",
org.eclipse.jdt.core;bundle-version="[3.36.0,4.0.0)",
org.eclipse.core.runtime;bundle-version="[3.29.0,4.0.0)",
org.eclipse.test.performance;bundle-version="[3.10.0,4.0.0)",
org.eclipse.core.resources;bundle-version="[3.2.0,4.0.0)",
org.eclipse.core.resources;bundle-version="[3.21.0,4.0.0)",
org.eclipse.jdt.annotation;bundle-version="[1.1.0,2.0.0)";resolution:=optional,
org.eclipse.jdt.annotation;bundle-version="[2.0.0,3.0.0)";resolution:=optional
Import-Package: jakarta.annotation;version="[2.1.0,3.0.0)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
Expand Down Expand Up @@ -1381,24 +1380,10 @@ protected void checkClassFile(String directoryName, String className, String dis
if (index == -1) {
assertEquals("Wrong contents", expectedOutput, result);
}
FileInputStream stream = null;
try {
stream = new FileInputStream(f);
ClassFileReader.read(stream, className + ".class", true);
} catch (org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException e) {
e.printStackTrace();
assertTrue("ClassFormatException", false);
} catch (IOException e) {
e.printStackTrace();
assertTrue("IOException", false);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
/* ignore */
}
}
ClassFileReader.read(Files.readAllBytes(f.toPath()), className + ".class", true);
} catch (Exception e) {
throw new AssertionError(e);
}
} finally {
removeTempClass(className);
Expand All @@ -1418,24 +1403,10 @@ protected ClassFileReader getInternalClassFile(String directoryName, String clas
}
File f = new File(directory, disassembledClassName + ".class");
ClassFileReader classFileReader = null;
FileInputStream stream = null;
try {
stream = new FileInputStream(f);
classFileReader = ClassFileReader.read(stream, className + ".class", true);
} catch (org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException e) {
e.printStackTrace();
assertTrue("ClassFormatException", false);
} catch (IOException e) {
e.printStackTrace();
assertTrue("IOException", false);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
/* ignore */
}
}
classFileReader = ClassFileReader.read(Files.readAllBytes(f.toPath()), className + ".class", true);
} catch (Exception e) {
throw new AssertionError(e);
}
return classFileReader;
} finally {
Expand Down Expand Up @@ -1468,19 +1439,7 @@ protected void checkDisassembledClassFile(String fileName, String className, Str
fail("ClassFormatException swallowed in Disassembler:\n..." + result.substring(start, end));
}

FileInputStream stream = null;
try {
stream = new FileInputStream(classFile);
ClassFileReader.read(stream, className + ".class", true);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
/* ignore */
}
}
}
ClassFileReader.read(Files.readAllBytes(classFile.toPath()), className + ".class", true);
}

protected void compileAndDeploy(String source, String directoryName, String className, boolean suppressConsole) {
Expand Down Expand Up @@ -1712,30 +1671,15 @@ protected void addIndexEntry(char[] category, char[] key) {

protected ClassFileReader getClassFileReader(String fileName, String className) {
File classFile = new File(fileName);
if (!classFile.exists()) {
assertTrue(".class file doesn't exist", false);
}
FileInputStream stream = null;
assertTrue(".class file doesn't exist:" + fileName, classFile.exists());
try {
stream = new FileInputStream(classFile);
ClassFileReader reader = ClassFileReader.read(stream, className + ".class", true);
ClassFileReader reader = ClassFileReader.read(Files.readAllBytes(classFile.toPath()), className + ".class", true);
return reader;
} catch (org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException e) {
e.printStackTrace();
assertTrue("ClassFormatException", false);
throw new AssertionError(e);
} catch (IOException e) {
e.printStackTrace();
assertTrue("IOException", false);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
/* ignore */
}
}
throw new AssertionError(e);
}
return null;
}

protected INameEnvironment[] getClassLibs(boolean useDefaultClasspaths, Map<String, String> options) {
Expand Down
4 changes: 2 additions & 2 deletions org.eclipse.jdt.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ Export-Package: org.eclipse.jdt.core,
org.eclipse.jdt.internal.formatter;x-friends:="org.eclipse.jdt.core.tests.model, org.eclipse.jdt.core.tests.compiler, org.eclipse.jdt.core.tests.builder, org.eclipse.jdt.core.tests.performance, org.eclipse.jdt.ui.tests, org.eclipse.jdt.ui.tests",
org.eclipse.jdt.internal.formatter.linewrap;x-friends:="org.eclipse.jdt.core.tests.model, org.eclipse.jdt.core.tests.compiler, org.eclipse.jdt.core.tests.builder, org.eclipse.jdt.core.tests.performance, org.eclipse.jdt.ui.tests",
org.eclipse.jdt.internal.formatter.old;x-friends:="org.eclipse.jdt.core.tests.model, org.eclipse.jdt.core.tests.compiler, org.eclipse.jdt.core.tests.builder, org.eclipse.jdt.core.tests.performance, org.eclipse.jdt.ui.tests"
Require-Bundle: org.eclipse.core.resources;bundle-version="[3.18.0,4.0.0)",
Require-Bundle: org.eclipse.core.resources;bundle-version="[3.21.0,4.0.0)",
org.eclipse.core.runtime;bundle-version="[3.29.0,4.0.0)",
org.eclipse.core.filesystem;bundle-version="[1.7.0,2.0.0)",
org.eclipse.core.filesystem;bundle-version="[1.11.0,2.0.0)",
org.eclipse.text;bundle-version="[3.6.0,4.0.0)",
org.eclipse.team.core;bundle-version="[3.1.0,4.0.0)";resolution:=optional,
org.eclipse.jdt.core.compiler.batch;bundle-version="3.38.0";visibility:=reexport
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,23 +667,15 @@ public char[] getContents() {
// no need to force opening of CU to get the content
// also this cannot be a working copy, as its buffer is never closed while the working copy is alive
IFile file = (IFile) getResource();
// Get encoding from file
String encoding;
try {
encoding = file.getCharset();
} catch(CoreException ce) {
// do not use any encoding
encoding = null;
}
try {
return Util.getResourceContentsAsCharArray(file, encoding);
return Util.getResourceContentsAsCharArray(file);
} catch (JavaModelException e) {
if (JavaModelManager.getJavaModelManager().abortOnMissingSource.get() == Boolean.TRUE) {
IOException ioException =
e.getJavaModelStatus().getCode() == IJavaModelStatusConstants.IO_EXCEPTION ?
(IOException)e.getException() :
new IOException(e.getMessage());
throw new AbortCompilationUnit(null, ioException, encoding);
throw new AbortCompilationUnit(null, ioException, null);
} else {
Util.log(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1172,36 +1172,28 @@ private static String[] decodeArgumentString(int length, String argumentsString)
* Returns the given file's contents as a byte array.
*/
public static byte[] getResourceContentsAsByteArray(IFile file) throws JavaModelException {
try (InputStream stream = file.getContents(true)) {
return org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsByteArray(stream);
try {
return file.readAllBytes();
} catch (CoreException e) {
throw new JavaModelException(e);
} catch (IOException e) {
throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST);
}
}

/**
* Returns the given file's contents as a character array.
*/
public static char[] getResourceContentsAsCharArray(IFile file) throws JavaModelException {
// Get encoding from file
String encoding;
try {
encoding = file.getCharset();
} catch(CoreException ce) {
// do not use any encoding
encoding = null;
return file.readAllChars();
} catch (CoreException e) {
throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST);
}
return getResourceContentsAsCharArray(file, encoding);
}

public static char[] getResourceContentsAsCharArray(IFile file, String encoding) throws JavaModelException {
// Get resource contents
try (InputStream stream = file.getContents(true)) {
return org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(stream, encoding);
} catch (IOException e) {
throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
try {
return org.eclipse.jdt.internal.compiler.util.Util.getBytesAsCharArray(file.readAllBytes(), encoding);
} catch (CoreException e) {
throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST);
}
Expand Down Expand Up @@ -1906,9 +1898,7 @@ public static void log(Throwable e) {
}

public static ClassFileReader newClassFileReader(IResource resource) throws CoreException, ClassFormatException, IOException {
try (InputStream in = ((IFile) resource).getContents(true)) {
return ClassFileReader.read(in, resource.getFullPath().toString());
}
return ClassFileReader.read(((IFile) resource).readAllBytes(), resource.getFullPath().toString());
}

/**
Expand Down
Loading

0 comments on commit 18d9a53

Please sign in to comment.