public class PGStream
extends java.lang.Object
implements java.io.Closeable, java.io.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.
Modifier and Type | Field and Description |
---|---|
private java.net.Socket |
connection |
private Encoding |
encoding |
private java.io.Writer |
encodingWriter |
private HostSpec |
hostSpec |
private byte[] |
int2Buf |
private byte[] |
int4Buf |
private long |
maxResultBuffer |
private int |
minStreamAvailableCheckDelay |
private long |
nextStreamAvailableCheckTime |
private VisibleBufferedInputStream |
pgInput |
private java.io.OutputStream |
pgOutput |
private long |
resultBufferByteCount |
private javax.net.SocketFactory |
socketFactory |
private byte[] |
streamBuffer |
Constructor and Description |
---|
PGStream(javax.net.SocketFactory socketFactory,
HostSpec hostSpec)
Deprecated.
|
PGStream(javax.net.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(java.net.Socket socket)
Switch this stream to using a new socket.
|
void |
clearResultBufferCount()
Method to clear count of byte buffer.
|
void |
close()
Closes the connection.
|
void |
flush()
Flush any pending output to the backend.
|
Encoding |
getEncoding() |
java.io.Writer |
getEncodingWriter()
Get a Writer instance that encodes directly onto the underlying stream.
|
HostSpec |
getHostSpec() |
int |
getNetworkTimeout() |
java.net.Socket |
getSocket() |
javax.net.SocketFactory |
getSocketFactory() |
boolean |
hasMessagePending()
Check for pending backend messages without blocking.
|
private void |
increaseByteCounter(long value)
Method to increase actual count of buffer.
|
boolean |
isClosed() |
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.
|
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.
|
java.lang.String |
receiveString()
Receives a null-terminated string from the backend.
|
java.lang.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(java.io.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(java.lang.String value)
Method to set MaxResultBuffer inside PGStream.
|
void |
setMinStreamAvailableCheckDelay(int delay) |
void |
setNetworkTimeout(int milliseconds) |
void |
skip(int size) |
private final javax.net.SocketFactory socketFactory
private final HostSpec hostSpec
private final byte[] int4Buf
private final byte[] int2Buf
private java.net.Socket connection
private VisibleBufferedInputStream pgInput
private java.io.OutputStream pgOutput
private byte[] streamBuffer
private long nextStreamAvailableCheckTime
private int minStreamAvailableCheckDelay
private Encoding encoding
private java.io.Writer encodingWriter
private long maxResultBuffer
private long resultBufferByteCount
public PGStream(javax.net.SocketFactory socketFactory, HostSpec hostSpec, int timeout) throws java.io.IOException
socketFactory
- socket factory to use when creating socketshostSpec
- the host and port to connect totimeout
- timeout in milliseconds, or 0 if no timeout setjava.io.IOException
- if an IOException occurs below it.@Deprecated public PGStream(javax.net.SocketFactory socketFactory, HostSpec hostSpec) throws java.io.IOException
PGStream(SocketFactory, org.postgresql.util.HostSpec, int)
socketFactory
- socket factoryhostSpec
- the host and port to connect tojava.io.IOException
- if an IOException occurs below it.public HostSpec getHostSpec()
public java.net.Socket getSocket()
public javax.net.SocketFactory getSocketFactory()
public boolean hasMessagePending() throws java.io.IOException
java.io.IOException
- if something wrong happenspublic void setMinStreamAvailableCheckDelay(int delay)
public void changeSocket(java.net.Socket socket) throws java.io.IOException
socket
- the new socket to change tojava.io.IOException
- if something goes wrongpublic Encoding getEncoding()
public void setEncoding(Encoding encoding) throws java.io.IOException
encoding
- the new encoding to usejava.io.IOException
- if something goes wrongpublic java.io.Writer getEncodingWriter() throws java.io.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.
java.io.IOException
- if something goes wrong.public void sendChar(int val) throws java.io.IOException
val
- the character to be sentjava.io.IOException
- if an I/O error occurspublic void sendInteger4(int val) throws java.io.IOException
val
- the integer to be sentjava.io.IOException
- if an I/O error occurspublic void sendInteger2(int val) throws java.io.IOException
val
- the integer to be sentjava.io.IOException
- if an I/O error occurs or val
cannot be encoded in 2 bytespublic void send(byte[] buf) throws java.io.IOException
buf
- The array of bytes to be sentjava.io.IOException
- if an I/O error occurspublic void send(byte[] buf, int siz) throws java.io.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 sentjava.io.IOException
- if an I/O error occurspublic void send(byte[] buf, int off, int siz) throws java.io.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 sentjava.io.IOException
- if an I/O error occurspublic void send(ByteStreamWriter writer) throws java.io.IOException
length < siz
, pad with zeros. If
length > siz
, truncate the array.writer
- the stream writer to invoke to send the bytesjava.io.IOException
- if an I/O error occurspublic int peekChar() throws java.io.IOException
java.io.IOException
- if an I/O Error occurspublic int receiveChar() throws java.io.IOException
java.io.IOException
- if an I/O Error occurspublic int receiveInteger4() throws java.io.IOException
java.io.IOException
- if an I/O error occurspublic int receiveInteger2() throws java.io.IOException
java.io.IOException
- if an I/O error occurspublic java.lang.String receiveString(int len) throws java.io.IOException
len
- the length of the string to receive, in bytes.java.io.IOException
- if something wrong happenspublic EncodingPredictor.DecodeResult receiveErrorString(int len) throws java.io.IOException
len
- the length of the string to receive, in bytes.java.io.IOException
- if something wrong happenspublic java.lang.String receiveString() throws java.io.IOException
java.io.IOException
- if an I/O error occurs, or end of filepublic Tuple receiveTupleV3() throws java.io.IOException, java.lang.OutOfMemoryError, java.sql.SQLException
java.io.IOException
- if a data I/O error occursjava.sql.SQLException
- if read more bytes than set maxResultBufferjava.lang.OutOfMemoryError
public byte[] receive(int siz) throws java.io.IOException
siz
- number of bytes to readjava.io.IOException
- if a data I/O error occurspublic void receive(byte[] buf, int off, int siz) throws java.io.IOException
buf
- buffer to store resultoff
- offset in buffersiz
- number of bytes to readjava.io.IOException
- if a data I/O error occurspublic void skip(int size) throws java.io.IOException
java.io.IOException
public void sendStream(java.io.InputStream inStream, int remaining) throws java.io.IOException
inStream
- the stream to read data fromremaining
- the number of bytes to copyjava.io.IOException
- if a data I/O error occurspublic void flush() throws java.io.IOException
flush
in interface java.io.Flushable
java.io.IOException
- if an I/O error occurspublic void receiveEOF() throws java.sql.SQLException, java.io.IOException
java.io.IOException
- if an I/O error occursjava.sql.SQLException
- if we get something other than an EOFpublic void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
- if an I/O Error occurspublic void setNetworkTimeout(int milliseconds) throws java.io.IOException
java.io.IOException
public int getNetworkTimeout() throws java.io.IOException
java.io.IOException
public void setMaxResultBuffer(java.lang.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 void clearResultBufferCount()
private void increaseByteCounter(long value) throws java.sql.SQLException
value
- size of bytes to add to byte buffer.java.sql.SQLException
- exception returned when result buffer count is bigger than max result
buffer.public boolean isClosed()