Skip to content

Commit 8d1cb65

Browse files
committed
batch compiler.util: writeToDisk use Files.write()
instead of streaming, because less source and it is a bit faster. For example MethodVerifyTest on windows: 65s->64s
1 parent ce043d4 commit 8d1cb65

File tree

1 file changed

+10
-33
lines changed
  • org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util

1 file changed

+10
-33
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/Util.java

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
*******************************************************************************/
1515
package org.eclipse.jdt.internal.compiler.util;
1616

17-
import java.io.BufferedOutputStream;
1817
import java.io.File;
19-
import java.io.FileOutputStream;
2018
import java.io.IOException;
2119
import java.io.InputStream;
2220
import java.io.PrintWriter;
@@ -231,7 +229,6 @@ public interface Displayable {
231229
String displayString(Object o);
232230
}
233231

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

@@ -397,9 +394,9 @@ public static byte[] getFileByteContent(File file) throws IOException {
397394
public static char[] getFileCharContent(File file, String encoding) throws IOException {
398395
return org.eclipse.jdt.internal.compiler.util.Util.getBytesAsCharArray(Files.readAllBytes(file.toPath()), encoding);
399396
}
400-
private static FileOutputStream getFileOutputStream(boolean generatePackagesStructure, String outputPath, String relativeFileName) throws IOException {
397+
private static File getFile(boolean generatePackagesStructure, String outputPath, String relativeFileName) throws IOException {
401398
if (generatePackagesStructure) {
402-
return new FileOutputStream(new File(buildAllDirectoriesInto(outputPath, relativeFileName)));
399+
return new File(buildAllDirectoriesInto(outputPath, relativeFileName));
403400
} else {
404401
String fileName = null;
405402
char fileSeparatorChar = File.separatorChar;
@@ -422,7 +419,7 @@ private static FileOutputStream getFileOutputStream(boolean generatePackagesStru
422419
fileName = outputPath + fileSeparator + relativeFileName.substring(indexOfPackageSeparator + 1, length);
423420
}
424421
}
425-
return new FileOutputStream(new File(fileName));
422+
return new File(fileName);
426423
}
427424
}
428425

@@ -915,34 +912,14 @@ public static String toString(Object[] objects, Displayable renderer) {
915912
* @param relativeFileName the given relative file name
916913
* @param classFile the given classFile to write
917914
*/
918-
public static void writeToDisk(boolean generatePackagesStructure, String outputPath, String relativeFileName, ClassFile classFile) throws IOException {
919-
FileOutputStream file = getFileOutputStream(generatePackagesStructure, outputPath, relativeFileName);
920-
/* use java.nio to write
921-
if (true) {
922-
FileChannel ch = file.getChannel();
923-
try {
924-
ByteBuffer buffer = ByteBuffer.allocate(classFile.headerOffset + classFile.contentsOffset);
925-
buffer.put(classFile.header, 0, classFile.headerOffset);
926-
buffer.put(classFile.contents, 0, classFile.contentsOffset);
927-
buffer.flip();
928-
while (true) {
929-
if (ch.write(buffer) == 0) break;
930-
}
931-
} finally {
932-
ch.close();
933-
}
934-
return;
935-
}
936-
*/
937-
try (BufferedOutputStream output = new BufferedOutputStream(file, DEFAULT_WRITING_SIZE)) {
938-
// if no IOException occured, output cannot be null
939-
output.write(classFile.header, 0, classFile.headerOffset);
940-
output.write(classFile.contents, 0, classFile.contentsOffset);
941-
output.flush();
942-
} catch(IOException e) {
943-
throw e;
944-
}
915+
public static void writeToDisk(boolean generatePackagesStructure, String outputPath, String relativeFileName,
916+
ClassFile classFile) throws IOException {
917+
File file = getFile(generatePackagesStructure, outputPath, relativeFileName);
918+
byte[] bytes = Arrays.copyOf(classFile.header, classFile.headerOffset + classFile.contentsOffset);
919+
System.arraycopy(classFile.contents, 0, bytes, classFile.headerOffset, classFile.contentsOffset);
920+
Files.write(file.toPath(), bytes);
945921
}
922+
946923
@SuppressWarnings({ "rawtypes", "unchecked" })
947924
public static void recordNestedType(ClassFile classFile, TypeBinding typeBinding) {
948925
if (classFile.visitedTypes == null) {

0 commit comments

Comments
 (0)