Skip to content

Commit

Permalink
✨ feat: add unify functions for Os4j #4
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jul 6, 2024
1 parent b1c7ee3 commit 523aa37
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions plugin/src/main/groovy/org/unify4j/common/Os4j.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.unify4j.common;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.CompletionHandler;
Expand Down Expand Up @@ -670,6 +667,39 @@ public static void readFileEachLinesIfNeeded(String filename, ReaderAsyncCallbac
readFileEachLinesIfNeeded(toPath(filename), callback);
}

/**
* Reads the content of a specific file and keeps the format content of the file.
*
* @param filename The path to the file to read.
* @return The content of the file as a string.
* @throws IOException If an I/O error occurs.
*/
public static String readFileKeepFormat(Path filename) throws IOException {
if (!exists(filename)) {
return "";
}
StringBuilder builder = new StringBuilder();
try (BufferedReader reader = Files.newBufferedReader(filename)) {
char[] buffer = new char[8192]; // Read in chunks of 8KB
int i;
while ((i = reader.read(buffer)) != -1) {
builder.append(buffer, 0, i);
}
}
return builder.toString();
}

/**
* Reads the content of a specific file and keeps the format content of the file.
*
* @param filename The path to the file to read.
* @return The content of the file as a string.
* @throws IOException If an I/O error occurs.
*/
public static String readFileKeepFormat(String filename) throws IOException {
return readFileKeepFormat(toPath(filename));
}

/**
* Traverses the file tree rooted at the given path and invokes a callback for each file and directory encountered.
*
Expand Down

0 comments on commit 523aa37

Please sign in to comment.