Skip to content

Commit

Permalink
batch compiler.util: writeToDisk use Files.write()
Browse files Browse the repository at this point in the history
instead of streaming, because less source and it is a bit faster.
For example MethodVerifyTest on windows: 65s->64s
  • Loading branch information
EcljpseB0T authored and jukzi committed Dec 10, 2024
1 parent ce043d4 commit 8d1cb65
Showing 1 changed file with 10 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
Expand Down Expand Up @@ -231,7 +229,6 @@ public interface Displayable {
String displayString(Object o);
}

private static final int DEFAULT_WRITING_SIZE = 1024;
public final static String UTF_8 = "UTF-8"; //$NON-NLS-1$
public static final String LINE_SEPARATOR = System.getProperty("line.separator"); //$NON-NLS-1$

Expand Down Expand Up @@ -397,9 +394,9 @@ public static byte[] getFileByteContent(File file) throws IOException {
public static char[] getFileCharContent(File file, String encoding) throws IOException {
return org.eclipse.jdt.internal.compiler.util.Util.getBytesAsCharArray(Files.readAllBytes(file.toPath()), encoding);
}
private static FileOutputStream getFileOutputStream(boolean generatePackagesStructure, String outputPath, String relativeFileName) throws IOException {
private static File getFile(boolean generatePackagesStructure, String outputPath, String relativeFileName) throws IOException {
if (generatePackagesStructure) {
return new FileOutputStream(new File(buildAllDirectoriesInto(outputPath, relativeFileName)));
return new File(buildAllDirectoriesInto(outputPath, relativeFileName));
} else {
String fileName = null;
char fileSeparatorChar = File.separatorChar;
Expand All @@ -422,7 +419,7 @@ private static FileOutputStream getFileOutputStream(boolean generatePackagesStru
fileName = outputPath + fileSeparator + relativeFileName.substring(indexOfPackageSeparator + 1, length);
}
}
return new FileOutputStream(new File(fileName));
return new File(fileName);
}
}

Expand Down Expand Up @@ -915,34 +912,14 @@ public static String toString(Object[] objects, Displayable renderer) {
* @param relativeFileName the given relative file name
* @param classFile the given classFile to write
*/
public static void writeToDisk(boolean generatePackagesStructure, String outputPath, String relativeFileName, ClassFile classFile) throws IOException {
FileOutputStream file = getFileOutputStream(generatePackagesStructure, outputPath, relativeFileName);
/* use java.nio to write
if (true) {
FileChannel ch = file.getChannel();
try {
ByteBuffer buffer = ByteBuffer.allocate(classFile.headerOffset + classFile.contentsOffset);
buffer.put(classFile.header, 0, classFile.headerOffset);
buffer.put(classFile.contents, 0, classFile.contentsOffset);
buffer.flip();
while (true) {
if (ch.write(buffer) == 0) break;
}
} finally {
ch.close();
}
return;
}
*/
try (BufferedOutputStream output = new BufferedOutputStream(file, DEFAULT_WRITING_SIZE)) {
// if no IOException occured, output cannot be null
output.write(classFile.header, 0, classFile.headerOffset);
output.write(classFile.contents, 0, classFile.contentsOffset);
output.flush();
} catch(IOException e) {
throw e;
}
public static void writeToDisk(boolean generatePackagesStructure, String outputPath, String relativeFileName,
ClassFile classFile) throws IOException {
File file = getFile(generatePackagesStructure, outputPath, relativeFileName);
byte[] bytes = Arrays.copyOf(classFile.header, classFile.headerOffset + classFile.contentsOffset);
System.arraycopy(classFile.contents, 0, bytes, classFile.headerOffset, classFile.contentsOffset);
Files.write(file.toPath(), bytes);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void recordNestedType(ClassFile classFile, TypeBinding typeBinding) {
if (classFile.visitedTypes == null) {
Expand Down

0 comments on commit 8d1cb65

Please sign in to comment.