Class ByteBufferInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
DatumImpl.IStream
ByteBuffer
as an InputStream
.
An implementing class must provide a buffer()
method that returns the
ByteBuffer
, and the method is responsible for knowing when the memory
region windowed by the ByteBuffer
is no longer to be accessed, and
throwing an exception in that case.
The implementing class may supply an object that the InputStream
operations will be synchronized
on.
The underlying buffer's
position
and
mark
are used to maintain the corresponding values
for the input stream.
-
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
Construct an instance whose critical sections will synchronize on the instance itself.protected
ByteBufferInputStream
(Object lock) Construct an instance, given an object on which to synchronize. -
Method Summary
Modifier and TypeMethodDescriptionint
protected abstract ByteBuffer
buffer()
Return theByteBuffer
being wrapped, or throw an exception if the memory windowed by the buffer should no longer be accessed.void
close()
void
mark
(int readlimit) boolean
Returntrue
; this class does supportmark
andreset
.protected void
pin()
Pin resources if necessary during a reading operation.int
read()
int
read
(byte[] b, int off, int len) void
reset()
long
skip
(long n) protected void
unpin()
Unpin resources if necessary after a reading operation.Methods inherited from class java.io.InputStream
nullInputStream, read, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Field Details
-
m_lock
The object on which theInputStream
operations will synchronize. -
m_open
protected boolean m_openWhether this stream is open; initially true.
-
-
Constructor Details
-
ByteBufferInputStream
protected ByteBufferInputStream()Construct an instance whose critical sections will synchronize on the instance itself. -
ByteBufferInputStream
Construct an instance, given an object on which to synchronize.- Parameters:
lock
- The Object to synchronize on.
-
-
Method Details
-
pin
Pin resources if necessary during a reading operation.This default implementation does nothing. A subclass should override it if (in addition to synchronizing on
m_lock
), some pinning of a resource is needed during access operations.- Throws:
IOException
-
unpin
protected void unpin()Unpin resources if necessary after a reading operation.This default implementation does nothing.
-
buffer
Return theByteBuffer
being wrapped, or throw an exception if the memory windowed by the buffer should no longer be accessed.The monitor on
m_lock
is held when this method is called.This method also should throw an exception if
m_open
is false. It is called everywhere that should happen, so it is the perfect place for the test, and allows the implementing class to use a customized message in the exception.All uses of the buffer in this class are preceded by
pin()
and followed byunpin()
(whose default implementations in this class do nothing). If a subclass overridespin
with a version that throws the appropriate exception in either case or both, it is then redundant and unnecessary forbuffer
to check the same conditions.- Throws:
IOException
-
read
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
-
skip
- Overrides:
skip
in classInputStream
- Throws:
IOException
-
available
- Overrides:
available
in classInputStream
- Throws:
IOException
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
mark
public void mark(int readlimit) - Overrides:
mark
in classInputStream
-
reset
- Overrides:
reset
in classInputStream
- Throws:
IOException
-
markSupported
public boolean markSupported()Returntrue
; this class does supportmark
andreset
.- Overrides:
markSupported
in classInputStream
-