001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.util;
019:
020: import java.io.*;
021: import java.util.List;
022: import java.util.ArrayList;
023:
024: /**
025: * Utilities to read hex from files.
026: *
027: * @author Marc Johnson
028: * @author Glen Stampoultzis (glens at apache.org)
029: */
030: public class HexRead {
031: /**
032: * This method reads hex data from a filename and returns a byte array.
033: * The file may contain line comments that are preceeded with a # symbol.
034: *
035: * @param filename The filename to read
036: * @return The bytes read from the file.
037: * @throws IOException If there was a problem while reading the file.
038: */
039: public static byte[] readData(String filename) throws IOException {
040: File file = new File(filename);
041: FileInputStream stream = new FileInputStream(file);
042: try {
043: return readData(stream, -1);
044: } finally {
045: stream.close();
046: }
047: }
048:
049: /**
050: * Same as readData(String) except that this method allows you to specify sections within
051: * a file. Sections are referenced using section headers in the form:
052: * <pre>
053: * [sectioname]
054: * </pre>
055: *
056: * @see #readData(String)
057: */
058: public static byte[] readData(String filename, String section)
059: throws IOException {
060: File file = new File(filename);
061: FileInputStream stream = new FileInputStream(file);
062: try {
063: StringBuffer sectionText = new StringBuffer();
064: boolean inSection = false;
065: int c = stream.read();
066: while (c != -1) {
067: switch (c) {
068: case '[':
069: inSection = true;
070: break;
071: case '\n':
072: case '\r':
073: inSection = false;
074: sectionText = new StringBuffer();
075: break;
076: case ']':
077: inSection = false;
078: if (sectionText.toString().equals(section))
079: return readData(stream, '[');
080: sectionText = new StringBuffer();
081: break;
082: default:
083: if (inSection)
084: sectionText.append((char) c);
085: }
086: c = stream.read();
087: }
088: } finally {
089: stream.close();
090: }
091: throw new IOException("Section '" + section + "' not found");
092: }
093:
094: static public byte[] readData(InputStream stream, int eofChar)
095: throws IOException {
096: int characterCount = 0;
097: byte b = (byte) 0;
098: List bytes = new ArrayList();
099: boolean done = false;
100: while (!done) {
101: int count = stream.read();
102: char baseChar = 'a';
103: if (count == eofChar)
104: break;
105: switch (count) {
106: case '#':
107: readToEOL(stream);
108: break;
109: case '0':
110: case '1':
111: case '2':
112: case '3':
113: case '4':
114: case '5':
115: case '6':
116: case '7':
117: case '8':
118: case '9':
119: b <<= 4;
120: b += (byte) (count - '0');
121: characterCount++;
122: if (characterCount == 2) {
123: bytes.add(new Byte(b));
124: characterCount = 0;
125: b = (byte) 0;
126: }
127: break;
128: case 'A':
129: case 'B':
130: case 'C':
131: case 'D':
132: case 'E':
133: case 'F':
134: baseChar = 'A';
135: case 'a':
136: case 'b':
137: case 'c':
138: case 'd':
139: case 'e':
140: case 'f':
141: b <<= 4;
142: b += (byte) (count + 10 - baseChar);
143: characterCount++;
144: if (characterCount == 2) {
145: bytes.add(new Byte(b));
146: characterCount = 0;
147: b = (byte) 0;
148: }
149: break;
150: case -1:
151: done = true;
152: break;
153: default:
154: break;
155: }
156: }
157: Byte[] polished = (Byte[]) bytes.toArray(new Byte[0]);
158: byte[] rval = new byte[polished.length];
159: for (int j = 0; j < polished.length; j++) {
160: rval[j] = polished[j].byteValue();
161: }
162: return rval;
163: }
164:
165: static public byte[] readFromString(String data) throws IOException {
166: return readData(new ByteArrayInputStream(data.getBytes()), -1);
167: }
168:
169: static private void readToEOL(InputStream stream)
170: throws IOException {
171: int c = stream.read();
172: while (c != -1 && c != '\n' && c != '\r') {
173: c = stream.read();
174: }
175: }
176: }
|