8317633: Modernize text.testlib.HexDumpReader
Reviewed-by: lancea, naoto, mli
This commit is contained in:
parent
5e8d893c48
commit
8e0686b065
@ -21,15 +21,14 @@
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.nio.file.Files;
|
||||
import java.util.HexFormat;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* HexDumpReader provides utility methods to read a hex dump text file
|
||||
@ -43,78 +42,33 @@ public final class HexDumpReader {
|
||||
// Utility class should not be instantiated
|
||||
private HexDumpReader() {}
|
||||
|
||||
public static InputStream getStreamFromHexDump(String fileName) {
|
||||
/*
|
||||
* Converts a Hex dump file given by the String name into an InputStream
|
||||
* containing bytes. The expected format of the file should be given as lines
|
||||
* that are either:
|
||||
* - Valid hexadecimal value(s) (two hexadecimal characters) combined with no
|
||||
* spaces between. E.g. "ace95365" represents four hexadecimal values.
|
||||
* There should not be an odd amount of hexadecimal characters. E.g. "ace"
|
||||
* - Contain leading comments given by '#' (which are ignored). E.g. "#foo"
|
||||
* Non-leading comments are not allowed. E.g. "ace953 #foo"
|
||||
* - Empty (which are ignored).
|
||||
*/
|
||||
public static InputStream getStreamFromHexDump(String fileName) throws IOException {
|
||||
return getStreamFromHexDump(new File(System.getProperty("test.src", "."),
|
||||
fileName));
|
||||
fileName));
|
||||
}
|
||||
|
||||
public static InputStream getStreamFromHexDump(File hexFile) {
|
||||
ByteArrayBuilder bab = new ByteArrayBuilder();
|
||||
int lineNo = 0;
|
||||
try (BufferedReader reader
|
||||
= new BufferedReader(new InputStreamReader(new FileInputStream(hexFile),
|
||||
StandardCharsets.US_ASCII))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
lineNo++;
|
||||
line = line.trim();
|
||||
// Skip blank and comment lines.
|
||||
if (line.length() == 0) {
|
||||
continue;
|
||||
}
|
||||
int x = line.indexOf('#');
|
||||
if (x == 0) {
|
||||
continue;
|
||||
}
|
||||
if (x > 0) {
|
||||
line = line.substring(0, x).trim();
|
||||
}
|
||||
int len = line.length();
|
||||
for (int i = 0; i < len; i += 2) {
|
||||
bab.put((byte)Integer.parseInt(line, i, i + 2, 16));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(hexFile.getName() + ":error:" + lineNo + ": " + e, e);
|
||||
}
|
||||
return new ByteArrayInputStream(bab.toArray());
|
||||
// Overloaded version of getStreamFromHexDump() that takes the File itself as input.
|
||||
public static InputStream getStreamFromHexDump(File hexFile) throws IOException {
|
||||
// This hexString should only consist of valid hexadecimal digits and be even
|
||||
// otherwise an exception will be thrown when converting to bytes
|
||||
String hexString = Files.readAllLines(hexFile.toPath(), StandardCharsets.UTF_8)
|
||||
.stream()
|
||||
.map(String::trim)
|
||||
.filter(s -> !s.isEmpty() && !s.startsWith("#"))
|
||||
.collect(Collectors.joining());
|
||||
// Iterate the hex string and convert it to bytes
|
||||
byte[] bArray = HexFormat.of().parseHex(hexString);
|
||||
return new ByteArrayInputStream(bArray);
|
||||
}
|
||||
|
||||
|
||||
private static class ByteArrayBuilder {
|
||||
private static final int BUFFER_SIZE = 4096;
|
||||
|
||||
private int size;
|
||||
private List<byte[]> bytes;
|
||||
private byte[] current;
|
||||
private int offset;
|
||||
|
||||
ByteArrayBuilder() {
|
||||
bytes = new ArrayList<>();
|
||||
current = new byte[BUFFER_SIZE];
|
||||
}
|
||||
|
||||
void put(byte b) {
|
||||
if (offset == BUFFER_SIZE) {
|
||||
bytes.add(current);
|
||||
current = new byte[BUFFER_SIZE];
|
||||
offset = 0;
|
||||
}
|
||||
current[offset++] = b;
|
||||
size++;
|
||||
}
|
||||
|
||||
byte[] toArray() {
|
||||
byte[] buf = new byte[size];
|
||||
int ptr = 0;
|
||||
for (byte[] ba : bytes) {
|
||||
System.arraycopy(ba, 0, buf, ptr, ba.length);
|
||||
ptr += ba.length;
|
||||
}
|
||||
System.arraycopy(current, 0, buf, ptr, offset);
|
||||
assert ptr + offset == size;
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user