diff --git a/source/net/yacy/kelondro/table/ChunkIterator.java b/source/net/yacy/kelondro/table/ChunkIterator.java index 1ddd7ba41..40c223f59 100644 --- a/source/net/yacy/kelondro/table/ChunkIterator.java +++ b/source/net/yacy/kelondro/table/ChunkIterator.java @@ -7,7 +7,7 @@ // $LastChangedBy$ // // LICENSE -// +// // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or @@ -39,22 +39,23 @@ import net.yacy.cora.util.LookAheadIterator; public class ChunkIterator extends LookAheadIterator implements Iterator { private final int chunksize; - + /** * create a ChunkIterator * 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 - * @throws FileNotFoundException + * @throws FileNotFoundException */ - - + + private final DataInputStream stream; private final int recordsize; - + public ChunkIterator(final File file, final int recordsize, final int chunksize) throws FileNotFoundException { if (!file.exists()) throw new FileNotFoundException(file.getAbsolutePath()); assert (file.exists()); @@ -63,7 +64,16 @@ public class ChunkIterator extends LookAheadIterator implements Iterator this.chunksize = chunksize; 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 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); diff --git a/source/net/yacy/kelondro/table/Table.java b/source/net/yacy/kelondro/table/Table.java index 2b652ad87..3c81c36cd 100644 --- a/source/net/yacy/kelondro/table/Table.java +++ b/source/net/yacy/kelondro/table/Table.java @@ -173,7 +173,7 @@ public class Table implements Index, Iterable { } else { byte[] record; key = new byte[rowdef.primaryKeyLength]; - final Iterator ri = new ChunkIterator(tablefile, rowdef.objectsize, rowdef.objectsize); + final ChunkIterator ri = new ChunkIterator(tablefile, rowdef.objectsize, rowdef.objectsize); while (ri.hasNext()) { record = ri.next(); assert record != null; @@ -188,6 +188,7 @@ public class Table implements Index, Iterable { this.table.addUnique(this.taildef.newEntry(record, rowdef.primaryKeyLength, true)); } catch (final SpaceExceededException e) { this.table = null; + ri.close(); // close inputstream of chunkiterator break; } } else {