1
package org.postgresql;
8
import org.postgresql.*;
9
import org.postgresql.core.*;
10
import org.postgresql.util.*;
13
* $Id: PG_Stream.java,v 1.16 2001/11/19 23:16:45 momjian Exp $
15
* This class is used by Connection & PGlobj for communicating with the
18
* @see java.sql.Connection
20
// This class handles all the Streamed I/O for a org.postgresql connection
21
public class PG_Stream
23
private Socket connection;
24
private InputStream pg_input;
25
private BufferedOutputStream pg_output;
26
private byte[] byte_buf = new byte[8*1024];
28
BytePoolDim1 bytePoolDim1 = new BytePoolDim1();
29
BytePoolDim2 bytePoolDim2 = new BytePoolDim2();
32
* Constructor: Connect to the PostgreSQL back end and return
33
* a stream connection.
35
* @param host the hostname to connect to
36
* @param port the port number that the postmaster is sitting on
37
* @exception IOException if an IOException occurs below it.
39
public PG_Stream(String host, int port) throws IOException
41
connection = new Socket(host, port);
43
// Submitted by Jason Venner <jason@idiom.com> adds a 10x speed
44
// improvement on FreeBSD machines (caused by a bug in their TCP Stack)
45
connection.setTcpNoDelay(true);
47
// Buffer sizes submitted by Sverre H Huseby <sverrehu@online.no>
48
pg_input = new BufferedInputStream(connection.getInputStream(), 8192);
49
pg_output = new BufferedOutputStream(connection.getOutputStream(), 8192);
53
* Sends a single character to the back end
55
* @param val the character to be sent
56
* @exception IOException if an I/O error occurs
58
public void SendChar(int val) throws IOException
60
pg_output.write((byte)val);
64
* Sends an integer to the back end
66
* @param val the integer to be sent
67
* @param siz the length of the integer in bytes (size of structure)
68
* @exception IOException if an I/O error occurs
70
public void SendInteger(int val, int siz) throws IOException
72
byte[] buf = bytePoolDim1.allocByte(siz);
76
buf[siz] = (byte)(val & 0xff);
83
* Send an array of bytes to the backend
85
* @param buf The array of bytes to be sent
86
* @exception IOException if an I/O error occurs
88
public void Send(byte buf[]) throws IOException
94
* Send an exact array of bytes to the backend - if the length
95
* has not been reached, send nulls until it has.
97
* @param buf the array of bytes to be sent
98
* @param siz the number of bytes to be sent
99
* @exception IOException if an I/O error occurs
101
public void Send(byte buf[], int siz) throws IOException
107
* Send an exact array of bytes to the backend - if the length
108
* has not been reached, send nulls until it has.
110
* @param buf the array of bytes to be sent
111
* @param off offset in the array to start sending from
112
* @param siz the number of bytes to be sent
113
* @exception IOException if an I/O error occurs
115
public void Send(byte buf[], int off, int siz) throws IOException
119
pg_output.write(buf, off, ((buf.length - off) < siz ? (buf.length - off) : siz));
120
if ((buf.length - off) < siz)
122
for (i = buf.length - off ; i < siz ; ++i)
130
* Receives a single character from the backend
132
* @return the character received
133
* @exception SQLException if an I/O Error returns
135
public int ReceiveChar() throws SQLException
143
throw new PSQLException("postgresql.stream.eof");
145
catch (IOException e)
147
throw new PSQLException("postgresql.stream.ioerror", e);
153
* Receives an integer from the backend
155
* @param siz length of the integer in bytes
156
* @return the integer received from the backend
157
* @exception SQLException if an I/O error occurs
159
public int ReceiveInteger(int siz) throws SQLException
165
for (int i = 0 ; i < siz ; i++)
167
int b = pg_input.read();
170
throw new PSQLException("postgresql.stream.eof");
171
n = n | (b << (8 * i)) ;
174
catch (IOException e)
176
throw new PSQLException("postgresql.stream.ioerror", e);
182
* Receives an integer from the backend
184
* @param siz length of the integer in bytes
185
* @return the integer received from the backend
186
* @exception SQLException if an I/O error occurs
188
public int ReceiveIntegerR(int siz) throws SQLException
194
for (int i = 0 ; i < siz ; i++)
196
int b = pg_input.read();
199
throw new PSQLException("postgresql.stream.eof");
203
catch (IOException e)
205
throw new PSQLException("postgresql.stream.ioerror", e);
211
* Receives a null-terminated string from the backend. If we don't see a
212
* null, then we assume something has gone wrong.
214
* @param encoding the charset encoding to use.
215
* @return string from back end
216
* @exception SQLException if an I/O error occurs, or end of file
218
public String ReceiveString(Encoding encoding)
222
byte[] rst = byte_buf;
225
int buflen = rst.length;
226
boolean done = false;
231
int c = pg_input.read();
233
throw new PSQLException("postgresql.stream.eof");
246
buflen = (int)(buflen * 2); // 100% bigger
247
byte[] newrst = new byte[buflen];
248
System.arraycopy(rst, 0, newrst, 0, s);
254
catch (IOException e)
256
throw new PSQLException("postgresql.stream.ioerror", e);
258
return encoding.decode(rst, 0, s);
262
* Read a tuple from the back end. A tuple is a two dimensional
265
* @param nf the number of fields expected
266
* @param bin true if the tuple is a binary tuple
267
* @return null if the current response has no more tuples, otherwise
268
* an array of strings
269
* @exception SQLException if a data I/O error occurs
271
public byte[][] ReceiveTuple(int nf, boolean bin) throws SQLException
273
int i, bim = (nf + 7) / 8;
274
byte[] bitmask = Receive(bim);
275
byte[][] answer = bytePoolDim2.allocByte(nf);
280
for (i = 0 ; i < nf ; ++i)
282
boolean isNull = ((bitmask[whichbyte] & whichbit) == 0);
293
int len = ReceiveIntegerR(4);
298
answer[i] = Receive(len);
305
* Reads in a given number of bytes from the backend
307
* @param siz number of bytes to read
308
* @return array of bytes received
309
* @exception SQLException if a data I/O error occurs
311
private byte[] Receive(int siz) throws SQLException
313
byte[] answer = bytePoolDim1.allocByte(siz);
314
Receive(answer, 0, siz);
319
* Reads in a given number of bytes from the backend
321
* @param buf buffer to store result
322
* @param off offset in buffer
323
* @param siz number of bytes to read
324
* @exception SQLException if a data I/O error occurs
326
public void Receive(byte[] b, int off, int siz) throws SQLException
334
int w = pg_input.read(b, off + s, siz - s);
336
throw new PSQLException("postgresql.stream.eof");
340
catch (IOException e)
342
throw new PSQLException("postgresql.stream.ioerror", e);
347
* This flushes any pending output to the backend. It is used primarily
348
* by the Fastpath code.
349
* @exception SQLException if an I/O error occurs
351
public void flush() throws SQLException
357
catch (IOException e)
359
throw new PSQLException("postgresql.stream.flush", e);
364
* Closes the connection
366
* @exception IOException if a IO Error occurs
368
public void close() throws IOException