@Override public int write(final ByteBuffer src) throws IOException { return write(src, this.nextExpectedWritePosition); }
@Override public int write(final ByteBuffer src, final long position) throws IOException { if (position != this.nextExpectedWritePosition) { throw new IOException("Next expected write position is " + this.nextExpectedWritePosition); } final FSDataOutputStream outputStream = getOutputStream(); int totalBytesWritten = 0; while (src.hasRemaining()) { final int length = Math.min(this.buf.length, src.remaining()); src.get(this.buf, 0, length); outputStream.write(this.buf, 0, length); totalBytesWritten += length; } this.nextExpectedWritePosition += totalBytesWritten; return totalBytesWritten; }
@Override public int read(final ByteBuffer dst, final long position) throws IOException { final int length = Math.min(this.buf.length, dst.remaining()); final FSDataInputStream inputStream = getInputStream(); if (position != this.nextExpectedReadPosition) { System.out.println("Next expected position is " + this.nextExpectedReadPosition + ", seeking to " + position); inputStream.seek(position); this.nextExpectedReadPosition = position; } final int bytesRead = inputStream.read(this.buf, 0, length); if (bytesRead == -1) { return -1; } dst.put(this.buf, 0, length); this.nextExpectedReadPosition += bytesRead; return bytesRead; }
@Override public int write(final ByteBuffer src, final long position) throws IOException { if (position != this.nextExpectedWritePosition) { throw new IOException("Next expected write position is " + this.nextExpectedWritePosition); } final FSDataOutputStream outputStream = getOutputStream(); int totalBytesWritten = 0; while (src.hasRemaining()) { final int length = Math.min(this.buf.length, src.remaining()); src.get(this.buf, 0, length); outputStream.write(this.buf, 0, length); totalBytesWritten += length; } this.nextExpectedWritePosition += totalBytesWritten; return totalBytesWritten; }
@Override public int read(final ByteBuffer dst, final long position) throws IOException { final int length = Math.min(this.buf.length, dst.remaining()); final FSDataInputStream inputStream = getInputStream(); if (position != this.nextExpectedReadPosition) { System.out.println("Next expected position is " + this.nextExpectedReadPosition + ", seeking to " + position); inputStream.seek(position); this.nextExpectedReadPosition = position; } final int bytesRead = inputStream.read(this.buf, 0, length); if (bytesRead == -1) { return -1; } dst.put(this.buf, 0, length); this.nextExpectedReadPosition += bytesRead; return bytesRead; }
@Override public int write(final ByteBuffer src) throws IOException { return write(src, this.nextExpectedWritePosition); }