KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > util > HexRead


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.util;
20
21 import java.io.*;
22 import java.util.List JavaDoc;
23 import java.util.ArrayList JavaDoc;
24
25 /**
26  * Utilities to read hex from files.
27  *
28  * @author Marc Johnson
29  * @author Glen Stampoultzis (glens at apache.org)
30  */

31 public class HexRead
32 {
33     /**
34      * This method reads hex data from a filename and returns a byte array.
35      * The file may contain line comments that are preceeded with a # symbol.
36      *
37      * @param filename The filename to read
38      * @return The bytes read from the file.
39      * @throws IOException If there was a problem while reading the file.
40      */

41     public static byte[] readData( String JavaDoc 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     /**
56      * Same as readData(String) except that this method allows you to specify sections within
57      * a file. Sections are referenced using section headers in the form:
58      * <pre>
59      * [sectioname]
60      * </pre>
61      *
62      * @see #readData(String)
63      */

64     public static byte[] readData( String JavaDoc filename, String JavaDoc section ) throws IOException
65     {
66         File file = new File( filename );
67         FileInputStream stream = new FileInputStream( file );
68         try
69         {
70             StringBuffer JavaDoc sectionText = new StringBuffer JavaDoc();
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 JavaDoc();
84                         break;
85                     case ']':
86                         inSection = false;
87                         if ( sectionText.toString().equals( section ) ) return readData( stream, '[' );
88                         sectionText = new StringBuffer JavaDoc();
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 JavaDoc bytes = new ArrayList JavaDoc();
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 JavaDoc( 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 JavaDoc( 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 JavaDoc[] polished = (Byte JavaDoc[]) bytes.toArray( new Byte JavaDoc[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 JavaDoc 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