Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors 
   * as indicated by the @author tags. 
   * See the copyright.txt in the distribution for a
   * full listing of individual contributors. 
   * This copyrighted material is made available to anyone wishing to use,
   * modify, copy, or redistribute it subject to the terms and conditions
   * of the GNU Lesser General Public License, v. 2.1.
  * This program is distributed in the hope that it will be useful, but WITHOUT A 
  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public License,
  * v.2.1 along with this distribution; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
  * MA  02110-1301, USA.
  * 
  * (C) 2005-2006,
  * @author JBoss Inc.
  */
 /*
  * Copyright (C) 1998, 1999, 2000, 2001,
  *
  * Arjuna Solutions Limited,
  * Newcastle upon Tyne,
  * Tyne and Wear,
  * UK.  
  *
  * $Id: InputBuffer.java 2342 2006-03-30 13:06:17Z  $
  */
 
 package com.arjuna.ats.arjuna.state;
 
 
An InputBuffer is used to retrieve various Java types from a byte stream created using an OutputBuffer. Similar to java serialization. However, InputBuffers are compatible with OTSArjuna states.

Author(s):
Mark Little (mark@arjuna.com)
Version:
$Id: InputBuffer.java 2342 2006-03-30 13:06:17Z $
Since:
JTS 1.0.
 
 
 public class InputBuffer
 {

    
Create a new buffer.
 
 
     public InputBuffer()
     {
          = null;
          = false;
          = null;
          = null;
     }

    
Create our own copy of the byte array.
 
 
     public InputBuffer(byte b[])
     {
          = true;
 
          = new byte[b.length];
 
         System.arraycopy(b, 0, , 0, b.length);
 
         try
         {
              = new ByteArrayInputStream();
              = new DataInputStream();
 
             skipHeader();
         }
         catch (IOException e)
         {
              = false;
         }
     }

    
Create a new buffer and copy the provided one.
 
 
     public InputBuffer(InputBuffer buff)
     {
          = null;
          = false;
          = null;
         = null;
        copy(buff);
    }

    
Is the buffer valid?
    public final synchronized boolean valid ()
    {
        return ;
    }

    
Copy the existing buffer.
    public synchronized void copy (InputBuffer buff)
    {
        if (buff._valid)
        {
             = new byte[buff._byteArray.length];
             = true;
            System.arraycopy(buff._byteArray, 0, , 0,
                    buff._byteArray.length);
            try
            {
                 = new ByteArrayInputStream();
                 = new DataInputStream();
                skipHeader();
            }
            catch (IOException e)
            {
                 = false;
            }
        }
    }

    
Return the length of the byte buffer.
    public final synchronized int length ()
    {
        return (( == null) ? 0 : .);
    }

    
Return the internal byte buffer.
    public final synchronized byte[] buffer ()
    {
        return ;
    }

    
Set the buffer to be used by this instance.
    public final synchronized void setBuffer (byte[] b)
    {
         = new byte[b.length];
        System.arraycopy(b, 0, , 0, b.length);
        try
        {
             = new ByteArrayInputStream();
             = new DataInputStream();
             = true;
            skipHeader();
        }
        catch (Exception e)
        {
             = null;
             = false;
        }
    }

    
Unpack a byte from the stream. If the next item in the buffer is not of the right type then an IOException is thrown.
    public final synchronized byte unpackByte () throws IOException
    {
        if (!)
            throw new IOException(..get_state_InputBuffer_1());
        int i = unpackInt();
        return (bytei;
    }

    
Unpack the next byte array from the stream. If the next item in the buffer is not of the right type then an IOException is thrown.
    public final synchronized byte[] unpackBytes () throws IOException
    {
        if (!)
            throw new IOException(..get_state_InputBuffer_2());
        int size = unpackInt();
        byte b[] = new byte[size];
        if (size > 0)
        {
            .read(b, 0, size);
            realign(size);
        }
        return b;
    }

    
Unpack a boolean from the stream. If the next item in the buffer is not of the right type then an IOException is thrown.
    public final synchronized boolean unpackBoolean () throws IOException
    {
        if (!)
            throw new IOException(..get_state_InputBuffer_3());
         = false;
        .skip(3);
        boolean b = .readBoolean();
         = true;
        return b;
    }

    
Unpack a character from the stream. If the next item in the buffer is not of the right type then an IOException is thrown.
    public final synchronized char unpackChar () throws IOException
    {
        if (!)
            throw new IOException(..get_state_InputBuffer_4());
        int i = unpackInt();
        return (chari;
    }

    
Unpack a short from the stream. If the next item in the buffer is not of the right type then an IOException is thrown.
    public final synchronized short unpackShort () throws IOException
    {
        if (!)
            throw new IOException(..get_state_InputBuffer_5());
        int i = unpackInt();
        return (shorti;
    }

    
Unpack an integer from the stream. If the next item in the buffer is not of the right type then an IOException is thrown.
    public final synchronized int unpackInt () throws IOException
    {
        if (!)
            throw new IOException(..get_state_InputBuffer_6());
         = false;
        int i = .readInt();
         = true;
        return i;
    }

    
Unpack a long from the stream. If the next item in the buffer is not of the right type then an IOException is thrown.
    public final synchronized long unpackLong () throws IOException
    {
        if (!)
            throw new IOException(..get_state_InputBuffer_7());
         = false;
        long l = .readLong();
         = true;
        return l;
    }

    
Unpack a float from the stream. If the next item in the buffer is not of the right type then an IOException is thrown.
    public final synchronized float unpackFloat () throws IOException
    {
        if (!)
            throw new IOException(..get_state_InputBuffer_8());
         = false;
        float f = .readFloat();
         = true;
        return f;
    }

    
Unpack a double from the stream. If the next item in the buffer is not of the right type then an IOException is thrown.
    public final synchronized double unpackDouble () throws IOException
    {
        if (!)
            throw new IOException(..get_state_InputBuffer_9());
         = false;
        double d = .readDouble();
         = true;
        return d;
    }

    
Unpack a String from the stream. If the next item in the buffer is not of the right type then an IOException is thrown. Currently different from the C++ version in that a distinct new instance will always be returned, rather than a reference to a previously returned object in the case of the "same" string.
    public final synchronized String unpackString () throws IOException
    {
        if (!)
            throw new IOException(..get_state_InputBuffer_10());
        int length = unpackInt();
        if (length == 0)
            return null;
        /*
         * We don't need the '\0' character which Arjuna puts in the buffer. We
         * only put it in for compatibility with C++. So ignore it.
         */
        byte[] b = new byte[length - 1];
        byte[] dummy = new byte[1];
        .read(b, 0, length - 1);
        .read(dummy, 0, 1);
        realign(length);
        return new String(b);
    }

    
Unpack a buffer from the provided buffer, and initialise this instance with it. If the next item in the buffer is not of the right type then an IOException is thrown.
    public synchronized void unpackFrom (InputBuffer buffthrows IOException
    {
        if (buff == null)
            throw new IOException(..get_state_InputBuffer_11());
         = false;
        /*
         * unpack number of bytes, then create new byte array and unpack each
         * byte separately.
         */
         = buff.unpackBytes();
         = true;
        try
        {
             = new ByteArrayInputStream();
             = new DataInputStream();
            skipHeader();
        }
        catch (IOException e)
        {
             = false;
        }
    }

    
Reset the read pointer for this buffer.
    public final boolean reread ()
    {
        if (!)
            return false;
        try
        {
             = new ByteArrayInputStream();
             = new DataInputStream();
            skipHeader();
        }
        catch (IOException e)
        {
             = false;
        }
        return ;
    }

    
Print information about this instance.
    public void print (PrintWriter strm)
    {
        if ()
        {
            strm.println("InputBuffer : \n");
            strm.println("InputBuffer : \n");
            for (int i = 0; i < .i++)
                strm.write((char[i]);
        }
        else
            strm.println("InputBuffer : invalid.");
    }
    private final void realign (int amountthrows IOException
    {
        if ((amount % .) > 0)
        {
            int excess = .
                    - (amount % .);
            if (.available() < excess)
                excess = .available();
            .skipBytes(excess);
        }
    }
    private final void skipHeader () throws IOException
    {
        .skip(.); // sizeof buffer header
    }
    protected boolean _valid;
    private DataInputStream _input;
    private byte[] _byteArray;
New to GrepCode? Check out our FAQ X