public class PGStream extends Object implements Closeable, Flushable
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.
Constructor and Description |
---|
PGStream(PGStream pgStream,
int timeout) |
PGStream(SocketFactory socketFactory,
HostSpec hostSpec)
Deprecated.
|
PGStream(SocketFactory socketFactory,
HostSpec hostSpec,
int timeout)
Constructor: Connect to the PostgreSQL back end and return a stream connection.
|
Modifier and Type | Method and Description |
---|---|
void |
changeSocket(Socket socket)
Switch this stream to using a new socket.
|
void |
clearMaxRowSizeBytes()
Clear value of max row size noticed so far.
|
void |
clearResultBufferCount()
Clear count of byte buffer.
|
void |
close()
Closes the connection.
|
void |
flush()
Flush any pending output to the backend.
|
Encoding |
getEncoding() |
Writer |
getEncodingWriter()
Get a Writer instance that encodes directly onto the underlying stream.
|
HostSpec |
getHostSpec() |
long |
getMaxResultBuffer()
Get MaxResultBuffer from PGStream.
|
int |
getMaxRowSizeBytes()
Get actual max row size noticed so far.
|
int |
getNetworkTimeout() |
Socket |
getSocket() |
SocketFactory |
getSocketFactory() |
boolean |
hasMessagePending()
Check for pending backend messages without blocking.
|
boolean |
isClosed() |
boolean |
isGssEncrypted() |
int |
peekChar()
Receives a single character from the backend, without advancing the current protocol stream
position.
|
void |
receive(byte[] buf,
int off,
int siz)
Reads in a given number of bytes from the backend.
|
byte[] |
receive(int siz)
Reads in a given number of bytes from the backend.
|
String |
receiveCanonicalString()
Receives a null-terminated string from the backend and attempts to decode to a
canonical String . |
String |
receiveCanonicalStringIfPresent()
Receives a null-terminated string from the backend and attempts to decode to a
canonical String . |
int |
receiveChar()
Receives a single character from the backend.
|
void |
receiveEOF()
Consume an expected EOF from the backend.
|
EncodingPredictor.DecodeResult |
receiveErrorString(int len)
Receives a fixed-size string from the backend, and tries to avoid "UTF-8 decode failed"
errors.
|
int |
receiveInteger2()
Receives a two byte integer from the backend.
|
int |
receiveInteger4()
Receives a four byte integer from the backend.
|
String |
receiveString()
Receives a null-terminated string from the backend.
|
String |
receiveString(int len)
Receives a fixed-size string from the backend.
|
Tuple |
receiveTupleV3()
Read a tuple from the back end.
|
void |
send(byte[] buf)
Send an array of bytes to the backend.
|
void |
send(byte[] buf,
int siz)
Send a fixed-size array of bytes to the backend.
|
void |
send(byte[] buf,
int off,
int siz)
Send a fixed-size array of bytes to the backend.
|
void |
send(ByteStreamWriter writer)
Send a fixed-size array of bytes to the backend.
|
void |
sendChar(int val)
Sends a single character to the back end.
|
void |
sendInteger2(int val)
Sends a 2-byte integer (short) to the back end.
|
void |
sendInteger4(int val)
Sends a 4-byte integer to the back end.
|
void |
sendStream(InputStream inStream,
int remaining)
Copy data from an input stream to the connection.
|
void |
setEncoding(Encoding encoding)
Change the encoding used by this connection.
|
void |
setMaxResultBuffer(@Nullable String value)
Method to set MaxResultBuffer inside PGStream.
|
void |
setMaxRowSizeBytes(int rowSizeBytes)
The idea behind this method is to keep in maxRowSize the size of biggest read data row.
|
void |
setMinStreamAvailableCheckDelay(int delay) |
void |
setNetworkTimeout(int milliseconds) |
void |
setSecContext(GSSContext secContext) |
void |
skip(int size) |
public PGStream(SocketFactory socketFactory, HostSpec hostSpec, int timeout) throws IOException
socketFactory
- socket factory to use when creating socketshostSpec
- the host and port to connect totimeout
- timeout in milliseconds, or 0 if no timeout setIOException
- if an IOException occurs below it.public PGStream(PGStream pgStream, int timeout) throws IOException
IOException
@Deprecated public PGStream(SocketFactory socketFactory, HostSpec hostSpec) throws IOException
PGStream(SocketFactory, org.postgresql.util.HostSpec, int)
socketFactory
- socket factoryhostSpec
- the host and port to connect toIOException
- if an IOException occurs below it.public boolean isGssEncrypted()
public void setSecContext(GSSContext secContext)
public HostSpec getHostSpec()
public Socket getSocket()
public SocketFactory getSocketFactory()
public boolean hasMessagePending() throws IOException
IOException
- if something wrong happenspublic void setMinStreamAvailableCheckDelay(int delay)
public void changeSocket(Socket socket) throws IOException
socket
- the new socket to change toIOException
- if something goes wrongpublic Encoding getEncoding()
public void setEncoding(Encoding encoding) throws IOException
encoding
- the new encoding to useIOException
- if something goes wrongpublic 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.
IOException
- if something goes wrong.public void sendChar(int val) throws IOException
val
- the character to be sentIOException
- if an I/O error occurspublic void sendInteger4(int val) throws IOException
val
- the integer to be sentIOException
- if an I/O error occurspublic void sendInteger2(int val) throws IOException
val
- the integer to be sentIOException
- if an I/O error occurs or val
cannot be encoded in 2 bytespublic void send(byte[] buf) throws IOException
buf
- The array of bytes to be sentIOException
- if an I/O error occurspublic void send(byte[] buf, int siz) throws IOException
buf.length < siz
, pad with zeros.
If buf.lengh > siz
, truncate the array.buf
- the array of bytes to be sentsiz
- the number of bytes to be sentIOException
- if an I/O error occurspublic void send(byte[] buf, int off, int siz) throws IOException
length < siz
, pad with zeros. If
length > siz
, truncate the array.buf
- the array of bytes to be sentoff
- offset in the array to start sending fromsiz
- the number of bytes to be sentIOException
- if an I/O error occurspublic void send(ByteStreamWriter writer) throws IOException
length < siz
, pad with zeros. If
length > siz
, truncate the array.writer
- the stream writer to invoke to send the bytesIOException
- if an I/O error occurspublic int peekChar() throws IOException
IOException
- if an I/O Error occurspublic int receiveChar() throws IOException
IOException
- if an I/O Error occurspublic int receiveInteger4() throws IOException
IOException
- if an I/O error occurspublic int receiveInteger2() throws IOException
IOException
- if an I/O error occurspublic String receiveString(int len) throws IOException
len
- the length of the string to receive, in bytes.IOException
- if something wrong happenspublic EncodingPredictor.DecodeResult receiveErrorString(int len) throws IOException
len
- the length of the string to receive, in bytes.IOException
- if something wrong happenspublic String receiveString() throws IOException
IOException
- if an I/O error occurs, or end of filepublic String receiveCanonicalString() throws IOException
canonical
String
.
If we don't see a null, then we assume something has gone wrong.IOException
- if an I/O error occurs, or end of fileEncoding.decodeCanonicalized(byte[], int, int)
public String receiveCanonicalStringIfPresent() throws IOException
canonical
String
.
If we don't see a null, then we assume something has gone wrong.IOException
- if an I/O error occurs, or end of fileEncoding.decodeCanonicalizedIfPresent(byte[], int, int)
public Tuple receiveTupleV3() throws IOException, OutOfMemoryError, SQLException
IOException
- if a data I/O error occursSQLException
- if read more bytes than set maxResultBufferOutOfMemoryError
public byte[] receive(int siz) throws IOException
siz
- number of bytes to readIOException
- if a data I/O error occurspublic void receive(byte[] buf, int off, int siz) throws IOException
buf
- buffer to store resultoff
- offset in buffersiz
- number of bytes to readIOException
- if a data I/O error occurspublic void skip(int size) throws IOException
IOException
public void sendStream(InputStream inStream, int remaining) throws IOException
inStream
- the stream to read data fromremaining
- the number of bytes to copyIOException
- if a data I/O error occurspublic void flush() throws IOException
flush
in interface Flushable
IOException
- if an I/O error occurspublic void receiveEOF() throws SQLException, IOException
IOException
- if an I/O error occursSQLException
- if we get something other than an EOFpublic void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
- if an I/O Error occurspublic void setNetworkTimeout(int milliseconds) throws IOException
IOException
public int getNetworkTimeout() throws IOException
IOException
public void setMaxResultBuffer(@Nullable String value) throws PSQLException
value
- value of new max result buffer as string (cause we can expect % or chars to use
multiplier)PSQLException
- exception returned when occurred parsing problem.public long getMaxResultBuffer()
public void setMaxRowSizeBytes(int rowSizeBytes)
rowSizeBytes
- new value to be set as maxRowSizeBytespublic int getMaxRowSizeBytes()
public void clearMaxRowSizeBytes()
public void clearResultBufferCount()
public boolean isClosed()
Copyright © 1997-2020 PostgreSQL Global Development Group. All Rights Reserved.