Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Firebird Open Source J2ee connector - jdbc driver
   *
   * Distributable under LGPL license.
   * You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * LGPL License for more details.
  *
  * This file was created by members of the firebird development team.
  * All individual contributions remain the Copyright (C) of those
  * individuals.  Contributors to this file are either listed here or
  * can be obtained from a CVS history command.
  *
  * All rights reserved.
  */
 
 /*
  * The Original Code is the Firebird Java GDS implementation.
  *
  * The Initial Developer of the Original Code is Alejandro Alberola.
  * Portions created by Alejandro Alberola are Copyright (C) 2001
  * Boix i Oltra, S.L. All Rights Reserved.
  *
  */
 
 package org.firebirdsql.gds.impl.wire;
 
XdrInputStream is an input stream for reading in data that is in the XDR format. An XdrInputStream instance is wrapped around an underlying java.io.InputStream.

Author(s):
Alejandro Alberola
David Jencks
Version:
1.0
 
 public class XdrInputStream {
 
     protected final static byte[] pad = new byte[8];
     protected InputStream in = null;
     // Buffer
     protected static int defaultBufferSize = 16384;
     protected byte buf[];
     protected int count;
     protected int pos;
 
     protected XdrInputStream() {
         // empty, for subclasses only
     }
    
    
Create a new instance of XdrInputStream.

Parameters:
in The underlying InputStream to read from
 
     public XdrInputStream(InputStream in) {
          = new byte[];
         this. = in;
     }

    
Read in a byte buffer.

Returns:
The buffer that was read
Throws:
java.io.IOException if an error occurs while reading from the underlying input stream
 
     public byte[] readBuffer() throws IOException {
         int len = readInt();
         byte[] buffer = new byte[len];
         readFully(buffer,0,len);
         readFully(,0,(4 - len) & 3);
         return buffer;
     }

    
Read in a raw array of bytes.

Parameters:
len The number of bytes to read
Returns:
The byte buffer that was read
Throws:
java.io.IOException if an error occurs while reading from the underlying input stream
 
     public byte [] readRawBuffer(int lenthrows IOException {
         byte [] buffer = new byte[len];
         readFully(buffer, 0, len);
         return buffer;
     }
    
    
Read in a String.

Returns:
The String that was read
Throws:
java.io.IOException if an error occurs while reading from the underlying input stream
    public String readString() throws IOException {
        int len = readInt();
        byte[] buffer = new byte[len];
        readFully(buffer,0,len);
        readFully(,0,(4 - len) & 3);
        return new String(buffer);
    }
    //
    // Read SQL data
    //
    //Now returns results in Object[] and in xsqlda.data
    //Nulls are represented by null values in Object array

    
Read a row of SQL data and store it in the results set of a statement.

Parameters:
ioLength array containing the lengths of each column in the data row that is to be read
stmt The statement where the row is to be stored
Throws:
java.io.IOException if an error occurs while reading from the underlying input stream
    public void readSQLData(int[] ioLengthisc_stmt_handle_impl stmtthrows IOException {
        // This only works if not (port->port_flags & PORT_symmetric)		 
        int numCols = ioLength.length;
        byte[][] row = new byte[numCols][];
        byte[] buffer;
        for (int i = 0; i < numColsi++) {
            int len = ioLength[i];
            if (len == 0){
                len = readInt();
                buffer = new byte[len];
                readFully(buffer,0,len);
                readFully(,0,(4 - len) & 3);
            }
            else if (len < 0){
                buffer = new byte[-len];
                readFully(buffer,0,-len);
            }
            else {
                // len is incremented to avoid value 0 so it must be decremented					
                len --;
                buffer = new byte[len];
                readFully(buffer,0,len);
                readFully(,0,(4 - len) & 3);
            }
            if (readInt()==-1)
                buffer = null;
            row[i] = buffer;
        }
        if (stmt != null
            stmt.addRow(row);
    }
    //
    // Substitute DataInputStream
    //


    
Read in a long.

Returns:
The long that was read
Throws:
java.io.IOException if an error occurs while reading from the underlying input stream
    public long readLong() throws IOException {
        return ((long)read() << 56) | ((long)read() << 48) | ((long)read() << 40) | ((long)read() << 32) 
        | ((long)read() << 24) | ((long)read() << 16) | ((long)read() << 8) | ((long)read() << 0);
    }

    
Read in an int.

Returns:
The int that was read
Throws:
java.io.IOException if an error occurs while reading from the underlying input stream
    public int readInt() throws IOException {
        return (read() << 24) | (read() << 16) | (read() << 8) | (read() << 0);
    }
    
    
Read in a short.

Returns:
The short that was read
Throws:
java.io.IOException if an error occurs while reading from the underlying input stream
    public int readShort() throws IOException {
        return (read() << 8) | (read() << 0);
    }

    
Read a given amount of data from the underlying input stream. The data that is read is stored in b, starting from offset off.

Parameters:
b The byte buffer to hold the data that is read
off The offset at which to start storing data in b
len The number of bytes to be read
Throws:
java.io.IOException if an error occurs while reading from the underlying input stream
    public void readFully(byte b[], int offint lenthrows IOException {
        if (len <= -){
            System.arraycopy(bofflen);
             += len;
        }
        else {
            int n = 0;
            while (n < len) {
                if ( <= ){
                    =  = 0;
                   int readn = .read(, 0, );
                   if (readn > 0)
                        = readn;
                   else 
                       throw new EOFException();
                }
                int lenN = len-n;
                int avail =  - ;
                int cnt = (avail < lenN) ? avail : lenN;
                System.arraycopy(boff+ncnt);
                 += cnt;
                n += cnt;
            }
        }
    }
    //
    // Buffering classes (those interface with the real InputStream
    //

    
Read in the next byte of data from the underlying input stream.

Returns:
The value that was read
Throws:
java.io.IOException if an error occurs while reading from the underlying input stream
    public int read() throws IOException {
        if ( >= ){
             =  = 0;
            int readn = .read(, 0, );
            if (readn > 0)
                 = readn;
            else 
                throw new EOFException();
        }
        return [++] & 0xff;
    }

    
Close this input stream and the underlying input stream.

Throws:
java.io.IOException if an error occurs while closing the underlying input stream
    public void close() throws IOException {
        if ( == null)
            return;
        try {
            .close();
        } finally {
             = null;
             = null;
        }
    }
New to GrepCode? Check out our FAQ X