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: OutputBuffer.java 2342 2006-03-30 13:06:17Z  $
  */
 
 package com.arjuna.ats.arjuna.state;
 
 
An OuptputBuffer is used to store various Java types as a byte stream. Similar to java serialization. However, OutputBuffers are compatible with OTSArjuna states.

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

    
Create a new buffer.
 
 
     public OutputBuffer()
     {
          = true;
 
         try
         {
              = new ByteArrayOutputStream();
              = new DataOutputStream();
 
             initBuffer();
         }
         catch (IOException e)
         {
              = false;
         }
     }

    
Create a new buffer with the specified initial size. If required, the internal byte array will be automatically increased in size.
 
 
     public OutputBuffer(int buffSize)
     {
          = true;
 
         try
         {
              = new ByteArrayOutputStream(buffSize);
              = new DataOutputStream();
 
             initBuffer();
         }
         catch (IOException e)
         {
              = false;
         }
     }

    
Create a new buffer using the provided byte array.
 
    public OutputBuffer(byte[] b)
    {
         = true;
        try
        {
             = new ByteArrayOutputStream(0);
             = new DataOutputStream();
            .write(b, 0, b.length);
            initBuffer();
        }
        catch (final NullPointerException ex)
        {
             = false;
        }
        catch (IOException e)
        {
             = false;
        }
    }

    
Create a new OutputBuffer and initialise its state with a copy of the provided buffer.
    public OutputBuffer(OutputBuffer copyFrom)
    {
         = true;
         = null;
         = null;
        copy(copyFrom);
    }

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

    
Return the byte array used to store data types.
    public final synchronized byte[] buffer ()
    {
        try
        {
            .flush();
        }
        catch (final IOException ex)
        {
            // ignore?
        }
        
        return .toByteArray();
    }

    
Return the length of the byte array being used to store data types.
    public final synchronized int length ()
    {
        return .size();
    }

    
Copy the provided OutputBuffer and overwrite the current instance.
    public synchronized void copy (OutputBuffer b)
    {
        if (b._valid)
        {
             = true;
            try
            {
                 = new ByteArrayOutputStream(b.length());
                 = new DataOutputStream();
                .write(b.buffer(), 0, b.length());
                initBuffer();
            }
            catch (IOException e)
            {
                 = false;
            }
        }
    }

    
Clear the OutputBuffer and rewind the pack pointer.
    public final synchronized void reset () throws IOException
    {
        .reset();
        initBuffer();
    }

    
Pack a byte. If the buffer is invalid then an IOException is thrown.
    public final synchronized void packByte (byte bthrows IOException
    {
        if (!)
            throw new IOException(..get_state_OutputBuffer_1());
        packInt((byteb);
    }

    
Pack the array of bytes. If the buffer is invalid then an IOException is thrown.
    public final synchronized void packBytes (byte[] bthrows IOException
    {
        if (!)
            throw new IOException(..get_state_OutputBuffer_2());
        packInt(b.length);
        if (b.length > 0)
        {
            .write(b, 0, b.length);
            realign(b.length);
        }
    }

    
Pack the boolean. If the buffer is invalid then an IOException is thrown.
    public final synchronized void packBoolean (boolean bthrows IOException
    {
        if (!)
            throw new IOException(..get_state_OutputBuffer_3());
         = false;
        for (int i = 0; i < 3; i++)
            .write(., 0, 1);
        .writeBoolean(b);
         = true;
    }

    
Pack the character. If the buffer is invalid then an IOException is thrown.
    public final synchronized void packChar (char cthrows IOException
    {
        if (!)
            throw new IOException(..get_state_OutputBuffer_4());
        packInt((intc);
    }

    
Pack the short. If the buffer is invalid then an IOException is thrown.
    public final synchronized void packShort (short sthrows IOException
    {
        if (!)
            throw new IOException(..get_state_OutputBuffer_5());
        packInt((ints);
    }

    
Pack the integer. If the buffer is invalid then an IOException is thrown.
    public final synchronized void packInt (int ithrows IOException
    {
        if (!)
            throw new IOException(..get_state_OutputBuffer_6());
         = false;
        .writeInt(i);
         = true;
    }

    
Pack the long. If the buffer is invalid then an IOException is thrown.
    public final synchronized void packLong (long lthrows IOException
    {
        if (!)
            throw new IOException(..get_state_OutputBuffer_7());
         = false;
        .writeLong(l);
         = true;
    }

    
Pack the float. If the buffer is invalid then an IOException is thrown.
    public final synchronized void packFloat (float fthrows IOException
    {
        if (!)
            throw new IOException(..get_state_OutputBuffer_8());
         = false;
        .writeFloat(f);
         = true;
    }

    
Pack the double. If the buffer is invalid then an IOException is thrown.
    public final synchronized void packDouble (double dthrows IOException
    {
        if (!)
            throw new IOException(..get_state_OutputBuffer_9());
         = false;
        .writeDouble(d);
         = true;
    }

    
Pack the String. Currently different from the C++ version in that a copy of the string will always be packed, even if we have previously seen this object. If the buffer is invalid then an IOException is thrown.
    public final synchronized void packString (String sthrows IOException
    {
        if (!)
            throw new IOException(..get_state_OutputBuffer_10());
        int sz = 0;
        String dummy = null;
        if (s != null)
        {
            sz = s.length() + 1;
            dummy = s + '\0';
        }
        packInt(sz);
         = false;
        if (sz > 0)
        {
            byte[] bytes = dummy.getBytes();
            .write(bytes, 0, bytes.length);
            realign(bytes.length);
        }
         = true;
    }

    
Pack this buffer into that provided. If the buffer is invalid then an IOException is thrown.
    public synchronized void packInto (OutputBuffer buffthrows IOException
    {
        if (buff == null)
            throw new IOException(..get_state_OutputBuffer_11());
        if ()
        {
            /*
             * pack number of bytes and then pack each byte separately.
             */
            buff.packBytes(buffer());
        }
    }

    
Print out information about this instance.
    public void print (PrintWriter strm)
    {
        if ()
        {
            strm.println("OutputBuffer : \n");
            byte[] b = buffer();
            for (int i = 0; i < b.lengthi++)
                strm.write((charb[i]);
        }
        else
            strm.println("OutputBuffer : invalid.");
    }

    
Reset the pack pointer.
    public final boolean rewrite ()
    {
        if (!)
            return false;
        try
        {
             = new ByteArrayOutputStream();
             = new DataOutputStream();
            initBuffer();
        }
        catch (IOException e)
        {
             = false;
        }
        return ;
    }
    /*
     * 1 = 3
     */
    private final void realign (int amountthrows IOException
    {
        if ((amount % .) > 0)
        {
            int excess = .
                    - (amount % .);
            for (int i = 0; i < excessi++)
                .write(, 0, 1);
        }
    }
    private final void initBuffer () throws IOException
    {
        String version = "#BE";
        .writeBytes(version);
        .writeBoolean(true);
        .writeByte(16);
        .writeByte(32);
        .writeByte(64);
        .writeByte(0);
    }
    protected boolean _valid;
    protected static final int headerSize = 8;
    protected static final int ALIGNMENT = 4;
    private DataOutputStream _output;
    private static final byte[] _byte = new byte[1];
New to GrepCode? Check out our FAQ X