Class ByteBufferInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.postgresql.pljava.internal.ByteBufferInputStream
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
- Direct Known Subclasses:
VarlenaWrapper.Input.Stream
public abstract class ByteBufferInputStream extends InputStream
Wrap a readableByteBuffer
as anInputStream
.An implementing class must provide a
buffer()
method that returns theByteBuffer
, and the method is responsible for knowing when the memory region windowed by theByteBuffer
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 besynchronized
on.The underlying buffer's
position
andmark
are used to maintain the corresponding values for the input stream.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ByteBufferInputStream()
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 Type Method Description int
available()
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
markSupported()
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 Detail
-
m_lock
protected final Object m_lock
The object on which theInputStream
operations will synchronize.
-
m_open
protected boolean m_open
Whether this stream is open; initially true.
-
-
Constructor Detail
-
ByteBufferInputStream
protected ByteBufferInputStream()
Construct an instance whose critical sections will synchronize on the instance itself.
-
ByteBufferInputStream
protected ByteBufferInputStream(Object lock)
Construct an instance, given an object on which to synchronize.- Parameters:
lock
- The Object to synchronize on.
-
-
Method Detail
-
pin
protected void pin() throws IOException
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
protected abstract ByteBuffer buffer() throws IOException
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
public int read() throws IOException
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
public int read(byte[] b, int off, int len) throws IOException
- Overrides:
read
in classInputStream
- Throws:
IOException
-
skip
public long skip(long n) throws IOException
- Overrides:
skip
in classInputStream
- Throws:
IOException
-
available
public int available() throws IOException
- Overrides:
available
in classInputStream
- Throws:
IOException
-
close
public void close() throws IOException
- 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
public void reset() throws IOException
- Overrides:
reset
in classInputStream
- Throws:
IOException
-
markSupported
public boolean markSupported()
Returntrue
; this class does supportmark
andreset
.- Overrides:
markSupported
in classInputStream
-
-