iso8211 0-1-0 by Weft

uk.co.weft.iso8211
Class ISO8211InputStream

java.lang.Object
  |
  +--java.io.InputStream
        |
        +--java.io.FilterInputStream
              |
              +--java.io.PushbackInputStream
                    |
                    +--uk.co.weft.iso8211.ISO8211InputStream

public class ISO8211InputStream
extends java.io.PushbackInputStream

Lots of stuff in parsing ISO8211 depends on knowing positions and offsets within the stream, but this is generally not easy with Java streams. This is an attempt to make it easier. It may also be a convenient home for things which read raw bitpatterns and interpret them as numbers, for example.

Version:
$Revision: 1.5 $ This revision: $Author: simon $
  $Log: ISO8211InputStream.java,v $
  Revision 1.5  2002/11/04 14:11:15  simon
  Now generating (some) XML. Tidy up for first public release.

  Revision 1.4  2002/11/01 14:46:09  simon
  Much better! Now parsing most fields reasonably well.

  Revision 1.3  2002/10/31 23:04:28  simon
  Not in all truth a lot of progress. I'm parsing the DDR OK but when I come
  to real records I'm still not parsing them, and the reason seems to be that
  I'm not associating the right formats with the right field types. I'm still
  finding this very opaque.

  Revision 1.2  2002/10/31 19:25:22  simon


  This package of work is to take a parser which correctly recognised
  all the record and field boundaries, and try to make it read actual
  data. The DDR is now reading correctly (I believe) but the data are
  not being read, primarily because (I think) I'm not correctly making
  use of the data I read from the DDR.

  Revision 1.1.1.1  2002/10/29 11:27:33  simon
  More or less parses


  
Author:
Simon Brooke (simon@jasmine.org.uk)

Fields inherited from class java.io.PushbackInputStream
buf, pos
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
ISO8211InputStream(java.io.InputStream in)
          constructor.
 
Method Summary
 boolean eof()
          check if this stream is at an end
 long getPosition()
          return the current position in the stream
 long go(long target)
          go to this position in the stream.
 int peek()
          peek at the next byte on the stream but don't remove it.
 int read()
          read the next byte from the stream and increment the counter
 int read(byte[] b, int off, int len)
          read the next few bytes from the stream into this array.
 int readInt()
          read an ASCII formatted integer value from this stream
 java.lang.String readString()
          read a string of data up to but not including a unit terminator.
 java.lang.String readString(int terminator)
          read a string of data up to but not including this terminator character.
 java.lang.String readString(int delimiter, int precision)
          read a string from the stream with either delimiter or precision.
 java.lang.String readStringWithPrecision(int precision)
          read this many characters from the stream and return them as a string
 long skip(long n)
          skip over (discard) this many bytes in the stream.
 void unread(byte[] b)
          push these bytes back onto the stream
 void unread(byte[] b, int off, int len)
          push these bytes back onto the stream
 void unread(int b)
          push this byte back onto the stream
 
Methods inherited from class java.io.PushbackInputStream
available, close, markSupported
 
Methods inherited from class java.io.FilterInputStream
mark, read, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ISO8211InputStream

public ISO8211InputStream(java.io.InputStream in)
constructor. Nothing special
Method Detail

read

public int read()
         throws java.io.IOException
read the next byte from the stream and increment the counter
Overrides:
read in class java.io.PushbackInputStream
Returns:
the byte read
Throws:
java.io.IOException - if something evil this way comes

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
read the next few bytes from the stream into this array.
Overrides:
read in class java.io.PushbackInputStream
Parameters:
b - an array of bytes to read into
off - the start offset in the array to put the bytes in
len - the number of bytes to read
Returns:
the number of bytes read
Throws:
java.io.IOException - is somthing deep and mysterious occurs
UnexpectedEndException - if there aren't len bytes on the stream to read

unread

public void unread(int b)
            throws java.io.IOException
push this byte back onto the stream
Overrides:
unread in class java.io.PushbackInputStream

unread

public void unread(byte[] b)
            throws java.io.IOException
push these bytes back onto the stream
Overrides:
unread in class java.io.PushbackInputStream

unread

public void unread(byte[] b,
                   int off,
                   int len)
            throws java.io.IOException
push these bytes back onto the stream
Overrides:
unread in class java.io.PushbackInputStream

skip

public long skip(long n)
          throws java.io.IOException
skip over (discard) this many bytes in the stream. TODO: should this method throw an UnexpectedEndException if it can't skip the requested number of bytes?
Overrides:
skip in class java.io.PushbackInputStream

peek

public int peek()
         throws java.io.IOException
peek at the next byte on the stream but don't remove it. If the end is encountered return -1, don't throw an exception

eof

public boolean eof()
            throws java.io.IOException
check if this stream is at an end

go

public long go(long target)
        throws java.io.IOException
go to this position in the stream. Similar to fseek, except you can go backward only as far as the pushback buffer allows.
Parameters:
target: - the byte offset to go to counting from the beginning of the stream.
Returns:
the number of bytes actually moved
Throws:
java.io.IOException - if target is upstream or somthing horrid happens in the undergrowth

getPosition

public long getPosition()
return the current position in the stream

readInt

public int readInt()
            throws java.lang.Exception
read an ASCII formatted integer value from this stream

readString

public java.lang.String readString()
                            throws java.io.IOException,
                                   FormatException
read a string of data up to but not including a unit terminator. Do not leave the unit terminator on the stream

readString

public java.lang.String readString(int terminator)
                            throws java.io.IOException,
                                   FormatException
read a string of data up to but not including this terminator character. Do not leave the terminating character on the stream.
Parameters:
terminator - the read() value to terminate at - int 'cause it could be -1

readStringWithPrecision

public java.lang.String readStringWithPrecision(int precision)
                                         throws java.io.IOException
read this many characters from the stream and return them as a string

readString

public java.lang.String readString(int delimiter,
                                   int precision)
                            throws java.io.IOException,
                                   FormatException
read a string from the stream with either delimiter or precision. Note if both delimiter and precision are valid delimiter will be preferred.
Parameters:
delimiter - the delimiter to read up to, or -1 if precision should be used
precision - the number of characters to read, or -1 if the delimiter should be used.
Throws:
LostThePlotException - if neither are valid

iso8211 0-1-0 by Weft