From: Glen Stampoultzis Date: Sat, 28 Jun 2003 05:43:09 +0000 (+0000) Subject: Fixing the line endings and problem make readData public. X-Git-Tag: REL_2_5_1~119 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=678cd63b008c7452e62ee78b552a3a2e5493179c;p=poi.git Fixing the line endings and problem make readData public. git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/branches/REL_2_BRANCH@353172 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 0be7930a7b..3e0485c5aa 100644 --- a/src/java/org/apache/poi/util/HexRead.java +++ b/src/java/org/apache/poi/util/HexRead.java @@ -1 +1,223 @@ -/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache POI" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * "Apache POI", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . */package org.apache.poi.util;import java.io.IOException;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.util.List;import java.util.ArrayList;/** * Utilities to read hex from files. * * @author Marc Johnson * @author Glen Stampoultzis (glens at apache.org) */public class HexRead{ /** * This method reads hex data from a filename and returns a byte array. * The file may contain line comments that are preceeded with a # symbol. * * @param filename The filename to read * @return The bytes read from the file. * @throws IOException If there was a problem while reading the file. */ public static byte[] readData( String filename ) throws IOException { File file = new File( filename ); FileInputStream stream = new FileInputStream( file ); try { return readData(stream, -1); } finally { stream.close(); } } /** * Same as readData(String) except that this method allows you to specify sections within * a file. Sections are referenced using section headers in the form: *
     *  [sectioname]     * 
* * @see #readData(String) */ public static byte[] readData(String filename, String section) throws IOException { File file = new File( filename ); FileInputStream stream = new FileInputStream( file ); try { StringBuffer sectionText = new StringBuffer(); boolean inSection = false; int c = stream.read(); while (c != -1) { switch(c) { case '[': inSection = true; break; case '\n': case '\r': inSection = false; sectionText = new StringBuffer(); break; case ']': inSection = false; if (sectionText.toString().equals(section)) return readData(stream, '['); sectionText = new StringBuffer(); break; default: if (inSection) sectionText.append((char)c); } c = stream.read(); } } finally { stream.close(); } throw new IOException("Section '" + section + "' not found"); } static private byte[] readData( FileInputStream stream, int eofChar ) throws IOException { int characterCount = 0; byte b = (byte) 0; List bytes = new ArrayList(); boolean done = false; while ( !done ) { 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( new Byte( b ) ); characterCount = 0; b = (byte) 0; } break; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': baseChar = 'A'; 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( new Byte( b ) ); characterCount = 0; b = (byte) 0; } break; case -1: done = true; break; default : break; } } Byte[] polished = (Byte[]) bytes.toArray( new Byte[0] ); byte[] rval = new byte[polished.length]; for ( int j = 0; j < polished.length; j++ ) { rval[j] = polished[j].byteValue(); } return rval; } static private void readToEOL( InputStream stream ) throws IOException { int c = stream.read(); while ( c != -1 && c != '\n' && c != '\r') { c = stream.read(); } }} \ No newline at end of file +/* ==================================================================== + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2002 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, + * if any, must include the following acknowledgment: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgment may appear in the software itself, + * if and wherever such third-party acknowledgments normally appear. + * + * 4. The names "Apache" and "Apache Software Foundation" and + * "Apache POI" must not be used to endorse or promote products + * derived from this software without prior written permission. For + * written permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache", + * "Apache POI", nor may "Apache" appear in their name, without + * prior written permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + */ + +package org.apache.poi.util; + +import java.io.IOException; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.List; +import java.util.ArrayList; + +/** + * Utilities to read hex from files. + * + * @author Marc Johnson + * @author Glen Stampoultzis (glens at apache.org) */ +public class HexRead +{ + /** + * This method reads hex data from a filename and returns a byte array. + * The file may contain line comments that are preceeded with a # symbol. + * + * @param filename The filename to read + * @return The bytes read from the file. + * @throws IOException If there was a problem while reading the file. */ + public static byte[] readData( String filename ) throws IOException + { + File file = new File( filename ); + FileInputStream stream = new FileInputStream( file ); + try + { + return readData( stream, -1 ); + } + finally + { + stream.close(); + } + } + + /** + * Same as readData(String) except that this method allows you to specify sections within + * a file. Sections are referenced using section headers in the form: + *
+     *  [sectioname]
+     * 
+ * + * @see #readData(String) + */ + public static byte[] readData( String filename, String section ) throws IOException + { + File file = new File( filename ); + FileInputStream stream = new FileInputStream( file ); + try + { + StringBuffer sectionText = new StringBuffer(); + boolean inSection = false; + int c = stream.read(); + while ( c != -1 ) + { + switch ( c ) + { + case '[': + inSection = true; + break; + case '\n': + case '\r': + inSection = false; + sectionText = new StringBuffer(); + break; + case ']': + inSection = false; + if ( sectionText.toString().equals( section ) ) return readData( stream, '[' ); + sectionText = new StringBuffer(); + break; + default: + if ( inSection ) sectionText.append( (char) c ); + } + c = stream.read(); + } + } + finally + { + stream.close(); + } + throw new IOException( "Section '" + section + "' not found" ); + } + + static public byte[] readData( InputStream stream, int eofChar ) throws IOException + { + int characterCount = 0; + byte b = (byte) 0; + List bytes = new ArrayList(); + boolean done = false; + while ( !done ) + { + 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( new Byte( b ) ); + characterCount = 0; + b = (byte) 0; + } + break; + case 'A': + case 'B': + case 'C': + case 'D': + case 'E': + case 'F': + baseChar = 'A'; + 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( new Byte( b ) ); + characterCount = 0; + b = (byte) 0; + } + break; + case -1: + done = true; + break; + default : + break; + } + } + Byte[] polished = (Byte[]) bytes.toArray( new Byte[0] ); + byte[] rval = new byte[polished.length]; + for ( int j = 0; j < polished.length; j++ ) + { + rval[j] = polished[j].byteValue(); + } + return rval; + } + + static private void readToEOL( InputStream stream ) throws IOException + { + int c = stream.read(); + while ( c != -1 && c != '\n' && c != '\r' ) + { + c = stream.read(); + } + } +} \ No newline at end of file