mbien commented on code in PR #7344:
URL: https://github.com/apache/netbeans/pull/7344#discussion_r1586557729
##########
platform/openide.util.ui/test/unit/src/org/openide/util/test/TestFileUtils.java:
##########
@@ -48,52 +46,48 @@ public class TestFileUtils {
private TestFileUtils() {}
/**
- * Create a new data file with specified initial contents.
+ * Create a new data file with specified initial contents or overwrite
existing file.
* @param f a file to create (parents will be created automatically)
* @param body the complete contents of the new file (in UTF-8 encoding)
*/
public static File writeFile(File f, String body) throws IOException {
f.getParentFile().mkdirs();
- OutputStream os = new FileOutputStream(f);
- PrintWriter pw = new PrintWriter(new OutputStreamWriter(os,
StandardCharsets.UTF_8));
- pw.print(body);
- pw.flush();
- os.close();
+ Files.write(f.toPath(), body.getBytes(), StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING);
+ // TODO jdk11
+// Files.writeString(f.toPath(), body, StandardOpenOption.CREATE_NEW);
return f;
}
/**
* Read the contents of a file as a single string.
- * @param a data file
+ * @param file data file
* @return its contents (in UTF-8 encoding)
*/
public static String readFile(File file) throws IOException {
- InputStream is = new FileInputStream(file);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte[] buf = new byte[4096];
- int read;
- while ((read = is.read(buf)) != -1) {
- baos.write(buf, 0, read);
- }
- is.close();
- return baos.toString("UTF-8");
+ return new String(Files.readAllBytes(file.toPath()),
StandardCharsets.UTF_8);
+ // TODO jdk11
+// return Files.readString(file.toPath());
}
/**
* Read the contents of a file as a byte array.
- * @param a data file
+ * @param file data file
* @return its raw binary contents
*/
public static byte[] readFileBin(File file) throws IOException {
- InputStream is = new FileInputStream(file);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte[] buf = new byte[4096];
- int read;
- while ((read = is.read(buf)) != -1) {
- baos.write(buf, 0, read);
+ return Files.readAllBytes(file.toPath());
+ }
+
+ /**
+ * Deletes the file or directory and its contents.
+ */
+ public static void deleteFile(File file) throws IOException {
+ if (file.isDirectory() && file.equals(file.getCanonicalFile())) {
Review Comment:
I believe this is a way of checking that the recursion didn't walk through a
symlink and is clearing half of the drive.
Please note that I copied this method (from `NBTestCase`) mostly from the
other util class (there were 15 variants of this method copied around). I tried
to centralize it and made the actual delete operation use `Files#delete` to get
a cause msg next time it fails in CI.
I thought about reimplementing this fully using `Path`, but keeping it as-is
felt like the less risky approach for a hotfix. But now that I know that there
is a shared test utility class, this opens up more cleanup and deduplicaiton in
future.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists