Top Description Fields Constructors Methods
org.postgresql.core

public Class PGStream

extends Object
implements Closeable, Flushable
Class Inheritance
All Implemented Interfaces
java.io.Flushable, java.io.Closeable, java.lang.AutoCloseable
Imports
org.postgresql.gss.GSSInputStream, .GSSOutputStream, org.postgresql.util.ByteStreamWriter, .GT, .HostSpec, .PGPropertyMaxResultBufferParser, .PSQLException, .PSQLState, org.checkerframework.checker.nullness.qual.Nullable, org.ietf.jgss.GSSContext, .MessageProp, java.io.BufferedOutputStream, .Closeable, .EOFException, .FilterOutputStream, .Flushable, .IOException, .InputStream, .OutputStream, .Writer, java.net.InetAddress, .InetSocketAddress, .Socket, .SocketException, .SocketTimeoutException, java.sql.SQLException, javax.net.SocketFactory

Wrapper around the raw connection to the server that implements some basic primitives (reading/writing formatted data, doing string encoding, etc).

In general, instances of PGStream are not threadsafe; the caller must ensure that only one thread at a time is accessing a particular PGStream instance.

Field Summary

Modifier and TypeField and Description
private Socket
private Encoding
private Writer
pack-priv boolean
private final HostSpec
private final byte[]
private final byte[]
private long
private int
private int
private long
private VisibleBufferedInputStream
private OutputStream
private long
private final SocketFactory
private @Nullable byte[]

Constructor Summary

AccessConstructor and Description
public
PGStream(SocketFactory
socket factory to use when creating sockets
socketFactory
,
HostSpec
the host and port to connect to
hostSpec
,
int
timeout in milliseconds, or 0 if no timeout set
timeout
)

Constructor: Connect to the PostgreSQL back end and return a stream connection.

public
PGStream(PGStream pgStream, int timeout)

public
PGStream(SocketFactory
socket factory
socketFactory
,
HostSpec
the host and port to connect to
hostSpec
)

Constructor: Connect to the PostgreSQL back end and return a stream connection.

Method Summary

Modifier and TypeMethod and Description
public void
changeSocket(Socket
the new socket to change to
socket
)

Switch this stream to using a new socket.

public void
clearMaxRowSizeBytes()

Clear value of max row size noticed so far.

public void
clearResultBufferCount()

Clear count of byte buffer.

public void
close()

Implements java.io.Closeable.close.

Closes the connection.

private Socket
createSocket(int timeout)

public void
flush()

Implements java.io.Flushable.flush.

Flush any pending output to the backend.

public Encoding
public Writer

Returns:

the shared Writer instance
getEncodingWriter
()

Get a Writer instance that encodes directly onto the underlying stream.

public HostSpec
public long

Returns:

size of MaxResultBuffer
getMaxResultBuffer
()

Get MaxResultBuffer from PGStream.

public int

Returns:

value of max row size
getMaxRowSizeBytes
()

Get actual max row size noticed so far.

public int
public Socket
public SocketFactory
public boolean

Returns:

true if there is a pending backend message
hasMessagePending
()

Check for pending backend messages without blocking.

private void
increaseByteCounter(long
size of bytes to add to byte buffer.
value
)

Increase actual count of buffer.

public boolean
public boolean
public int

Returns:

the character received
peekChar
()

Receives a single character from the backend, without advancing the current protocol stream position.

public byte[]

Returns:

array of bytes received
receive
(int
number of bytes to read
siz
)

Reads in a given number of bytes from the backend.

public void
receive(byte[]
buffer to store result
buf
,
int
offset in buffer
off
,
int
number of bytes to read
siz
)

Reads in a given number of bytes from the backend.

public String

Returns:

string from back end
receiveCanonicalString
()

Receives a null-terminated string from the backend and attempts to decode to a canonical String.

public String

Returns:

string from back end
receiveCanonicalStringIfPresent
()

Receives a null-terminated string from the backend and attempts to decode to a canonical String.

public int

Returns:

the character received
receiveChar
()

Receives a single character from the backend.

public void
receiveEOF()

Consume an expected EOF from the backend.

public EncodingPredictor.DecodeResult

Returns:

the decoded string
receiveErrorString
(int
the length of the string to receive, in bytes.
len
)

Receives a fixed-size string from the backend, and tries to avoid "UTF-8 decode failed" errors.

public int

Returns:

the integer received from the backend
receiveInteger2
()

Receives a two byte integer from the backend.

public int

Returns:

the integer received from the backend
receiveInteger4
()

Receives a four byte integer from the backend.

public String

Returns:

the decoded string
receiveString
(int
the length of the string to receive, in bytes.
len
)

