FileDocCategorySizeDatePackage
HexRead.javaAPI DocApache Poi 3.0.16010Mon Jan 01 12:39:42 GMT 2007org.apache.poi.util

HexRead

public class HexRead extends Object
Utilities to read hex from files.
author
Marc Johnson
author
Glen Stampoultzis (glens at apache.org)

Fields Summary
Constructors Summary
Methods Summary
public static byte[]readData(java.lang.String filename)
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.

        File file = new File( filename );
        FileInputStream stream = new FileInputStream( file );
        try
        {
            return readData( stream, -1 );
        }
        finally
        {
            stream.close();
        }
    
public static byte[]readData(java.lang.String filename, java.lang.String section)
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)

        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" );
    
public static byte[]readData(java.io.InputStream stream, int eofChar)

        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;
    
public static byte[]readFromString(java.lang.String data)

        return readData(new ByteArrayInputStream( data.getBytes() ), -1);
    
private static voidreadToEOL(java.io.InputStream stream)

        int c = stream.read();
        while ( c != -1 && c != '\n" && c != '\r" )
        {
            c = stream.read();
        }