~ubuntu-branches/ubuntu/wily/libpgjava/wily

« back to all changes in this revision

Viewing changes to org/postgresql/core/PGStream.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2008-04-26 22:01:11 UTC
  • mfrom: (3.1.4 gutsy)
  • Revision ID: james.westby@ubuntu.com-20080426220111-yasgxtas5smx2qm3
Tags: 8.2-504-2
* Updated description to mention PostgreSQL 8.3 as supported.
  Closes: #398348
* Removed libpgjava transitional package. Closes: #477557
* Moved debhelper and cdbs from Build-Depends-Indep to Build-Depends.
* Added Homepage, Vcs-Svn and Vcs-Browser fields.
* Added watch file.
* Added myself to Uploaders.
* Removed Stafan and Wolfgang from Uploaders.
* Updated Standards-Version to 3.7.3
* Updated debhelper level to 5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
* Copyright (c) 2003-2005, PostgreSQL Global Development Group
4
4
*
5
5
* IDENTIFICATION
6
 
*   $PostgreSQL: pgjdbc/org/postgresql/core/PGStream.java,v 1.16.2.1 2005/12/02 03:06:01 jurka Exp $
 
6
*   $PostgreSQL: pgjdbc/org/postgresql/core/PGStream.java,v 1.20 2006/10/30 18:30:16 jurka Exp $
7
7
*
8
8
*-------------------------------------------------------------------------
9
9
*/
10
10
package org.postgresql.core;
11
11
 
12
 
import java.io.BufferedInputStream;
13
12
import java.io.BufferedOutputStream;
14
13
import java.io.InputStream;
15
14
import java.io.OutputStream;
37
36
    private final int port;
38
37
 
39
38
    private Socket connection;
40
 
    private InputStream pg_input;
 
39
    private VisibleBufferedInputStream pg_input;
41
40
    private OutputStream pg_output;
42
41
    private byte[] streamBuffer;
43
42
 
103
102
        connection.setTcpNoDelay(true);
104
103
 
105
104
        // Buffer sizes submitted by Sverre H Huseby <sverrehu@online.no>
106
 
        pg_input = new BufferedInputStream(connection.getInputStream(), 8192);
 
105
        pg_input = new VisibleBufferedInputStream(connection.getInputStream(), 8192);
107
106
        pg_output = new BufferedOutputStream(connection.getOutputStream(), 8192);
108
107
 
109
108
        if (encoding != null)
268
267
     */
269
268
    public int ReceiveIntegerR(int siz) throws IOException
270
269
    {
 
270
        if (!pg_input.ensureBytes(siz)) {
 
271
            throw new EOFException();
 
272
        }
271
273
        int n = 0;
272
274
 
273
275
        for (int i = 0 ; i < siz ; i++)
274
276
        {
275
 
            int b = pg_input.read();
 
277
            int b = pg_input.readRaw() & 0xFF;
276
278
 
277
 
            if (b < 0)
278
 
                throw new EOFException();
279
279
            n = b | (n << 8);
280
280
        }
281
281
 
290
290
 
291
291
    }
292
292
 
293
 
    private byte[] byte_buf = new byte[8*1024];
294
 
 
295
293
    /**
296
294
     * Receives a fixed-size string from the backend.
297
295
     *
299
297
     * @return the decoded string
300
298
     */
301
299
    public String ReceiveString(int len) throws IOException {
302
 
        if (len > byte_buf.length)
303
 
            byte_buf = new byte[len];
 
300
        if (!pg_input.ensureBytes(len)) {
 
301
            throw new EOFException();
 
302
        }
304
303
 
305
 
        Receive(byte_buf, 0, len);
306
 
        return encoding.decode(byte_buf, 0, len);
 
304
        String res = encoding.decode(pg_input.getBuffer(), pg_input.getIndex(),
 
305
                                     len);
 
306
        pg_input.skip(len);
 
307
        return res;
307
308
    }
308
309
 
309
310
    /**
315
316
     */
316
317
    public String ReceiveString() throws IOException