Receives a fixed-size string from the backend.

public String

Returns:

string from back end
receiveString
()

Receives a null-terminated string from the backend.

public Tuple

Returns:

tuple from the back end
receiveTupleV3
()

Read a tuple from the back end.

public void
send(byte[]
The array of bytes to be sent
buf
)

Send an array of bytes to the backend.

public void
send(byte[]
the array of bytes to be sent
buf
,
int
the number of bytes to be sent
siz
)

Send a fixed-size array of bytes to the backend.

public void
send(byte[]
the array of bytes to be sent
buf
,
int
offset in the array to start sending from
off
,
int
the number of bytes to be sent
siz
)

Send a fixed-size array of bytes to the backend.

public void
send(ByteStreamWriter
the stream writer to invoke to send the bytes
writer
)

Send a fixed-size array of bytes to the backend.

public void
sendChar(int
the character to be sent
val
)

Sends a single character to the back end.

public void
sendInteger2(int
the integer to be sent
val
)

Sends a 2-byte integer (short) to the back end.

public void
sendInteger4(int
the integer to be sent
val
)

Sends a 4-byte integer to the back end.

public void
sendStream(InputStream
the stream to read data from
inStream
,
int
the number of bytes to copy
remaining
)

Copy data from an input stream to the connection.

public void
setEncoding(Encoding
the new encoding to use
encoding
)

Change the encoding used by this connection.

public void
setMaxResultBuffer(@Nullable String
value of new max result buffer as string (cause we can expect % or chars to use multiplier)
value
)

Method to set MaxResultBuffer inside PGStream.

public void
setMaxRowSizeBytes(int
new value to be set as maxRowSizeBytes
rowSizeBytes
)

The idea behind this method is to keep in maxRowSize the size of biggest read data row.

public void
public void
setNetworkTimeout(int milliseconds)

public void
public void
skip(int size)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

connectionback to summary
private Socket connection
encodingback to summary
private Encoding encoding
encodingWriterback to summary
private Writer encodingWriter
gssEncryptedback to summary
pack-priv boolean gssEncrypted
hostSpecback to summary
private final HostSpec hostSpec
int2Bufback to summary
private final byte[] int2Buf
int4Bufback to summary
private final byte[] int4Buf
maxResultBufferback to summary
private long maxResultBuffer
maxRowSizeBytesback to summary
private int maxRowSizeBytes
minStreamAvailableCheckDelayback to summary
private int minStreamAvailableCheckDelay
nextStreamAvailableCheckTimeback to summary
private long nextStreamAvailableCheckTime
pgInputback to summary
private VisibleBufferedInputStream pgInput
pgOutputback to summary
private OutputStream pgOutput
resultBufferByteCountback to summary
private long resultBufferByteCount
socketFactoryback to summary
private final SocketFactory socketFactory
streamBufferback to summary
private @Nullable byte[] streamBuffer

Constructor Detail

PGStreamback to summary
public PGStream(SocketFactory socketFactory, HostSpec hostSpec, int timeout) throws IOException

Constructor: Connect to the PostgreSQL back end and return a stream connection.

Parameters
socketFactory:SocketFactory

socket factory to use when creating sockets

hostSpec:HostSpec

the host and port to connect to

timeout:int

timeout in milliseconds, or 0 if no timeout set

Annotations
@SuppressWarnings:method.invocation, initialization.fields.uninitialized
Exceptions
IOException:
if an IOException occurs below it.
PGStreamback to summary
public PGStream(PGStream pgStream, int timeout) throws IOException
Annotations
@SuppressWarnings:method.invocation, initialization.fields.uninitialized
PGStreamback to summary
public PGStream(SocketFactory socketFactory, HostSpec hostSpec) throws IOException

Constructor: Connect to the PostgreSQL back end and return a stream connection.

Parameters
socketFactory:SocketFactory

socket factory

hostSpec:HostSpec

the host and port to connect to

Annotations
@Deprecated
Exceptions
IOException:
if an IOException occurs below it.

Method Detail

changeSocketback to summary
public void changeSocket(Socket socket) throws IOException

Switch this stream to using a new socket. Any existing socket is not closed; it's assumed that we are changing to a new socket that delegates to the original socket (e.g. SSL).

Parameters
socket:Socket

the new socket to change to

Exceptions
IOException:
if something goes wrong
clearMaxRowSizeBytesback to summary
public void clearMaxRowSizeBytes()

Clear value of max row size noticed so far.

clearResultBufferCountback to summary
public void clearResultBufferCount()

Clear count of byte buffer.

closeback to summary
public void close() throws IOException

Implements java.io.Closeable.close.

Closes the connection.

