1 package org.apache.poi.util; 2 3 import java.io.IOException; 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.InputStream; 7 import java.util.List; 8 import java.util.ArrayList; 9 10 public class HexRead 11 { 12 public static byte[] readTestData( String filename ) 13 throws IOException 14 { 15 File file = new File( filename ); 16 FileInputStream stream = new FileInputStream( file ); 17 int characterCount = 0; 18 byte b = (byte) 0; 19 List bytes = new ArrayList(); 20 boolean done = false; 21 22 while ( !done ) 23 { 24 int count = stream.read(); 25 26 switch ( count ) 27 { 28 29 case '#': 30 readToEOL(stream); 31 break; 32 case '0': 33 case '1': 34 case '2': 35 case '3': 36 case '4': 37 case '5': 38 case '6': 39 case '7': 40 case '8': 41 case '9': 42 b <<= 4; 43 b += (byte) ( count - '0' ); 44 characterCount++; 45 if ( characterCount == 2 ) 46 { 47 bytes.add( new Byte( b ) ); 48 characterCount = 0; 49 b = (byte) 0; 50 } 51 break; 52 53 case 'A': 54 case 'B': 55 case 'C': 56 case 'D': 57 case 'E': 58 case 'F': 59 b <<= 4; 60 b += (byte) ( count + 10 - 'A' ); 61 characterCount++; 62 if ( characterCount == 2 ) 63 { 64 bytes.add( new Byte( b ) ); 65 characterCount = 0; 66 b = (byte) 0; 67 } 68 break; 69 70 case 'a': 71 case 'b': 72 case 'c': 73 case 'd': 74 case 'e': 75 case 'f': 76 b <<= 4; 77 b += (byte) ( count + 10 - 'a' ); 78 characterCount++; 79 if ( characterCount == 2 ) 80 { 81 bytes.add( new Byte( b ) ); 82 characterCount = 0; 83 b = (byte) 0; 84 } 85 break; 86 87 case -1: 88 done = true; 89 break; 90 91 default : 92 break; 93 } 94 } 95 stream.close(); 96 Byte[] polished = (Byte[]) bytes.toArray( new Byte[0] ); 97 byte[] rval = new byte[polished.length]; 98 99 for ( int j = 0; j < polished.length; j++ ) 100 { 101 rval[j] = polished[j].byteValue(); 102 } 103 return rval; 104 } 105 106 static private void readToEOL( InputStream stream ) throws IOException 107 { 108 int c = stream.read(); 109 while ( c != -1 && c != '\n' && c != '\r') 110 { 111 c = stream.read(); 112 } 113 } 114 115 116 } 117