diff options
author | Axel Howind <axh@apache.org> | 2024-02-22 20:48:04 +0000 |
---|---|---|
committer | Axel Howind <axh@apache.org> | 2024-02-22 20:48:04 +0000 |
commit | 17c6a1ec509ad6bae26145d42c842035ac665c59 (patch) | |
tree | 453d4938cce5d718d9dee733db5b1ae4f9b37a1a | |
parent | e5e22bb1f2226b6396750e2bbc3f367bf1dc4478 (diff) | |
download | poi-17c6a1ec509ad6bae26145d42c842035ac665c59.tar.gz poi-17c6a1ec509ad6bae26145d42c842035ac665c59.zip |
replace List<Byte> with ByteArrayOutputStream to avoid boxing/unboxing and repeated copying of data
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1915958 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | poi/src/main/java/org/apache/poi/util/HexRead.java | 60 |
1 files changed, 28 insertions, 32 deletions
diff --git a/poi/src/main/java/org/apache/poi/util/HexRead.java b/poi/src/main/java/org/apache/poi/util/HexRead.java index 2da79c6c23..4a1b7a1c4e 100644 --- a/poi/src/main/java/org/apache/poi/util/HexRead.java +++ b/poi/src/main/java/org/apache/poi/util/HexRead.java @@ -94,42 +94,38 @@ public class HexRead { { 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) { |