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;
 
 
 
 
An XdrOutputStream writes data in XDR format to an underlying java.io.OutputStream.

Author(s):
Alejandro Alberola
David Jencks
Version:
1.0
 
 public class XdrOutputStream {
 
     private static final int BUF_SIZE = 32767;
 
     private static final Logger log = LoggerFactory.getLogger(XdrOutputStream.class,false);
     private static final byte[] textPad = new byte[];
     private static final byte[] zero = new XSQLVAR().encodeInt(0);   // todo
     private static final byte[] minusOne = new XSQLVAR().encodeInt(-1);
     
     static {
         // fill the padding with blanks
         Arrays.fill(,(byte) 32);
     }
 
     private byte[] buf = new byte[];
     private int count;
     private OutputStream out;
 
     protected XdrOutputStream() {
         // empty, for subclasses.
     }
    
    
Create a new instance of XdrOutputStream.

Parameters:
out The underlying OutputStream to write to
 
     public XdrOutputStream(OutputStream out) {
         this. = out;
     }

    
Write a byte buffer to the underlying output stream in XDR format.

Parameters:
buffer The byte buffer to be written
Throws:
java.io.IOException if an error occurs while writing to the underlying output stream
 
     public void writeBuffer(byte[] bufferthrows IOException {
         if (buffer == null)
             writeInt(0);
         else {
             int len = buffer.length;
             writeInt(len);
             write(bufferlen, (4 - len) & 3);
         }
     }

    
Write a blob buffer to the underlying output stream in XDR format.

Parameters:
buffer A byte array containing the blob
Throws:
java.io.IOException if an error occurs while writing to the underlying output stream
    public void writeBlobBuffer(byte[] bufferthrows IOException {
        int len = buffer.length ; // 2 for short for buffer length
        if ( != null && .isDebugEnabled()) .debug("writeBlobBuffer len: " + len);
        if (len > .) {
            throw new IOException(""); //Need a value???
        }
        writeInt(len + 2);
        writeInt(len + 2); //bizarre but true! three copies of the length
        checkBufferSize(2);
        [++] = (byte) ((len >> 0) & 0xff);
        [++] = (byte) ((len >> 8) & 0xff);
        write(bufferlen, (4 - len + 2) & 3); // TODO Is this correct, shouldn't it be (4 - (len + 2)) & 3; see writeSet
    }

    
Write a String to the underlying output stream in XDR format.

Parameters:
s The String to be written
Throws:
java.io.IOException if an error occurs while writing to the underlying output stream
    public void writeString(String sthrows IOException {
        byte[] buffer = s.getBytes();
        writeBuffer(buffer);
    }

    
Write content of the specified string using the specified Java encoding.
    public void writeString(String sString encodingthrows IOException {
        if (encoding != null)
            writeBuffer(s.getBytes(encoding));
        else
            writeBuffer(s.getBytes());
    }

    
Write a set of distinct byte values (ie parameter buffer).

Parameters:
type The type of the parameter value being written, e.g. org.firebirdsql.gds.ISCConstants.isc_tpb_version3
s The set of byte values to be written
Throws:
java.io.IOException if an error occurs while writing to the underlying output stream
    public void writeSet(int typebyte[] sthrows IOException {
        if (s == null) {
            writeInt(1);
            write(type); //e.g. gds.isc_tpb_version3
        } else {
            int len = s.length;
            writeInt(len + 1);
            write(type);
            write(slen, (4 - ( len + 1 )) & 3); // TODO Is this correct; see writeBlobBuffer, that suggests it should be (4 - len + 1) & 3
        }
    }

    
Write an Xdrable to this output stream.

Parameters:
type Type of the Xdrable to be written, e.g. org.firebirdsql.gds.ISCConstants.isc_tpb_version3
item The object to be written
Throws:
java.io.IOException if an error occurs while writing to the underlying output stream
    public void writeTyped(int typeXdrable itemthrows IOException {
        int size;
        if (item == null) {
            writeInt(1);
            write(type); //e.g. gds.isc_tpb_version3
            size = 1;
        } else {
            size = item.getLength() + 1;
            writeInt(size);
            write(type);
            item.write(this);
        }
        // TODO Find out if we need to pad with 0x00 instead
        write(, (4 - size) & 3, 0);
    }
    // 
    // WriteSQLData methods
    // 

    
Write a set of SQL data from a XSQLDA data structure.

Parameters:
xsqlda The datastructure containing the SQL data to be written
Throws:
java.io.IOException if an error occurs while writing to the underlying output stream
    public void writeSQLData(XSQLDA xsqldathrows IOException {
        for (int i = 0; i < xsqlda.sqldi++) {
            XSQLVAR xsqlvar = xsqlda.sqlvar[i];
            if ( != null && .isDebugEnabled()) {
                if ( == null) {
                    .debug("db.out null in writeSQLDatum");
                }
                if (xsqlvar.sqldata == null) {
                    .debug("sqldata null in writeSQLDatum: " + xsqlvar);
                }
                if (xsqlvar.sqldata == null) {
                    .debug("sqldata still null in writeSQLDatum: " + xsqlvar);
                }
            }
            int len = xsqlda.ioLength[i];
            byte[] buffer = xsqlvar.sqldata;
            int tempType = xsqlvar.sqltype & ~1;
            if (tempType == .) {
                write(xsqlvar.sqldata != null ?  : , 4, 0);
            } else
            if (len==0) {
                if (buffer != null) {
                    len = buffer.length;
                    writeInt(len);
                    write(bufferlen, (4 - len) & 3);
                    // sqlind
                    write(, 4, 0);
                }
                else{
                    writeInt(0);
                    // sqlind
                    write(, 4, 0);
                }
            }
            else if (len < 0){
                if (buffer != null) {
                    write(buffer, -len, 0);
                    // sqlind
                    write(,4, 0);
                }
                else{
                    write(, -len, 0);
                    // sqlind
                    write(, 4, 0);
                }
            }
            else {
                // decrement length because it was incremented before
                len--;
                if (buffer != null) {
                    int buflen = buffer.length;
                    if (buflen >=len){
                        write(bufferlen, (4 - len) & 3);
                    }
                    else{
                        write(bufferbuflen, 0);
                        write(lenbuflen, (4 - len) & 3);
                    }
                    // sqlind
                    write(, 4, 0);
                }
                else{
                    write(len, (4 - len) & 3);
                    // sqlind
                    write(, 4, 0);
                }
            }
        }
    }
    //
    // DataOutputStream methods
    // 
    
    
Write a long value to the underlying stream in XDR format.

Parameters:
v The long value to be written
Throws:
java.io.IOException if an error occurs while writing to the underlying output stream
    public void writeLong(long vthrows IOException {
        checkBufferSize(8);
        [++] = (byte) (v >>> 56 & 0xFF);
        [++] = (byte) (v >>> 48 & 0xFF);
        [++] = (byte) (v >>> 40 & 0xFF);
        [++] = (byte) (v >>> 32 & 0xFF);
        [++] = (byte) (v >>> 24 & 0xFF);
        [++] = (byte) (v >>> 16 & 0xFF);
        [++] = (byte) (v >>>  8 & 0xFF);
        [++] = (byte) (v >>>  0 & 0xFF);
    }

    
Write an int value to the underlying stream in XDR format.

Parameters:
v The int value to be written
Throws:
java.io.IOException if an error occurs while writing to the underlying output stream
    public void writeInt(int vthrows IOException {
        checkBufferSize(4);
        [++] = (byte) (v >>> 24);
        [++] = (byte) (v >>> 16);
        [++] = (byte) (v >>>  8);
        [++] = (byte) (v >>>  0);
    }
    //
    // Buffering 
    // If the piece to write is greater than 256 bytes, write it directly
    //

    
Write a byte buffer to the underlying output stream in XDR format

Parameters:
b The byte buffer to be written
len The number of bytes to write
pad The number of (blank) padding bytes to write
Throws:
java.io.IOException if an error occurs while writing to the underlying output stream
    public void write(byte[] bint lenint padthrows IOException {
        if (len > 256 ||  + len + pad >= ){
            if ( > 0)
                .write(, 0, );
            .write(b, 0, len);
            .write(, 0, pad);
             = 0;
        } else {
            checkBufferSize(len + pad);
            if (len > 0) {
                System.arraycopy(b, 0, len);
                 += len;
            }
            if (pad > 0) {
                System.arraycopy(, 0, pad);
                 += pad;
            }
        }
    }

    
Write a single byte to the underlying output stream in XDR format.

Parameters:
b The value to be written, will be truncated to a byte
Throws:
java.io.IOException if an error occurs while writing to the underlying output stream
    public void write(int bthrows IOException {
        checkBufferSize(1);
        [++] = (byte)b;
    }

    
Write an array of bytes to the underlying output stream in XDR format.

Parameters:
b The byte array to be written
Throws:
java.io.IOException if an error occurs while writing to the underlying output stream
    public void write(byte b[]) throws IOException{
        write(bb.length, 0);
    }

    
Flush all buffered data to the underlying output stream.

Throws:
java.io.IOException if an error occurs while writing to the underlying output stream
    public void flush() throws IOException {
        if ( > 0){
            .write(, 0, );
        }
         = 0;
        .flush();
    }

    
Close this stream and the underlying output stream.

Throws:
java.io.IOException if an error occurs while closing the underlying stream
    public void close() throws IOException {
        if ( == null) {
            return;
        }
        IOException firstException = null;
        
        try {
            .flush();
        } catch (IOException ex) {
            firstException = ex;
        } catch (RuntimeException ex) {
            firstException = new IOException("Runtime exception flushing XdrOutputStream during close()");
            firstException.initCause(ex);
        }
        
        try {
            .close();
        } catch (IOException ex) {
            if (firstException == null) {
                firstException = ex;
            } else if ( != null) {
                .debug("Ignoring IOException closing XdrOutputStream; received an earlier exception, ignored exception:"ex);
            }
        } finally {
             = null;
             = null;
        }
        
        if (firstException != null) {
            throw firstException;
        }
    }

    
Ensure that there is room in the buffer for a given number of direct writes to it.

Parameters:
countToWrite The size of write that is being checked.
Throws:
java.io.IOException If a write to the underlying OutputStream fails
    private void checkBufferSize(int countToWritethrows IOException {
        if ( -  <= countToWrite){
            .write(, 0, );
             = 0;
        }
    }
New to GrepCode? Check out our FAQ X