Annotations
@Override
Exceptions
IOException:
if an I/O Error occurs
createSocketback to summary
private Socket createSocket(int timeout) throws IOException
flushback to summary
public void flush() throws IOException

Implements java.io.Flushable.flush.

Flush any pending output to the backend.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
getEncodingback to summary
public Encoding getEncoding()
getEncodingWriterback to summary
public Writer getEncodingWriter() throws IOException

Get a Writer instance that encodes directly onto the underlying stream.

The returned Writer should not be closed, as it's a shared object. Writer.flush needs to be called when switching between use of the Writer and use of the PGStream write methods, but it won't actually flush output all the way out -- call flush to actually ensure all output has been pushed to the server.

Returns:Writer

the shared Writer instance

Exceptions
IOException:
if something goes wrong.
getHostSpecback to summary
public HostSpec getHostSpec()
getMaxResultBufferback to summary
public long getMaxResultBuffer()

Get MaxResultBuffer from PGStream.

Returns:long

size of MaxResultBuffer

getMaxRowSizeBytesback to summary
public int getMaxRowSizeBytes()

Get actual max row size noticed so far.

Returns:int

value of max row size

getNetworkTimeoutback to summary
public int getNetworkTimeout() throws IOException
getSocketback to summary
public Socket getSocket()
getSocketFactoryback to summary
public SocketFactory getSocketFactory()
hasMessagePendingback to summary
public boolean hasMessagePending() throws IOException

Check for pending backend messages without blocking. Might return false when there actually are messages waiting, depending on the characteristics of the underlying socket. This is used to detect asynchronous notifies from the backend, when available.

Returns:boolean

true if there is a pending backend message

Exceptions
IOException:
if something wrong happens
increaseByteCounterback to summary
private void increaseByteCounter(long value) throws SQLException

Increase actual count of buffer. If buffer count is bigger than max result buffer limit, then gonna return an exception.

Parameters
value:long

size of bytes to add to byte buffer.

Exceptions
SQLException:
exception returned when result buffer count is bigger than max result buffer.
isClosedback to summary
public boolean isClosed()
isGssEncryptedback to summary
public boolean isGssEncrypted()
peekCharback to summary
public int peekChar() throws IOException

Receives a single character from the backend, without advancing the current protocol stream position.

Returns:int

the character received

Exceptions
IOException:
if an I/O Error occurs
receiveback to summary
public byte[] receive(int siz) throws IOException

Reads in a given number of bytes from the backend.

Parameters
siz:int

number of bytes to read

Returns:byte[]

array of bytes received

Exceptions
IOException:
if a data I/O error occurs
receiveback to summary
public void receive(byte[] buf, int off, int siz) throws IOException

Reads in a given number of bytes from the backend.

Parameters
buf:byte[]

buffer to store result

off:int

offset in buffer

siz:int

number of bytes to read

Exceptions
IOException:
if a data I/O error occurs
receiveCanonicalStringback to summary
public String receiveCanonicalString() throws IOException

Receives a null-terminated string from the backend and attempts to decode to a canonical String. If we don't see a null, then we assume something has gone wrong.

Returns:String

string from back end

Exceptions
IOException:
if an I/O error occurs, or end of file
See Also
Encoding#decodeCanonicalized(byte[], int, int)
receiveCanonicalStringIfPresentback to summary
public String receiveCanonicalStringIfPresent() throws IOException

Receives a null-terminated string from the backend and attempts to decode to a canonical String. If we don't see a null, then we assume something has gone wrong.

Returns:String

string from back end

Exceptions
IOException:
if an I/O error occurs, or end of file
See Also
Encoding#decodeCanonicalizedIfPresent(byte[], int, int)
receiveCharback to summary
public int receiveChar() throws IOException

Receives a single character from the backend.

Returns:int

the character received

Exceptions
IOException:
if an I/O Error occurs
receiveEOFback to summary
public void receiveEOF() throws SQLException, IOException

Consume an expected EOF from the backend.

Exceptions
SQLException:
if we get something other than an EOF
IOException:
if an I/O error occurs
receiveErrorStringback to summary
public EncodingPredictor.DecodeResult receiveErrorString(int len) throws IOException

Receives a fixed-size string from the backend, and tries to avoid "UTF-8 decode failed" errors.

Parameters
len:int

the length of the string to receive, in bytes.

Returns:EncodingPredictor.DecodeResult

the decoded string

Exceptions
IOException:
if something wrong happens
receiveInteger2back to summary
public int receiveInteger2() throws IOException

Receives a two byte integer from the backend.

Returns:int

the integer received from the backend

Exceptions
IOException:
if an I/O error occurs
receiveInteger4back to summary
public int receiveInteger4() throws IOException

