public SF readStructuredField ( ) throws IOException { int buf = 0 ; long thisOffset = offset ; if ( leadingLengthBytes == - 1 ) { // we haven't tested for mvs download leading length bytes // we don't need them and don't want to see them int leadingLength = 0 ; do { buf = read ( ) ; offset ++ ; leadingLength ++ ; } while ( ( buf & 0xff ) != 0x5a && buf != - 1 && leadingLength < 5 ) ; if ( ( buf & 0xff ) != 0x5a ) { has5a = false ; leadingLength = 1 ; // so try if byte 3 is 0xd3 -> so this would be an afp without 5a magic byte offset = 2 ; buf = read ( ) ; if ( buf == - 1 ) return null ; if ( ( buf & 0xff ) != 0xd3 ) { throw new IOException ( "cannot find 5a magic byte nor d3 -> this is no AFP" ) ; } offset = 0 ; } leadingLengthBytes = leadingLength - 1 ; // if(buf == -1 && leadingLength > 1) // throw new IOException("found trailing garbage at the end of file."); } else { if ( leadingLengthBytes > 0 ) read ( data , 0 , leadingLengthBytes ) ; // just throw away those if ( has5a ) { buf = read ( ) ; offset ++ ; } } if ( buf == - 1 ) { return null ; } if ( has5a && ( buf & 0xff ) != 0x5a ) { throw new IOException ( "cannot find 5a magic byte" ) ; } data [ 0 ] = 0x5a ; // (byte) (buf & 0xff); buf = read ( ) ; offset ++ ; if ( buf == - 1 && ! has5a ) { return null ; } if ( buf == - 1 ) { throw new IOException ( "premature end of file." ) ; } data [ 1 ] = ( byte ) ( buf & 0xff ) ; length = ( byte ) buf << 8 ; buf = read ( ) ; offset ++ ; if ( buf == - 1 ) throw new IOException ( "premature end of file." ) ; data [ 2 ] = ( byte ) ( buf & 0xff ) ; length |= ( byte ) buf & 0xff ; length -= 2 ; if ( length > data . length ) throw new IOException ( "length of structured field is too large: " + length ) ; int read = read ( data , 3 , length ) ; offset += read ; if ( read < length ) throw new IOException ( "premature end of file." ) ; SF sf = factory . sf ( data , 0 , getLength ( ) + 2 ) ; sf . setLength ( length + 3 ) ; sf . setOffset ( thisOffset ) ; sf . setNumber ( number ++ ) ; return sf ; }
Reads a new structured field from the input stream . This method is not thread - safe!