Skip to content

Commit

Permalink
replace List<Byte> with ByteArrayOutputStream to avoid boxing/unboxin…
Browse files Browse the repository at this point in the history
…g and repeated copying of data

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1915958 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Axel Howind committed Feb 22, 2024
1 parent e5e22bb commit 17c6a1e
Showing 1 changed file with 28 additions and 32 deletions.
60 changes: 28 additions & 32 deletions poi/src/main/java/org/apache/poi/util/HexRead.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,42 +94,38 @@ static public byte[] readData( InputStream stream, int eofChar )
{
int characterCount = 0;
byte b = (byte) 0;
List<Byte> bytes = new ArrayList<>();
final char a = 'a' - 10;
final char A = 'A' - 10;
while ( true ) {
int count = stream.read();
int digitValue = -1;
if ( '0' <= count && count <= '9' ) {
digitValue = count - '0';
} else if ( 'A' <= count && count <= 'F' ) {
digitValue = count - A;
} else if ( 'a' <= count && count <= 'f' ) {
digitValue = count - a;
} else if ( '#' == count ) {
readToEOL( stream );
} else if ( -1 == count || eofChar == count ) {
break;
}
// else: ignore the character
try (ByteArrayOutputStream bytes = new ByteArrayOutputStream()) {
final char a = 'a' - 10;
final char A = 'A' - 10;
while (true) {
int count = stream.read();
int digitValue = -1;
if ('0' <= count && count <= '9') {
digitValue = count - '0';
} else if ('A' <= count && count <= 'F') {
digitValue = count - A;
} else if ('a' <= count && count <= 'f') {
digitValue = count - a;
} else if ('#' == count) {
readToEOL(stream);
} else if (-1 == count || eofChar == count) {
break;
}
// else: ignore the character

if (digitValue != -1) {
b <<= 4;
b += (byte) digitValue;
characterCount++;
if ( characterCount == 2 ) {
bytes.add( Byte.valueOf( b ) );
characterCount = 0;
b = (byte) 0;
if (digitValue != -1) {
b <<= 4;
b += (byte) digitValue;
characterCount++;
if (characterCount == 2) {
bytes.write(b);
characterCount = 0;
b = (byte) 0;
}
}
}
return bytes.toByteArray();
}
Byte[] polished = bytes.toArray(new Byte[0]);
byte[] rval = new byte[polished.length];
for ( int j = 0; j < polished.length; j++ ) {
rval[j] = polished[j].byteValue();
}
return rval;
}

static public byte[] readFromString(String data) {
Expand Down

0 comments on commit 17c6a1e

Please sign in to comment.