Class BufferOverReadableChannel<A>
java.lang.Object
org.aksw.commons.io.buffer.array.BufferOverReadableChannel<A>
- All Implemented Interfaces:
ArrayReadable<A>, ArrayWritable<A>, BufferLike<A>, HasArrayOps<A>, Buffer<A>, ReadableChannelFactory<A>, ReadableChannelSource<A>, SeekableReadableChannelSource<A>
Implementation of a buffer that supports reading data from a channel.
This implementation can serve as a plain buffer by simply providing a channel that provides no data.
Instances of these class are thread safe, but the obtained channels are not; each channel should only be operated on
by one thread.
This class can be seen as a generalization of BufferedInputStream.
Differences to BufferedInputStream:
- this class caches all data read from the inputstream hence there is no mark / reset mechanism
- buffer is split into buckets (no data copying required when allocating more space)
- data is loaded on demand based on (possibly concurrent) requests to the seekable channels obtained with
newChannel()
Closest known-to-me Hadoop counterpart is BufferedFSInputStream (which is based on BufferedInputStream)
- Author:
- raven
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classprotected classstatic class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected BufferOverReadableChannel.BucketPointerEnd marker with two components (idx, pos) it is wrapped in an object to enable atomic replacement of the reference The pointer is monotonous in the sense that the end marker's logical linear location is only increased Reading an old version while a new one has been set will only cause a read to return on the old boundary, but a subsequent synchronized check for whether loading of additional data is needed is then made anywayprotected A[]The buffered dataprotected ReadableChannel<A> protected booleanprotected longThe number of cached bytes.protected intMaximum number to read from the dataSupplier in one requestprotected int -
Constructor Summary
ConstructorsConstructorDescriptionBufferOverReadableChannel(ArrayOps<A> arrayOps, int initialBucketSize, ReadableChannel<A> dataSupplier, int minReadSize) -
Method Summary
Modifier and TypeMethodDescriptionstatic BufferOverReadableChannel<byte[]> static BufferOverReadableChannel<byte[]> static BufferOverReadableChannel<byte[]> createForBytes(InputStream in, int minReadSize) static BufferOverReadableChannel<byte[]> createForBytes(ReadableChannel<byte[]> channel, int minReadSize) static <T> BufferOverReadableChannel<T[]> createForObjects(int initialCapacity) static <A> voiddebuffer(ReadableChannel<A> channel) Remove the buffer of channel created with newBufferedChannelintdoRead(ArraySink<A> dst, BufferOverReadableChannel.Channel reader) protected voidReturns a view of the buffered data only.longBuffers with 'unlimited' capacity should return Long.MAX_VALUEReturn the data supplier.longstatic <A> BufferOverReadableChannel.BucketPointergetPointer(ArrayOps<A> arrayOps, A[] buckets, BufferOverReadableChannel.BucketPointer end, long pos) static <A> BufferOverReadableChannel.BucketPointergetPointerRel(ArrayOps<A> arrayOps, A[] buckets, BufferOverReadableChannel.BucketPointer end, long startPos, BufferOverReadableChannel.BucketPointer startPtr, long pos) static <A> longgetPosition(ArrayOps<A> arrayOps, A[] buckets, int idx, int pos) booleanIf this method returns true then further reads will not increase the known size unless a new data supplier is set.protected intloadData(int needed, boolean exact) fetch a chunk from the input streamprotected voidloadDataUpTo(long requestedPos) Preload data up to including the requested position.intloadFully(int amount, boolean exact) Transfer the given amount of data from the supplier to the end of the buffer.static voidstatic voidstatic <A> ReadableChannelSwitchable<A> newBufferedChannel(BufferOverReadableChannel<A> buffer) Create a switchable buffered channel where the buffering can be disabled laterprotected intintvoidsetDataSupplier(ReadableChannel<A> dataSupplier) Set a new data supplier whose data can be appended to this buffer on demandvoidtruncate()Sets the end marker to position 0 effectively resizing the buffer to size 0.voidThe method that needs to be implemented; all other methods default-delegate to this one.voidwrite(long offsetInBuffer, ReadableChannel<A> source, int amount) Read a certain amount of data from a channel into this buffer starting at a certain positionMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ArrayReadable
get, readIntoRaw, sizeMethods inherited from interface ArrayWritable
put, write, writeMethods inherited from interface SeekableReadableChannelSource
newReadableChannel, newReadableChannel, newReadableChannel
-
Field Details
-
buckets
The buffered data -
arrayOps
-
activeEnd
End marker with two components (idx, pos) it is wrapped in an object to enable atomic replacement of the reference The pointer is monotonous in the sense that the end marker's logical linear location is only increased Reading an old version while a new one has been set will only cause a read to return on the old boundary, but a subsequent synchronized check for whether loading of additional data is needed is then made anyway -
knownDataSize
protected long knownDataSizeThe number of cached bytes. Corresponds to the linear representation of activeEnd. -
dataSupplier
-
isDataSupplierConsumed
protected boolean isDataSupplierConsumed -
minReadSize
protected int minReadSize -
maxReadSize
protected int maxReadSizeMaximum number to read from the dataSupplier in one request -
bufferView
-
-
Constructor Details
-
BufferOverReadableChannel
public BufferOverReadableChannel(ArrayOps<A> arrayOps, int initialBucketSize, ReadableChannel<A> dataSupplier, int minReadSize)
-
-
Method Details
-
getKnownDataSize
public long getKnownDataSize() -
getBuffer
-
getDataSupplier
Return the data supplier. The data supplier usually needs to be closed when it is no longer needed. -
setDataSupplier
Set a new data supplier whose data can be appended to this buffer on demand -
getPosition
-
getPointer
public static <A> BufferOverReadableChannel.BucketPointer getPointer(ArrayOps<A> arrayOps, A[] buckets, BufferOverReadableChannel.BucketPointer end, long pos) - Parameters:
buckets-pos-- Returns:
- Pointer to a valid location in the known data block or null
-
getPointerRel
public static <A> BufferOverReadableChannel.BucketPointer getPointerRel(ArrayOps<A> arrayOps, A[] buckets, BufferOverReadableChannel.BucketPointer end, long startPos, BufferOverReadableChannel.BucketPointer startPtr, long pos) -
isDataSupplierConsumed
public boolean isDataSupplierConsumed()If this method returns true then further reads will not increase the known size unless a new data supplier is set. -
nextBucketSize
protected int nextBucketSize() -
doRead
-
loadDataUpTo
protected void loadDataUpTo(long requestedPos) Preload data up to including the requested position. It is inclusive in order to allow for checking whether the requested position is in range.- Parameters:
requestedPos-
-
truncate
public void truncate()Sets the end marker to position 0 effectively resizing the buffer to size 0. Allocated resources remain untouched. -
loadFully
public int loadFully(int amount, boolean exact) Transfer the given amount of data from the supplier to the end of the buffer. Returns the transferred amount or -1 if no data was transferred - should never return 0.- Parameters:
amount-exact- If true then the requested amount is not subject to adjustment by the minimum read length- Returns:
-
loadData
protected int loadData(int needed, boolean exact) fetch a chunk from the input stream -
ensureCapacityInActiveBucket
protected void ensureCapacityInActiveBucket() -
getCapacity
public long getCapacity()Description copied from interface:BufferLikeBuffers with 'unlimited' capacity should return Long.MAX_VALUE- Specified by:
getCapacityin interfaceBufferLike<A>
-
write
Read a certain amount of data from a channel into this buffer starting at a certain position- Throws:
IOException
-
write
public void write(long offsetInBuffer, A arrayWithItemsOfTypeT, int arrOffset, int arrLength) throws IOException Description copied from interface:ArrayWritableThe method that needs to be implemented; all other methods default-delegate to this one.- Specified by:
writein interfaceArrayWritable<A>- Throws:
IOException
-
getArrayOps
- Specified by:
getArrayOpsin interfaceHasArrayOps<A>
-
readInto
- Specified by:
readIntoin interfaceArrayReadable<A>- Throws:
IOException
-
newReadableChannel
- Specified by:
newReadableChannelin interfaceArrayReadable<A>- Specified by:
newReadableChannelin interfaceReadableChannelFactory<A>- Specified by:
newReadableChannelin interfaceSeekableReadableChannelSource<A>- Throws:
IOException
-
createForObjects
-
createForBytes
-
createForBytes
-
createForBytes
-
createForBytes
public static BufferOverReadableChannel<byte[]> createForBytes(ReadableChannel<byte[]> channel, int minReadSize) -
main
- Throws:
IOException
-
main2
- Throws:
IOException
-
newBufferedChannel
public static <A> ReadableChannelSwitchable<A> newBufferedChannel(BufferOverReadableChannel<A> buffer) Create a switchable buffered channel where the buffering can be disabled later -
debuffer
Remove the buffer of channel created with newBufferedChannel
-