@Override public final void verify(final ByteBuffer inBuff) throws DataCorruptionException { final ByteBuffer ringBuff = dataInput.getLayer(layerNum).asReadOnlyBuffer(); ringBuff.position((int) ((offset + position) % dataInputSize)); verify(inBuff, ringBuff); }
@Override public final int read(final ByteBuffer dst) { final int n; final MappedByteBuffer ringBuff = (MappedByteBuffer) dataInput .getLayer(layerNum) .asReadOnlyBuffer(); ringBuff.position((int) ((offset + position) % dataInputSize)); // bytes count to transfer n = Math.min(dst.remaining(), ringBuff.remaining()); ringBuff.limit(ringBuff.position() + n); // do the transfer dst.put(ringBuff); position += n; return n; } //
@Override public final int write(final ByteBuffer src) throws DataCorruptionException, DataSizeException { if(src == null) { return 0; } int m; final MappedByteBuffer ringBuff = (MappedByteBuffer) dataInput .getLayer(layerNum) .asReadOnlyBuffer(); ringBuff.position((int) ((offset + position) % dataInputSize)); final int n = Math.min(src.remaining(), ringBuff.remaining()); if(n > 0) { byte bs, bi; for(m = 0; m < n; m ++) { bs = ringBuff.get(); bi = src.get(); if(bs != bi) { throw new DataCorruptionException(m, bs, bi); } } position += n; } else { return n; } return m; }
@Override public final long writeToFileChannel(final FileChannel chanDst, final long maxCount) throws IOException { final MappedByteBuffer ringBuff = (MappedByteBuffer) dataInput .getLayer(layerNum) .asReadOnlyBuffer(); int n = (int) ((offset + position) % dataInputSize); ringBuff.position(n); n = (int) Math.min(maxCount, ringBuff.remaining()); ringBuff.limit(ringBuff.position() + n); n = chanDst.write(ringBuff); position += n; return n; }
@Override public final long writeToSocketChannel(final WritableByteChannel chanDst, final long maxCount) throws IOException { final MappedByteBuffer ringBuff = (MappedByteBuffer) dataInput .getLayer(layerNum) .asReadOnlyBuffer(); long doneCount = 0; int n, m; // spin while not done either destination channel consumes all the data while(doneCount < maxCount) { ringBuff.position((int) ((offset + position) % dataInputSize)); n = (int) Math.min(maxCount - doneCount, ringBuff.remaining()); ringBuff.limit(ringBuff.position() + n); m = chanDst.write(ringBuff); doneCount += m; position += m; if(m < n) { break; } } return doneCount; }