From: Javen O'Neal Date: Thu, 7 Jul 2016 00:48:52 +0000 (+0000) Subject: findbugs: replace switch statement with fall-throughs with if statement, remove dupli... X-Git-Tag: REL_3_15_BETA3~174 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5c43bcf4363ea1f0497dcc70963543fa4137a0b9;p=poi.git findbugs: replace switch statement with fall-throughs with if statement, remove duplicate code git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751738 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/util/HexRead.java b/src/java/org/apache/poi/util/HexRead.java index 8e5ee7f939..165ad03135 100644 --- a/src/java/org/apache/poi/util/HexRead.java +++ b/src/java/org/apache/poi/util/HexRead.java @@ -108,58 +108,35 @@ public class HexRead int characterCount = 0; byte b = (byte) 0; List bytes = new ArrayList(); - boolean done = false; - while ( !done ) + final char a = 'a' - 10; + final char A = 'A' - 10; + while ( true ) { int count = stream.read(); - char baseChar = 'a'; - if ( count == eofChar ) break; - switch ( count ) - { - case '#': - readToEOL( stream ); - break; - case '0': case '1': case '2': case '3': case '4': case '5': - case '6': case '7': case '8': case '9': - b <<= 4; - b += (byte) ( count - '0' ); - characterCount++; - if ( characterCount == 2 ) - { - bytes.add( Byte.valueOf( b ) ); - characterCount = 0; - b = (byte) 0; - } - break; - case 'A': - case 'B': - case 'C': - case 'D': - case 'E': - case 'F': - baseChar = 'A'; - // fall through - case 'a': - case 'b': - case 'c': - case 'd': - case 'e': - case 'f': - b <<= 4; - b += (byte) ( count + 10 - baseChar ); - characterCount++; - if ( characterCount == 2 ) - { - bytes.add( Byte.valueOf( b ) ); - characterCount = 0; - b = (byte) 0; - } - break; - case -1: - done = true; - break; - default : - break; + 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; + } } } Byte[] polished = bytes.toArray( new Byte[0] );