1 2 17 18 19 package org.apache.poi.util; 20 21 import java.io.*; 22 import java.util.List ; 23 import java.util.ArrayList ; 24 25 31 public class HexRead 32 { 33 41 public static byte[] readData( String filename ) throws IOException 42 { 43 File file = new File( filename ); 44 FileInputStream stream = new FileInputStream( file ); 45 try 46 { 47 return readData( stream, -1 ); 48 } 49 finally 50 { 51 stream.close(); 52 } 53 } 54 55 64 public static byte[] readData( String filename, String section ) throws IOException 65 { 66 File file = new File( filename ); 67 FileInputStream stream = new FileInputStream( file ); 68 try 69 { 70 StringBuffer sectionText = new StringBuffer (); 71 boolean inSection = false; 72 int c = stream.read(); 73 while ( c != -1 ) 74 { 75 switch ( c ) 76 { 77 case '[': 78 inSection = true; 79 break; 80 case '\n': 81 case '\r': 82 inSection = false; 83 sectionText = new StringBuffer (); 84 break; 85 case ']': 86 inSection = false; 87 if ( sectionText.toString().equals( section ) ) return readData( stream, '[' ); 88 sectionText = new StringBuffer (); 89 break; 90 default: 91 if ( inSection ) sectionText.append( (char) c ); 92 } 93 c = stream.read(); 94 } 95 } 96 finally 97 { 98 stream.close(); 99 } 100 throw new IOException( "Section '" + section + "' not found" ); 101 } 102 103 static public byte[] readData( InputStream stream, int eofChar ) 104 throws IOException 105 { 106 int characterCount = 0; 107 byte b = (byte) 0; 108 List bytes = new ArrayList (); 109 boolean done = false; 110 while ( !done ) 111 { 112 int count = stream.read(); 113 char baseChar = 'a'; 114 if ( count == eofChar ) break; 115 switch ( count ) 116 { 117 case '#': 118 readToEOL( stream ); 119 break; 120 case '0': case '1': case '2': case '3': case '4': case '5': 121 case '6': case '7': case '8': case '9': 122 b <<= 4; 123 b += (byte) ( count - '0' ); 124 characterCount++; 125 if ( characterCount == 2 ) 126 { 127 bytes.add( new Byte ( b ) ); 128 characterCount = 0; 129 b = (byte) 0; 130 } 131 break; 132 case 'A': 133 case 'B': 134 case 'C': 135 case 'D': 136 case 'E': 137 case 'F': 138 baseChar = 'A'; 139 case 'a': 140 case 'b': 141 case 'c': 142 case 'd': 143 case 'e': 144 case 'f': 145 b <<= 4; 146 b += (byte) ( count + 10 - baseChar ); 147 characterCount++; 148 if ( characterCount == 2 ) 149 { 150 bytes.add( new Byte ( b ) ); 151 characterCount = 0; 152 b = (byte) 0; 153 } 154 break; 155 case -1: 156 done = true; 157 break; 158 default : 159 break; 160 } 161 } 162 Byte [] polished = (Byte []) bytes.toArray( new Byte [0] ); 163 byte[] rval = new byte[polished.length]; 164 for ( int j = 0; j < polished.length; j++ ) 165 { 166 rval[j] = polished[j].byteValue(); 167 } 168 return rval; 169 } 170 171 static public byte[] readFromString(String data) throws IOException 172 { 173 return readData(new ByteArrayInputStream( data.getBytes() ), -1); 174 } 175 176 static private void readToEOL( InputStream stream ) throws IOException 177 { 178 int c = stream.read(); 179 while ( c != -1 && c != '\n' && c != '\r' ) 180 { 181 c = stream.read(); 182 } 183 } 184 } 185 | Popular Tags |