Receives a four byte integer from the backend.

Returns:int

the integer received from the backend

Exceptions
IOException:
if an I/O error occurs
receiveStringback to summary
public String receiveString(int len) throws IOException

Receives a fixed-size string from the backend.

Parameters
len:int

the length of the string to receive, in bytes.

Returns:String

the decoded string

Exceptions
IOException:
if something wrong happens
receiveStringback to summary
public String receiveString() throws IOException

Receives a null-terminated string from the backend. If we don't see a null, then we assume something has gone wrong.

Returns:String

string from back end

Exceptions
IOException:
if an I/O error occurs, or end of file
receiveTupleV3back to summary
public Tuple receiveTupleV3() throws IOException, OutOfMemoryError, SQLException

Read a tuple from the back end. A tuple is a two dimensional array of bytes. This variant reads the V3 protocol's tuple representation.

Returns:Tuple

tuple from the back end

Exceptions
IOException:
if a data I/O error occurs
SQLException:
if read more bytes than set maxResultBuffer
sendback to summary
public void send(byte[] buf) throws IOException

Send an array of bytes to the backend.

Parameters
buf:byte[]

The array of bytes to be sent

Exceptions
IOException:
if an I/O error occurs
sendback to summary
public void send(byte[] buf, int siz) throws IOException

Send a fixed-size array of bytes to the backend. If buf.length < siz, pad with zeros. If buf.length > siz, truncate the array.

Parameters
buf:byte[]

the array of bytes to be sent

siz:int

the number of bytes to be sent

Exceptions
IOException:
if an I/O error occurs
sendback to summary
public void send(byte[] buf, int off, int siz) throws IOException

Send a fixed-size array of bytes to the backend. If length < siz, pad with zeros. If length > siz, truncate the array.

Parameters
buf:byte[]

the array of bytes to be sent

off:int

offset in the array to start sending from

siz:int

the number of bytes to be sent

Exceptions
IOException:
if an I/O error occurs
sendback to summary
public void send(ByteStreamWriter writer) throws IOException

Send a fixed-size array of bytes to the backend. If length < siz, pad with zeros. If length > siz, truncate the array.

Parameters
writer:ByteStreamWriter

the stream writer to invoke to send the bytes

Exceptions
IOException:
if an I/O error occurs
sendCharback to summary
public void sendChar(int val) throws IOException

Sends a single character to the back end.

Parameters
val:int

the character to be sent

Exceptions
IOException:
if an I/O error occurs
sendInteger2back to summary
public void sendInteger2(int val) throws IOException

Sends a 2-byte integer (short) to the back end.

Parameters
val:int

the integer to be sent

Exceptions
IOException:
if an I/O error occurs or val cannot be encoded in 2 bytes
sendInteger4back to summary
public void sendInteger4(int val) throws IOException

Sends a 4-byte integer to the back end.

Parameters
val:int

the integer to be sent

Exceptions
IOException:
if an I/O error occurs
sendStreamback to summary
public void sendStream(InputStream inStream, int remaining) throws IOException

Copy data from an input stream to the connection.

Parameters
inStream:InputStream

the stream to read data from

remaining:int

the number of bytes to copy

Exceptions
IOException:
if a data I/O error occurs
setEncodingback to summary
public void setEncoding(Encoding encoding) throws IOException

Change the encoding used by this connection.

Parameters
encoding:Encoding

the new encoding to use

Exceptions
IOException:
if something goes wrong
setMaxResultBufferback to summary
public void setMaxResultBuffer(@Nullable String value) throws PSQLException

Method to set MaxResultBuffer inside PGStream.

Parameters
value:@Nullable String

value of new max result buffer as string (cause we can expect % or chars to use multiplier)

Exceptions
PSQLException:
exception returned when occurred parsing problem.
setMaxRowSizeBytesback to summary
public void setMaxRowSizeBytes(int rowSizeBytes)

The idea behind this method is to keep in maxRowSize the size of biggest read data row. As there may be many data rows send after each other for a query, then value in maxRowSize would contain value noticed so far, because next data rows and their sizes are not read for that moment. We want it increasing, because the size of the biggest among data rows will be used during computing new adaptive fetch size for the query.

Parameters
rowSizeBytes:int

new value to be set as maxRowSizeBytes

setMinStreamAvailableCheckDelayback to summary
public void setMinStreamAvailableCheckDelay(int delay)
setNetworkTimeoutback to summary
public void setNetworkTimeout(int milliseconds) throws IOException
setSecContextback to summary
public void setSecContext(GSSContext secContext)
skipback to summary
public void skip(int size) throws IOException