private final void switchCurrentBuffer() throws IOException {
   if (currentBufferIndex == file.numBuffers()) {
     currentBuffer = file.addBuffer(BUFFER_SIZE);
   } else {
     currentBuffer = file.getBuffer(currentBufferIndex);
   }
   bufferPosition = 0;
   bufferStart = (long) BUFFER_SIZE * (long) currentBufferIndex;
   bufferLength = currentBuffer.length;
 }
 /** Copy the current contents of this buffer to the named output. */
 public void writeTo(IndexOutput out) throws IOException {
   flush();
   final long end = file.length;
   long pos = 0;
   int buffer = 0;
   while (pos < end) {
     int length = BUFFER_SIZE;
     long nextPos = pos + length;
     if (nextPos > end) { // at the last buffer
       length = (int) (end - pos);
     }
     out.writeBytes(file.getBuffer(buffer++), length);
     pos = nextPos;
   }
 }
 /** Copy the current contents of this buffer to output byte array */
 public void writeTo(byte[] bytes, int offset) throws IOException {
   flush();
   final long end = file.length;
   long pos = 0;
   int buffer = 0;
   int bytesUpto = offset;
   while (pos < end) {
     int length = BUFFER_SIZE;
     long nextPos = pos + length;
     if (nextPos > end) { // at the last buffer
       length = (int) (end - pos);
     }
     System.arraycopy(file.getBuffer(buffer++), 0, bytes, bytesUpto, length);
     bytesUpto += length;
     pos = nextPos;
   }
 }
 private final void switchCurrentBuffer(boolean enforceEOF) throws IOException {
   bufferStart = (long) BUFFER_SIZE * (long) currentBufferIndex;
   if (currentBufferIndex >= file.numBuffers()) {
     // end of file reached, no more buffers left
     if (enforceEOF) {
       throw new EOFException("Read past EOF (resource: " + this + ")");
     } else {
       // Force EOF if a read takes place at this position
       currentBufferIndex--;
       bufferPosition = BUFFER_SIZE;
     }
   } else {
     currentBuffer = file.getBuffer(currentBufferIndex);
     bufferPosition = 0;
     long buflen = length - bufferStart;
     bufferLength = buflen > BUFFER_SIZE ? BUFFER_SIZE : (int) buflen;
   }
 }