Top Description Inners Methods
org.postgresql.util

public Interface ByteStreamWriter

Known Direct Implementers
org.postgresql.util.ByteBufferByteStreamWriter, org.postgresql.util.ByteBuffersByteStreamWriter
Imports
java.io.IOException, .InputStream, .OutputStream, java.nio.ByteBuffer

A class that can be used to set a byte array parameter by writing to an OutputStream.

The intended use case is wanting to write data to a byte array parameter that is stored off heap in a direct memory pool or in some other form that is inconvenient to assemble into a single heap-allocated buffer.

Users should write their own implementation depending on the original data source. The driver provides a built-in implementation supporting the java.nio.ByteBuffer class, see ByteBufferByteStreamWriter.

Intended usage is to simply pass in an instance using java.sql.PreparedStatement#setObject(int, Object):

    int bufLength = someBufferObject.length();
    preparedStatement.setObject(1, new MyByteStreamWriter(bufLength, someBufferObject));

The length must be known ahead of the stream being written to.

This provides the application more control over memory management than calling java.sql.PreparedStatement#setBinaryStream(int, InputStream) as with the latter the caller has no control over the buffering strategy.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
ByteStreamWriter.ByteStreamTarget

Provides a target to write bytes to.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

the number of bytes in the stream.
getLength
()

Returns the length of the stream.

public static ByteStreamWriter
of(ByteBuffer... buf)

public void
writeTo(ByteStreamWriter.ByteStreamTarget
the stream to write the data to
target
)

Write the data to the provided OutputStream.

Method Detail

getLengthback to summary
public int getLength()

Returns the length of the stream.

This must be known ahead of calling writeTo(ByteStreamTarget).

Returns:int

the number of bytes in the stream.

ofback to summary
public static ByteStreamWriter of(ByteBuffer... buf)
writeToback to summary
public void writeTo(ByteStreamWriter.ByteStreamTarget target) throws IOException

Write the data to the provided OutputStream.

Should not write more than getLength() bytes. If attempted, the provided stream will throw an java.io.IOException.

Parameters
target:ByteStreamWriter.ByteStreamTarget

the stream to write the data to

Exceptions
IOException:
if the underlying stream throws or there is some other error.
org.postgresql.util back to summary

public Interface ByteStreamWriter.ByteStreamTarget


Provides a target to write bytes to.

Method Summary

Modifier and TypeMethod and Description
public OutputStream

Returns:

an output stream
getOutputStream
()

Provides an output stream to write bytes to.

Method Detail

getOutputStreamback to summary
public OutputStream getOutputStream()

Provides an output stream to write bytes to.

Returns:OutputStream

an output stream