Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

batch compiler.util: writeToDisk use Files.write() #3425

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading