From: Dennis Schafroth Date: Mon, 9 Jan 2012 16:20:14 +0000 (+0100) Subject: Move the reading of the leader into hasNext. Otherwise next() might fail. Considerin... X-Git-Tag: v2.5~23^2~17 X-Git-Url: http://sru.miketaylor.org.uk/cgi-bin?a=commitdiff_plain;h=0ccea6c968363d3d1936bbc24687382b1804c5bc;p=marc4j.git Move the reading of the leader into hasNext. Otherwise next() might fail. Considering alternative... --- diff --git a/src/org/marc4j/MarcStreamReader.java b/src/org/marc4j/MarcStreamReader.java index 9b6f637..3abfc1e 100644 --- a/src/org/marc4j/MarcStreamReader.java +++ b/src/org/marc4j/MarcStreamReader.java @@ -85,6 +85,7 @@ public class MarcStreamReader implements MarcReader { private boolean setBadIndicators = true; + byte[] leaderBuffer; /** * Constructs an instance with the specified input stream. */ @@ -108,8 +109,18 @@ public class MarcStreamReader implements MarcReader { * Returns true if the iteration has more records, false otherwise. */ public boolean hasNext() { - try { - if (input.available() == 0) + int available; + try { + available = input.available(); + try { + leaderBuffer = new byte[24]; + input.readFully(leaderBuffer); + } catch (EOFException eof) { + // If we are not capable of reading the leader before EOF, we cannot read a record + // This happens when we read gzipped marc files, that it returns available bytes, but none is present + available = 0; + } + if (available == 0) return false; } catch (IOException e) { throw new MarcException(e.getMessage(), e); @@ -127,14 +138,10 @@ public class MarcStreamReader implements MarcReader { record = factory.newRecord(); try { - - byte[] byteArray = new byte[24]; - input.readFully(byteArray); - - int recordLength = parseRecordLength(byteArray); + int recordLength = parseRecordLength(leaderBuffer); byte[] recordBuf = new byte[recordLength - 24]; input.readFully(recordBuf); - parseRecord(record, byteArray, recordBuf, recordLength); + parseRecord(record, leaderBuffer, recordBuf, recordLength); return(record); } catch (EOFException e) {