include qligier's performance improvements

This commit is contained in:
Grahame Grieve 2024-01-16 16:18:05 +11:00
parent de0024ceec
commit 516b932717
1 changed files with 67 additions and 147 deletions

View File

@ -43,8 +43,11 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -59,224 +62,141 @@ public class TextFile {
public static List<String> readAllLines(String path) throws IOException public static List<String> readAllLines(String path) throws IOException
{ {
List<String> result = new ArrayList<String>(); final File file = new CSFile(path);
return Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
File file = new CSFile(path);
FileInputStream fs = new FileInputStream(file);
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(fs,"UTF-8"));
while( reader.ready() )
result.add(reader.readLine());
reader.close();
return result;
} finally {
fs.close();
}
} }
public static void writeAllLines(String path, List<String> lines) throws IOException public static void writeAllLines(String path, List<String> lines) throws IOException
{ {
File file = new CSFile(path); final File file = new CSFile(path);
FileOutputStream s = new FileOutputStream(file); Files.write(file.toPath(), lines, StandardCharsets.UTF_8);
OutputStreamWriter sw = new OutputStreamWriter(s, "UTF-8");
for( String line : lines )
sw.write(line + "\r\n");
sw.flush();
s.close();
} }
public static void stringToStream(String content, OutputStream stream) throws IOException { public static void stringToStream(final String content, final OutputStream stream) throws IOException {
OutputStreamWriter sw = new OutputStreamWriter(stream, "UTF-8"); stream.write(content.getBytes(StandardCharsets.UTF_8));
sw.write(content);
sw.flush();
sw.close();
} }
public static byte[] stringToBytes(String content) throws IOException { public static byte[] stringToBytes(final String content) throws IOException {
ByteArrayOutputStream bs = new ByteArrayOutputStream(); return content.getBytes(StandardCharsets.UTF_8);
OutputStreamWriter sw = new OutputStreamWriter(bs, "UTF-8");
sw.write(content);
sw.flush();
sw.close();
return bs.toByteArray();
} }
public static void stringToFile(String content, String path) throws IOException { public static void stringToFile(final String content, final String path) throws IOException {
File file = new CSFile(path); final File file = new CSFile(path);
stringToFile(content, file); stringToFile(content, file);
} }
public static void stringToFile(String content, File file) throws IOException { public static void stringToFile(final String content, final File file) throws IOException {
FileOutputStream fs = new FileOutputStream(file); try (final OutputStream output = Files.newOutputStream(file.toPath())) {
try { output.write(content.getBytes(StandardCharsets.UTF_8));
OutputStreamWriter sw = new OutputStreamWriter(fs, "UTF-8");
sw.write(content);
sw.flush();
sw.close();
} finally {
fs.close();
} }
} }
public static void stringToFileWithBOM(String content, File file) throws IOException { public static void stringToFileWithBOM(final String content, final File file) throws IOException {
FileOutputStream fs = new FileOutputStream(file); try (final OutputStream output = Files.newOutputStream(file.toPath())) {
OutputStreamWriter sw = new OutputStreamWriter(fs, "UTF-8"); output.write(new byte[]{(byte)239, (byte)187, (byte)191});
sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter output.write(content.getBytes(StandardCharsets.UTF_8));
sw.write(content); }
sw.flush();
sw.close();
} }
public static void stringToFileWithBom(String content, String path) throws IOException { public static void stringToFileWithBom(final String content, final String path) throws IOException {
File file = new CSFile(path); final File file = new CSFile(path);
stringToFileWithBOM(content, file); stringToFileWithBOM(content, file);
} }
public static void stringToFileNoPrefix(String content, String path) throws IOException { public static String fileToString(final File f) throws FileNotFoundException, IOException {
File file = new CSFile(path); // Files.readString(Path) will fail on invalid UTF-8 byte sequences, so we use Files.readAllBytes() instead.
OutputStreamWriter sw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); // This would happen when reading an XSLX file, for example.
sw.write(content); return new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8);
sw.flush();
sw.close();
} }
public static String fileToString(File f) throws FileNotFoundException, IOException { public static String fileToString(final String src) throws FileNotFoundException, IOException {
FileInputStream fs = new FileInputStream(f); final CSFile f = new CSFile(src);
try {
return streamToString(fs);
} finally {
fs.close();
}
}
public static String fileToString(String src) throws FileNotFoundException, IOException {
CSFile f = new CSFile(src);
if (!f.exists()) { if (!f.exists()) {
throw new IOException("File "+src+" not found"); throw new IOException("File "+src+" not found");
} }
FileInputStream fs = new FileInputStream(f); return fileToString(f);
try {
return streamToString(fs);
} finally {
fs.close();
}
} }
public static String streamToString(InputStream input) throws IOException { public static String streamToString(final InputStream input) throws IOException {
InputStreamReader sr = new InputStreamReader(input, "UTF-8"); return new String(input.readAllBytes(), StandardCharsets.UTF_8).replace("\uFEFF", "");
StringBuilder b = new StringBuilder();
//while (sr.ready()) { Commented out by Claude Nanjo (1/14/2014) - sr.ready() always returns false - please remove if change does not impact other areas of codebase
int i = -1;
while((i = sr.read()) > -1) {
String s = Character.toString(i);
b.append(s);
}
sr.close();
return b.toString().replace("\uFEFF", "");
} }
public static byte[] streamToBytes(InputStream input) throws IOException { public static byte[] streamToBytes(final InputStream input) throws IOException {
if (input == null) { if (input == null) {
return null; return null;
} }
ByteArrayOutputStream r = new ByteArrayOutputStream(2048); final byte[] read = input.readAllBytes();
byte[] read = new byte[512];
for (int i; -1 != (i = input.read(read)); r.write(read, 0, i));
input.close(); input.close();
return r.toByteArray(); return read;
} }
public static byte[] streamToBytesNoClose(InputStream input) throws IOException { public static byte[] streamToBytesNoClose(final InputStream input) throws IOException {
if (input == null) { if (input == null) {
return null; return null;
} }
ByteArrayOutputStream r = new ByteArrayOutputStream(2048); return input.readAllBytes();
byte[] read = new byte[512];
for (int i; -1 != (i = input.read(read)); r.write(read, 0, i));
return r.toByteArray();
} }
public static void bytesToFile(byte[] bytes, String path) throws IOException { public static void bytesToFile(final byte[] bytes, final String path) throws IOException {
File file = new CSFile(path); try (final OutputStream sw = new FileOutputStream(new CSFile(path))) {
OutputStream sw = new FileOutputStream(file);
sw.write(bytes); sw.write(bytes);
sw.flush(); }
sw.close();
} }
public static void bytesToFile(byte[] bytes, File f) throws IOException { public static void bytesToFile(final byte[] bytes, final File f) throws IOException {
OutputStream sw = new FileOutputStream(f); try (final OutputStream sw = new FileOutputStream(f)) {
sw.write(bytes); sw.write(bytes);
sw.flush(); }
sw.close();
} }
public static void appendBytesToFile(byte[] bytes, String path) throws IOException { public static void appendBytesToFile(final byte[] bytes, final String path) throws IOException {
byte[] linebreak = new byte[] {13, 10}; byte[] linebreak = new byte[] {13, 10};
Files.write(Paths.get(path), linebreak, StandardOpenOption.APPEND); Files.write(Paths.get(path), linebreak, StandardOpenOption.APPEND);
Files.write(Paths.get(path), bytes, StandardOpenOption.APPEND); Files.write(Paths.get(path), bytes, StandardOpenOption.APPEND);
} }
public static byte[] fileToBytes(String srcFile) throws FileNotFoundException, IOException { public static byte[] fileToBytes(final String srcFile) throws FileNotFoundException, IOException {
FileInputStream fs = new FileInputStream(new CSFile(srcFile)); final File f = new CSFile(srcFile);
try { return Files.readAllBytes(f.toPath());
return streamToBytes(fs);
} finally {
fs.close();
}
} }
/** /**
* *
* fileToBytes insists in case correctness to ensure that stuff works across linux and windows, but it's not always appropriate to ceheck case (e.g. validator parameters) * fileToBytes insists in case correctness to ensure that stuff works across linux and windows, but it's not always appropriate to check case (e.g. validator parameters)
* *
* @param srcFile * @param srcFile
* @return * @return
* @throws FileNotFoundException * @throws FileNotFoundException
* @throws IOException * @throws IOException
*/ */
public static byte[] fileToBytesNCS(String srcFile) throws FileNotFoundException, IOException { public static byte[] fileToBytesNCS(final String srcFile) throws FileNotFoundException, IOException {
FileInputStream fs = new FileInputStream(new File(srcFile)); return Files.readAllBytes(Path.of(srcFile));
try {
return streamToBytes(fs);
} finally {
fs.close();
}
} }
public static byte[] fileToBytes(File file) throws FileNotFoundException, IOException { public static byte[] fileToBytes(final File file) throws FileNotFoundException, IOException {
FileInputStream fs = new FileInputStream(file); return Files.readAllBytes(file.toPath());
try {
return streamToBytes(fs);
} finally {
fs.close();
}
} }
public static String bytesToString(byte[] bs) throws IOException { public static String bytesToString(final byte[] bs) throws IOException {
return streamToString(new ByteArrayInputStream(bs)); return new String(bs, StandardCharsets.UTF_8);
} }
public static String bytesToString(byte[] bs, boolean removeBOM) throws IOException { public static String bytesToString(final byte[] bs, final boolean removeBOM) throws IOException {
final String read = new String(bs, StandardCharsets.UTF_8);
if (removeBOM) if (removeBOM)
return streamToString(new ByteArrayInputStream(bs)).replace("\uFEFF", ""); return read.replace("\uFEFF", "");
else else
return streamToString(new ByteArrayInputStream(bs)); return read;
} }
public static void streamToFile(InputStream stream, String filename) throws IOException { public static void streamToFile(final InputStream stream, final String filename) throws IOException {
byte[] cnt = streamToBytes(stream); Files.copy(stream, Path.of(filename), StandardCopyOption.REPLACE_EXISTING);
bytesToFile(cnt, filename); stream.close();
} }
public static void streamToFileNoClose(InputStream stream, String filename) throws IOException { public static void streamToFileNoClose(final InputStream stream, final String filename) throws IOException {
byte[] cnt = streamToBytesNoClose(stream); Files.copy(stream, Path.of(filename), StandardCopyOption.REPLACE_EXISTING);
bytesToFile(cnt, filename);
} }
} }