@ -45,6 +45,7 @@ public class ChunkIterator extends LookAheadIterator<byte[]> implements Iterator
* a ChunkIterator uses a BufferedInputStream to iterate through the file
* and is therefore a fast option to get all elements in the file as a sequence
* ATTENTION : before calling this class ensure that all file buffers are flushed
* ATTENTION : if the iterator is not read to the end or interrupted , close ( ) must be called to release the InputStream
* @param file : the file
* @param recordsize : the size of the elements in the file
* @param chunksize : the size of the chunks that are returned by next ( ) . remaining bytes until the lenght of recordsize are skipped
@ -64,6 +65,15 @@ public class ChunkIterator extends LookAheadIterator<byte[]> implements Iterator
this . stream = new DataInputStream ( new BufferedInputStream ( new FileInputStream ( file ) , 64 * 1024 ) ) ;
}
/ * *
* Special close methode to release the used InputStream
* stream is automatically closed on last next ( ) ,
* close ( ) needs only be called if iterator not read to the end ( hasNext ( ) or next ( ) has not returned null )
* /
public void close ( ) throws IOException {
this . stream . close ( ) ;
}
@Override
public byte [ ] next0 ( ) {
final byte [ ] chunk = new byte [ chunksize ] ;
@ -82,6 +92,11 @@ public class ChunkIterator extends LookAheadIterator<byte[]> implements Iterator
return chunk ;
} catch ( final EOFException e ) {
// no real exception, this is the normal termination
try {
this . stream . close ( ) ; // close the underlaying inputstream
} catch ( IOException ex ) {
ConcurrentLog . logException ( ex ) ;
}
return null ;
} catch ( final IOException e ) {
ConcurrentLog . logException ( e ) ;