public void position( long newPosition ) { newPosition = Math.min( newPosition, length() ); if ( newPosition == length() ) { final ByteBuffer buffer = byteBuffer( curr = n - 1 ); buffer.position( buffer.capacity() ); return; } curr = (int)( newPosition >>> CHUNK_SHIFT ); byteBuffer( curr ).position( (int)( newPosition - ( (long)curr << CHUNK_SHIFT ) ) ); }
/** Creates a new read-only byte-buffer input stream by mapping a given file channel. * * @param fileChannel the file channel that will be mapped. * @return a new read-only byte-buffer input stream over the contents of <code>fileChannel</code>. */ public static ByteBufferInputStream map( final FileChannel fileChannel ) throws IOException { return map( fileChannel, MapMode.READ_ONLY ); }
public long getOffset() throws IOException { if (closed) { return 0; } return bbis.position(); }
public int read( final byte[] b, final int offset, final int length ) { if ( length == 0 ) return 0; final long remaining = remaining(); if ( remaining == 0 ) return -1; final int realLength = (int)Math.min( remaining, length ); int read = 0; while( read < realLength ) { int rem = byteBuffer( curr ).remaining(); if ( rem == 0 ) byteBuffer( ++curr ).position( 0 ); byteBuffer[ curr ].get( b, offset + read, Math.min( realLength - read, rem ) ); read += Math.min( realLength, rem ); } return realLength; }
private long remaining() { return curr == n - 1 ? byteBuffer( curr ).remaining() : byteBuffer( curr ).remaining() + ( (long)( n - 2 - curr ) << CHUNK_SHIFT ) + lastBufferCapacity; }
/** Creates a new byte-buffer input stream by mapping a given file channel. * * @param fileChannel the file channel that will be mapped. * @param mapMode this must be {@link MapMode#READ_ONLY}. * @return a new byte-buffer input stream over the contents of <code>fileChannel</code>. */ public static ByteBufferInputStream map( final FileChannel fileChannel, final MapMode mapMode ) throws IOException { final long size = fileChannel.size(); final int chunks = (int)( ( size + ( CHUNK_SIZE - 1 ) ) / CHUNK_SIZE ); final ByteBuffer[] byteBuffer = new ByteBuffer[ chunks ]; for( int i = 0; i < chunks; i++ ) byteBuffer[ i ] = fileChannel.map( mapMode, i * CHUNK_SIZE, Math.min( CHUNK_SIZE, size - i * CHUNK_SIZE ) ); byteBuffer[ 0 ].position( 0 ); final boolean[] readyToUse = new boolean[ chunks ]; //BooleanArrays.fill( readyToUse, true ); for (int i = 0; i < readyToUse.length; i++) { readyToUse[i] = true; } return new ByteBufferInputStream( byteBuffer, size, 0, readyToUse ); }
public int available() { final long available = remaining(); return available <= Integer.MAX_VALUE ? (int)available : Integer.MAX_VALUE; }
@Override public long getSize() throws IOException { return bbis.length(); }
public long getOffset() throws IOException { if (closed) { return 0; } return bbis.position(); }
public int read( final byte[] b, final int offset, final int length ) { if ( length == 0 ) return 0; final long remaining = remaining(); if ( remaining == 0 ) return -1; final int realLength = (int)Math.min( remaining, length ); int read = 0; while( read < realLength ) { int rem = byteBuffer( curr ).remaining(); if ( rem == 0 ) byteBuffer( ++curr ).position( 0 ); byteBuffer[ curr ].get( b, offset + read, Math.min( realLength - read, rem ) ); read += Math.min( realLength, rem ); } return realLength; }
private long remaining() { return curr == n - 1 ? byteBuffer( curr ).remaining() : byteBuffer( curr ).remaining() + ( (long)( n - 2 - curr ) << CHUNK_SHIFT ) + lastBufferCapacity; }
public ByteBufferInputStream copy() { return new ByteBufferInputStream( byteBuffer.clone(), size, curr, new boolean[ n ] ); } }
public int available() { final long available = remaining(); return available <= Integer.MAX_VALUE ? (int)available : Integer.MAX_VALUE; }
@Override public long getSize() throws IOException { return bbis.length(); }
public void position( long newPosition ) { newPosition = Math.min( newPosition, length() ); if ( newPosition == length() ) { final ByteBuffer buffer = byteBuffer( curr = n - 1 ); buffer.position( buffer.capacity() ); return; } curr = (int)( newPosition >>> CHUNK_SHIFT ); byteBuffer( curr ).position( (int)( newPosition - ( (long)curr << CHUNK_SHIFT ) ) ); }
public long getOffset() throws IOException { if (closed) { return 0; } return bbis.position(); }
public int read( final byte[] b, final int offset, final int length ) { if ( length == 0 ) return 0; final long remaining = remaining(); if ( remaining == 0 ) return -1; final int realLength = (int)Math.min( remaining, length ); int read = 0; while( read < realLength ) { int rem = byteBuffer( curr ).remaining(); if ( rem == 0 ) byteBuffer( ++curr ).position( 0 ); byteBuffer[ curr ].get( b, offset + read, Math.min( realLength - read, rem ) ); read += Math.min( realLength, rem ); } return realLength; }
@Override public int read() { if ( ! byteBuffer( curr ).hasRemaining() ) { if ( curr < n - 1 ) byteBuffer( ++curr ).position( 0 ); else return -1; } return byteBuffer[ curr ].get() & 0xFF; }