317
318
    {
318
 
        int i = 0;
319
 
        byte[] rst = byte_buf;
320
 
        int buflen = rst.length;
321
 
 
322
 
        while (true)
323
 
        {
324
 
            int c = pg_input.read();
325
 
 
326
 
            if (c < 0)
327
 
                throw new EOFException();
328
 
 
329
 
            if (c == 0)
330
 
                break;
331
 
 
332
 
            if (i == buflen)
333
 
            {
334
 
                // Grow the buffer.
335
 
                buflen *= 2;     // 100% bigger
336
 
                if (buflen <= 0) // Watch for overflow
337
 
                    throw new IOException("Impossibly long string");
338
 
 
339
 
                byte[] newrst = new byte[buflen];
340
 
                System.arraycopy(rst, 0, newrst, 0, i);
341
 
                rst = newrst;
342
 
            }
343
 
 
344
 
            rst[i++] = (byte)c;
345
 
        }
346
 
 
347
 
        return encoding.decode(rst, 0, i);
 
319
        int len = pg_input.scanCStringLength();
 
320
        String res = encoding.decode(pg_input.getBuffer(), pg_input.getIndex(),
 
321
                                     len - 1);
 
322
        pg_input.skip(len);
 
323
        return res;
348
324
    }
349
325
 
350
326
    /**
356
332
     * an array of bytearrays
357
333
     * @exception IOException if a data I/O error occurs
358
334
     */
359
 
    public byte[][] ReceiveTupleV3() throws IOException
 
335
    public byte[][] ReceiveTupleV3() throws IOException, OutOfMemoryError
360
336
    {
361
337
        //TODO: use l_msgSize
362
338
        int l_msgSize = ReceiveIntegerR(4);
364
340
        int l_nf = ReceiveIntegerR(2);
365
341
        byte[][] answer = new byte[l_nf][];
366
342
 
 
343
        OutOfMemoryError oom = null;
367
344
        for (i = 0 ; i < l_nf ; ++i)
368
345
        {
369
346
            int l_size = ReceiveIntegerR(4);
370
 
            if (l_size != -1)
371
 
                answer[i] = Receive(l_size);
 
347
            if (l_size != -1) {
 
348
                try {
 
349
                    answer[i] = new byte[l_size];
 
350
                    Receive(answer[i], 0, l_size);
 
351
                } catch(OutOfMemoryError oome) {
 
352
                    oom = oome;
 
353
                    Skip(l_size);
 
354
                }
 
355
            }
372
356
        }
373
357
 
 
358
        if (oom != null)
 
359
            throw oom;
 
360
 
374
361
        return answer;
375
362
    }
376
363
 
385
372
     * an array of bytearrays
386
373
     * @exception IOException if a data I/O error occurs
387
374
     */
388
 
    public byte[][] ReceiveTupleV2(int nf, boolean bin) throws IOException
 
375
    public byte[][] ReceiveTupleV2(int nf, boolean bin) throws IOException, OutOfMemoryError
389
376
    {
390
377
        int i, bim = (nf + 7) / 8;
391
378
        byte[] bitmask = Receive(bim);
394
381
        int whichbit = 0x80;
395
382
        int whichbyte = 0;
396
383
 
 
384
        OutOfMemoryError oom = null;
397
385
        for (i = 0 ; i < nf ; ++i)
398
386
        {
399
387
            boolean isNull = ((bitmask[whichbyte] & whichbit) == 0);
410
398
                    len -= 4;
411
399
                if (len < 0)
412
400
                    len = 0;
413
 
                answer[i] = Receive(len);
 
401
                try {
 
402
                    answer[i] = new byte[len];
 
403
                    Receive(answer[i], 0, len);
 
404
                } catch(OutOfMemoryError oome) {
 
405
                    oom = oome;
 
406
                    Skip(len);
 
407
                }
414
408
            }
415
409
        }
 
410
 
 
411
        if (oom != null)
 
412
            throw oom;
 
413
 
416
414
        return answer;
417
415
    }
418
416
 
451
449
        }
452
450
    }
453
451
 
 
452
    public void Skip(int size) throws IOException {
 
453
        long s = 0;
 
454
        while (s < size) {
 
455
            s += pg_input.skip(size - s);
 
456
        }
 
457
    }
 
458
 
 
459
 
454
460
    /**
455
461
     * Copy data from an input stream to the connection.
456
462
     *