public static Iterator < Byte > iterator ( final DataInputStream self ) { return new Iterator < Byte > ( ) { Byte nextVal ; boolean nextMustRead = true ; boolean hasNext = true ; public boolean hasNext ( ) { if ( nextMustRead && hasNext ) { try { nextVal = self . readByte ( ) ; nextMustRead = false ; } catch ( IOException e ) { hasNext = false ; } } return hasNext ; } public Byte next ( ) { Byte retval = null ; if ( nextMustRead ) { try { retval = self . readByte ( ) ; } catch ( IOException e ) { hasNext = false ; } } else retval = nextVal ; nextMustRead = true ; return retval ; } public void remove ( ) { throw new UnsupportedOperationException ( "Cannot remove() from a DataInputStream Iterator" ) ; } } ; }
Standard iterator for a data input stream which iterates through the stream content a Byte at a time .