Index: lucene/src/java/org/apache/lucene/store/MMapDirectory.java =================================================================== --- lucene/src/java/org/apache/lucene/store/MMapDirectory.java (revision 1050282) +++ lucene/src/java/org/apache/lucene/store/MMapDirectory.java (working copy) @@ -242,8 +242,35 @@ throw new IOException("read past EOF"); } } + + @Override + public short readShort() throws IOException { + try { + return buffer.getShort(); + } catch (BufferUnderflowException e) { + throw new IOException("read past EOF"); + } + } @Override + public int readInt() throws IOException { + try { + return buffer.getInt(); + } catch (BufferUnderflowException e) { + throw new IOException("read past EOF"); + } + } + + @Override + public long readLong() throws IOException { + try { + return buffer.getLong(); + } catch (BufferUnderflowException e) { + throw new IOException("read past EOF"); + } + } + + @Override public long getFilePointer() { return buffer.position(); } @@ -294,7 +321,6 @@ private final int maxBufSize; private ByteBuffer curBuf; // redundant for speed: buffers[curBufIndex] - private int curAvail; // redundant for speed: (bufSizes[curBufIndex] - curBuf.position()) private boolean isClone = false; @@ -333,38 +359,67 @@ @Override public byte readByte() throws IOException { - // Performance might be improved by reading ahead into an array of - // e.g. 128 bytes and readByte() from there. - if (curAvail == 0) { + try { + return curBuf.get(); + } catch (BufferUnderflowException e) { curBufIndex++; if (curBufIndex >= buffers.length) throw new IOException("read past EOF"); curBuf = buffers[curBufIndex]; curBuf.position(0); - curAvail = bufSizes[curBufIndex]; + return curBuf.get(); } - curAvail--; - return curBuf.get(); } @Override public void readBytes(byte[] b, int offset, int len) throws IOException { - while (len > curAvail) { - curBuf.get(b, offset, curAvail); - len -= curAvail; - offset += curAvail; - curBufIndex++; - if (curBufIndex >= buffers.length) - throw new IOException("read past EOF"); - curBuf = buffers[curBufIndex]; - curBuf.position(0); - curAvail = bufSizes[curBufIndex]; + try { + curBuf.get(b, offset, len); + } catch (BufferUnderflowException e) { + int curAvail = curBuf.remaining(); + while (len > curAvail) { + curBuf.get(b, offset, curAvail); + len -= curAvail; + offset += curAvail; + curBufIndex++; + if (curBufIndex >= buffers.length) + throw new IOException("read past EOF"); + curBuf = buffers[curBufIndex]; + curBuf.position(0); + curAvail = curBuf.remaining(); + } + curBuf.get(b, offset, len); } - curBuf.get(b, offset, len); - curAvail -= len; } @Override + public short readShort() throws IOException { + try { + return curBuf.getShort(); + } catch (BufferUnderflowException e) { + return super.readShort(); + } + } + + @Override + public int readInt() throws IOException { + try { + return curBuf.getInt(); + } catch (BufferUnderflowException e) { + return super.readInt(); + } + } + + @Override + public long readLong() throws IOException { + try { + return curBuf.getLong(); + } catch (BufferUnderflowException e) { + return super.readLong(); + } + } + + @Override public long getFilePointer() { return ((long) curBufIndex * maxBufSize) + curBuf.position(); } @@ -375,7 +430,6 @@ curBuf = buffers[curBufIndex]; int bufOffset = (int) (pos - ((long) curBufIndex * maxBufSize)); curBuf.position(bufOffset); - curAvail = bufSizes[curBufIndex] - bufOffset; } @Override