[jboss-svn-commits] JBL Code SVN: r30018 - labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/arjuna/state.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Nov 5 08:46:24 EST 2009
Author: mark.little at jboss.com
Date: 2009-11-05 08:46:24 -0500 (Thu, 05 Nov 2009)
New Revision: 30018
Modified:
labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/arjuna/state/OutputBuffer.java
Log:
https://jira.jboss.org/jira/browse/JBTM-645
Modified: labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/arjuna/state/OutputBuffer.java
===================================================================
--- labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/arjuna/state/OutputBuffer.java 2009-11-05 13:37:12 UTC (rev 30017)
+++ labs/jbosstm/trunk/ArjunaCore/arjuna/classes/com/arjuna/ats/arjuna/state/OutputBuffer.java 2009-11-05 13:46:24 UTC (rev 30018)
@@ -32,33 +32,51 @@
package com.arjuna.ats.arjuna.state;
import com.arjuna.ats.arjuna.logging.tsLogger;
-import com.arjuna.ats.arjuna.common.*;
import java.io.*;
import java.io.IOException;
/**
- * An OuptputBuffer is used to store various Java types as a byte
- * stream. Similar to java serialization. However, OutputBuffers
- * are compatible with OTSArjuna states.
- *
+ * 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 Mark Little (mark at arjuna.com)
- * @version $Id: OutputBuffer.java 2342 2006-03-30 13:06:17Z $
+ * @version $Id: OutputBuffer.java 2342 2006-03-30 13:06:17Z $
* @since JTS 1.0.
- *
- *
- * @message com.arjuna.ats.arjuna.state.OutputBuffer_1 com.arjuna.ats.arjuna.state.OutputBuffer_1 - Invalid input buffer: byte.
- * @message com.arjuna.ats.arjuna.state.OutputBuffer_2 com.arjuna.ats.arjuna.state.OutputBuffer_2 - Invalid input buffer: bytes.
- * @message com.arjuna.ats.arjuna.state.OutputBuffer_3 com.arjuna.ats.arjuna.state.OutputBuffer_3 - Invalid input buffer: boolean.
- * @message com.arjuna.ats.arjuna.state.OutputBuffer_4 com.arjuna.ats.arjuna.state.OutputBuffer_4 - Invalid input buffer: char.
- * @message com.arjuna.ats.arjuna.state.OutputBuffer_5 com.arjuna.ats.arjuna.state.OutputBuffer_5 - Invalid input buffer: short.
- * @message com.arjuna.ats.arjuna.state.OutputBuffer_6 com.arjuna.ats.arjuna.state.OutputBuffer_6 - Invalid input buffer: int.
- * @message com.arjuna.ats.arjuna.state.OutputBuffer_7 com.arjuna.ats.arjuna.state.OutputBuffer_7 - Invalid input buffer: long.
- * @message com.arjuna.ats.arjuna.state.OutputBuffer_8 com.arjuna.ats.arjuna.state.OutputBuffer_8 - Invalid input buffer: float.
- * @message com.arjuna.ats.arjuna.state.OutputBuffer_9 com.arjuna.ats.arjuna.state.OutputBuffer_9 - Invalid input buffer: double
- * @message com.arjuna.ats.arjuna.state.OutputBuffer_10 com.arjuna.ats.arjuna.state.OutputBuffer_10 - Invalid input buffer: string.
- * @message com.arjuna.ats.arjuna.state.OutputBuffer_11 com.arjuna.ats.arjuna.state.OutputBuffer_11 - Invalid from buffer
+ * @message com.arjuna.ats.arjuna.state.OutputBuffer_1
+ * com.arjuna.ats.arjuna.state.OutputBuffer_1 - Invalid input buffer:
+ * byte.
+ * @message com.arjuna.ats.arjuna.state.OutputBuffer_2
+ * com.arjuna.ats.arjuna.state.OutputBuffer_2 - Invalid input buffer:
+ * bytes.
+ * @message com.arjuna.ats.arjuna.state.OutputBuffer_3
+ * com.arjuna.ats.arjuna.state.OutputBuffer_3 - Invalid input buffer:
+ * boolean.
+ * @message com.arjuna.ats.arjuna.state.OutputBuffer_4
+ * com.arjuna.ats.arjuna.state.OutputBuffer_4 - Invalid input buffer:
+ * char.
+ * @message com.arjuna.ats.arjuna.state.OutputBuffer_5
+ * com.arjuna.ats.arjuna.state.OutputBuffer_5 - Invalid input buffer:
+ * short.
+ * @message com.arjuna.ats.arjuna.state.OutputBuffer_6
+ * com.arjuna.ats.arjuna.state.OutputBuffer_6 - Invalid input buffer:
+ * int.
+ * @message com.arjuna.ats.arjuna.state.OutputBuffer_7
+ * com.arjuna.ats.arjuna.state.OutputBuffer_7 - Invalid input buffer:
+ * long.
+ * @message com.arjuna.ats.arjuna.state.OutputBuffer_8
+ * com.arjuna.ats.arjuna.state.OutputBuffer_8 - Invalid input buffer:
+ * float.
+ * @message com.arjuna.ats.arjuna.state.OutputBuffer_9
+ * com.arjuna.ats.arjuna.state.OutputBuffer_9 - Invalid input buffer:
+ * double
+ * @message com.arjuna.ats.arjuna.state.OutputBuffer_10
+ * com.arjuna.ats.arjuna.state.OutputBuffer_10 - Invalid input buffer:
+ * string.
+ * @message com.arjuna.ats.arjuna.state.OutputBuffer_11
+ * com.arjuna.ats.arjuna.state.OutputBuffer_11 - Invalid from buffer
*/
public class OutputBuffer
@@ -68,453 +86,444 @@
* Create a new buffer.
*/
-public OutputBuffer ()
+ public OutputBuffer()
{
- _valid = true;
+ _valid = true;
- try
- {
- _outputStream = new ByteArrayOutputStream();
- _output = new DataOutputStream(_outputStream);
-
- initBuffer();
- }
- catch (IOException e)
- {
- _valid = false;
- }
+ try
+ {
+ _outputStream = new ByteArrayOutputStream();
+ _output = new DataOutputStream(_outputStream);
+
+ initBuffer();
+ }
+ catch (IOException e)
+ {
+ _valid = false;
+ }
}
/**
- * Create a new buffer with the specified initial size. If required,
- * the internal byte array will be automatically increased in size.
+ * 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)
+ public OutputBuffer(int buffSize)
{
- _valid = true;
+ _valid = true;
- try
- {
- _outputStream = new ByteArrayOutputStream(buffSize);
- _output = new DataOutputStream(_outputStream);
+ try
+ {
+ _outputStream = new ByteArrayOutputStream(buffSize);
+ _output = new DataOutputStream(_outputStream);
- initBuffer();
- }
- catch (IOException e)
- {
- _valid = false;
- }
+ initBuffer();
+ }
+ catch (IOException e)
+ {
+ _valid = false;
+ }
}
/**
* Create a new buffer using the provided byte array.
*/
-public OutputBuffer (byte[] b)
+ public OutputBuffer(byte[] b)
{
- _valid = true;
+ _valid = true;
- try
- {
- _outputStream = new ByteArrayOutputStream(0);
- _output = new DataOutputStream(_outputStream);
+ try
+ {
+ _outputStream = new ByteArrayOutputStream(0);
+ _output = new DataOutputStream(_outputStream);
- _outputStream.write(b, 0, b.length);
-
- initBuffer();
- }
- catch (IOException e)
- {
- _valid = false;
- }
+ _outputStream.write(b, 0, b.length);
+
+ initBuffer();
+ }
+ catch (IOException e)
+ {
+ _valid = false;
+ }
}
/**
- * Create a new OutputBuffer and initialise its state with a
- * copy of the provided buffer.
+ * Create a new OutputBuffer and initialise its state with a copy of the
+ * provided buffer.
*/
-public OutputBuffer (OutputBuffer copyFrom)
+ public OutputBuffer(OutputBuffer copyFrom)
{
- _valid = true;
- _outputStream = null;
- _output = null;
-
- copy(copyFrom);
+ _valid = true;
+ _outputStream = null;
+ _output = null;
+
+ copy(copyFrom);
}
/**
* Is the buffer valid?
*/
-public final synchronized boolean valid ()
+ public final synchronized boolean valid ()
{
- return _valid;
+ return _valid;
}
/**
* Return the byte array used to store data types.
*/
-public final synchronized byte[] buffer ()
+ public final synchronized byte[] buffer ()
{
- return _outputStream.toByteArray();
+ try
+ {
+ _output.flush();
+ }
+ catch (final IOException ex)
+ {
+ // ignore?
+ }
+
+ return _outputStream.toByteArray();
}
/**
- * Return the length of the byte array being used to store
- * data types.
+ * Return the length of the byte array being used to store data types.
*/
-public final synchronized int length ()
+ public final synchronized int length ()
{
- return _outputStream.size();
+ return _outputStream.size();
}
/**
- * Copy the provided OutputBuffer and overwrite the current
- * instance.
+ * Copy the provided OutputBuffer and overwrite the current instance.
*/
-public synchronized void copy (OutputBuffer b)
+ public synchronized void copy (OutputBuffer b)
{
- if (b._valid)
- {
- _valid = true;
+ if (b._valid)
+ {
+ _valid = true;
- try
- {
- _outputStream = new ByteArrayOutputStream(b.length());
- _output = new DataOutputStream(_outputStream);
+ try
+ {
+ _outputStream = new ByteArrayOutputStream(b.length());
+ _output = new DataOutputStream(_outputStream);
- _outputStream.write(b.buffer(), 0, b.length());
-
- initBuffer();
- }
- catch (IOException e)
- {
- _valid = false;
- }
- }
+ _outputStream.write(b.buffer(), 0, b.length());
+
+ initBuffer();
+ }
+ catch (IOException e)
+ {
+ _valid = false;
+ }
+ }
}
/**
* Clear the OutputBuffer and rewind the pack pointer.
*/
-public final synchronized void reset () throws IOException
+ public final synchronized void reset () throws IOException
{
- _outputStream.reset();
- initBuffer();
+ _outputStream.reset();
+ initBuffer();
}
/**
- * Pack a byte.
- * If the buffer is invalid then an IOException is thrown.
+ * Pack a byte. If the buffer is invalid then an IOException is thrown.
*/
-public final synchronized void packByte (byte b) throws IOException
+ public final synchronized void packByte (byte b) throws IOException
{
- if (!_valid)
- throw new IOException(tsLogger.log_mesg.getString("com.arjuna.ats.arjuna.state.OutputBuffer_1"));
-
- packInt((byte) b);
+ if (!_valid)
+ throw new IOException(tsLogger.log_mesg
+ .getString("com.arjuna.ats.arjuna.state.OutputBuffer_1"));
+
+ packInt((byte) b);
}
/**
- * Pack the array of bytes.
- * If the buffer is invalid then an IOException is thrown.
+ * Pack the array of bytes. If the buffer is invalid then an IOException is
+ * thrown.
*/
-public final synchronized void packBytes (byte[] b) throws IOException
+ public final synchronized void packBytes (byte[] b) throws IOException
{
- if (!_valid)
- throw new IOException(tsLogger.log_mesg.getString("com.arjuna.ats.arjuna.state.OutputBuffer_2"));
+ if (!_valid)
+ throw new IOException(tsLogger.log_mesg
+ .getString("com.arjuna.ats.arjuna.state.OutputBuffer_2"));
- packInt(b.length);
+ packInt(b.length);
- if (b.length > 0)
- {
- _output.write(b, 0, b.length);
- realign(b.length);
- }
-
- _output.flush();
- }
+ if (b.length > 0)
+ {
+ _output.write(b, 0, b.length);
+ realign(b.length);
+ }
+ }
/**
- * Pack the boolean.
- * If the buffer is invalid then an IOException is thrown.
+ * Pack the boolean. If the buffer is invalid then an IOException is thrown.
*/
-public final synchronized void packBoolean (boolean b) throws IOException
+ public final synchronized void packBoolean (boolean b) throws IOException
{
- if (!_valid)
- throw new IOException(tsLogger.log_mesg.getString("com.arjuna.ats.arjuna.state.OutputBuffer_3"));
-
- _valid = false;
-
- for (int i = 0; i < 3; i++)
- _output.write(OutputBuffer._byte, 0, 1);
-
- _output.writeBoolean(b);
- _output.flush();
+ if (!_valid)
+ throw new IOException(tsLogger.log_mesg
+ .getString("com.arjuna.ats.arjuna.state.OutputBuffer_3"));
- _valid = true;
+ _valid = false;
+
+ for (int i = 0; i < 3; i++)
+ _output.write(OutputBuffer._byte, 0, 1);
+
+ _output.writeBoolean(b);
+
+ _valid = true;
}
/**
- * Pack the character.
- * If the buffer is invalid then an IOException is thrown.
+ * Pack the character. If the buffer is invalid then an IOException is
+ * thrown.
*/
-public final synchronized void packChar (char c) throws IOException
+ public final synchronized void packChar (char c) throws IOException
{
- if (!_valid)
- throw new IOException(tsLogger.log_mesg.getString("com.arjuna.ats.arjuna.state.OutputBuffer_4"));
-
- packInt((int) c);
+ if (!_valid)
+ throw new IOException(tsLogger.log_mesg
+ .getString("com.arjuna.ats.arjuna.state.OutputBuffer_4"));
+
+ packInt((int) c);
}
/**
- * Pack the short.
- * If the buffer is invalid then an IOException is thrown.
+ * Pack the short. If the buffer is invalid then an IOException is thrown.
*/
-public final synchronized void packShort (short s) throws IOException
+ public final synchronized void packShort (short s) throws IOException
{
- if (!_valid)
- throw new IOException(tsLogger.log_mesg.getString("com.arjuna.ats.arjuna.state.OutputBuffer_5"));
-
- packInt((int) s);
- }
+ if (!_valid)
+ throw new IOException(tsLogger.log_mesg
+ .getString("com.arjuna.ats.arjuna.state.OutputBuffer_5"));
+ packInt((int) s);
+ }
+
/**
- * Pack the integer.
- * If the buffer is invalid then an IOException is thrown.
+ * Pack the integer. If the buffer is invalid then an IOException is thrown.
*/
-public final synchronized void packInt (int i) throws IOException
+ public final synchronized void packInt (int i) throws IOException
{
- if (!_valid)
- throw new IOException(tsLogger.log_mesg.getString("com.arjuna.ats.arjuna.state.OutputBuffer_6"));
-
- _valid = false;
-
- _output.writeInt(i);
- _output.flush();
+ if (!_valid)
+ throw new IOException(tsLogger.log_mesg
+ .getString("com.arjuna.ats.arjuna.state.OutputBuffer_6"));
- _valid = true;
+ _valid = false;
+
+ _output.writeInt(i);
+
+ _valid = true;
}
/**
- * Pack the long.
- * If the buffer is invalid then an IOException is thrown.
+ * Pack the long. If the buffer is invalid then an IOException is thrown.
*/
-public final synchronized void packLong (long l) throws IOException
+ public final synchronized void packLong (long l) throws IOException
{
- if (!_valid)
- throw new IOException(tsLogger.log_mesg.getString("com.arjuna.ats.arjuna.state.OutputBuffer_7"));
-
- _valid = false;
-
- _output.writeLong(l);
- _output.flush();
+ if (!_valid)
+ throw new IOException(tsLogger.log_mesg
+ .getString("com.arjuna.ats.arjuna.state.OutputBuffer_7"));
- _valid = true;
+ _valid = false;
+
+ _output.writeLong(l);
+
+ _valid = true;
}
/**
- * Pack the float.
- * If the buffer is invalid then an IOException is thrown.
+ * Pack the float. If the buffer is invalid then an IOException is thrown.
*/
-public final synchronized void packFloat (float f) throws IOException
+ public final synchronized void packFloat (float f) throws IOException
{
- if (!_valid)
- throw new IOException(tsLogger.log_mesg.getString("com.arjuna.ats.arjuna.state.OutputBuffer_8"));
-
- _valid = false;
-
- _output.writeFloat(f);
- _output.flush();
+ if (!_valid)
+ throw new IOException(tsLogger.log_mesg
+ .getString("com.arjuna.ats.arjuna.state.OutputBuffer_8"));
- _valid = true;
+ _valid = false;
+
+ _output.writeFloat(f);
+
+ _valid = true;
}
/**
- * Pack the double.
- * If the buffer is invalid then an IOException is thrown.
+ * Pack the double. If the buffer is invalid then an IOException is thrown.
*/
-public final synchronized void packDouble (double d) throws IOException
+ public final synchronized void packDouble (double d) throws IOException
{
- if (!_valid)
- throw new IOException(tsLogger.log_mesg.getString("com.arjuna.ats.arjuna.state.OutputBuffer_9"));
-
- _valid = false;
-
- _output.writeDouble(d);
- _output.flush();
+ if (!_valid)
+ throw new IOException(tsLogger.log_mesg
+ .getString("com.arjuna.ats.arjuna.state.OutputBuffer_9"));
- _valid = true;
+ _valid = false;
+
+ _output.writeDouble(d);
+
+ _valid = 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.
+ * 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 s) throws IOException
+ public final synchronized void packString (String s) throws IOException
{
- if (!_valid)
- throw new IOException(tsLogger.log_mesg.getString("com.arjuna.ats.arjuna.state.OutputBuffer_10"));
+ if (!_valid)
+ throw new IOException(tsLogger.log_mesg
+ .getString("com.arjuna.ats.arjuna.state.OutputBuffer_10"));
- int index = -1;
- int sz = 0;
- String dummy = null;
-
- packInt(index);
-
- if (s != null)
- {
- sz = s.length()+1;
- dummy = s+'\0';
+ int sz = 0;
+ String dummy = null;
- packInt(sz);
- }
- else
- {
- packInt(0);
- }
+ if (s != null)
+ {
+ sz = s.length() + 1;
+ dummy = s + '\0';
- _valid = false;
+ }
- if (sz > 0)
- {
- byte[] bytes = dummy.getBytes();
- _output.write(bytes, 0, bytes.length);
- realign(bytes.length);
- }
-
- _output.flush();
+ packInt(sz);
- _valid = true;
+ _valid = false;
+
+ if (sz > 0)
+ {
+ byte[] bytes = dummy.getBytes();
+ _output.write(bytes, 0, bytes.length);
+ realign(bytes.length);
+ }
+
+ _valid = true;
}
/**
- * Pack this buffer into that provided.
- * If the buffer is invalid then an IOException is thrown.
+ * Pack this buffer into that provided. If the buffer is invalid then an
+ * IOException is thrown.
*/
-public synchronized void packInto (OutputBuffer buff) throws IOException
+ public synchronized void packInto (OutputBuffer buff) throws IOException
{
- if (buff == null)
- throw new IOException(tsLogger.log_mesg.getString("com.arjuna.ats.arjuna.state.OutputBuffer_11"));
-
- if (_valid)
- {
- /*
- * pack number of bytes and then pack each byte separately.
- */
+ if (buff == null)
+ throw new IOException(tsLogger.log_mesg
+ .getString("com.arjuna.ats.arjuna.state.OutputBuffer_11"));
- byte[] b = buffer();
- buff.packInt(b.length);
+ if (_valid)
+ {
+ /*
+ * pack number of bytes and then pack each byte separately.
+ */
- for (int i = 0; i < b.length; i++)
- buff.packByte(b[i]);
- }
+ byte[] b = buffer();
+ buff.packInt(b.length);
+
+ for (int i = 0; i < b.length; i++)
+ buff.packByte(b[i]);
+ }
}
/**
* Print out information about this instance.
*/
-public void print (PrintWriter strm)
+ public void print (PrintWriter strm)
{
- if (_valid)
- {
- strm.println("OutputBuffer : \n");
+ if (_valid)
+ {
+ strm.println("OutputBuffer : \n");
- byte[] b = buffer();
-
- for (int i = 0; i < b.length; i++)
- strm.write((char) b[i]);
- }
- else
- strm.println("OutputBuffer : invalid.");
+ byte[] b = buffer();
+
+ for (int i = 0; i < b.length; i++)
+ strm.write((char) b[i]);
+ }
+ else
+ strm.println("OutputBuffer : invalid.");
}
/**
* Reset the pack pointer.
*/
-public final boolean rewrite ()
+ public final boolean rewrite ()
{
- if (!_valid)
- return false;
-
- try
- {
- _outputStream = new ByteArrayOutputStream();
- _output = new DataOutputStream(_outputStream);
-
- initBuffer();
- }
- catch (IOException e)
- {
- _valid = false;
- }
+ if (!_valid)
+ return false;
- return _valid;
+ try
+ {
+ _outputStream = new ByteArrayOutputStream();
+ _output = new DataOutputStream(_outputStream);
+
+ initBuffer();
+ }
+ catch (IOException e)
+ {
+ _valid = false;
+ }
+
+ return _valid;
}
/*
* 1 = 3
*/
-
-private final void realign (int amount) throws IOException
+
+ private final void realign (int amount) throws IOException
{
- if ((amount % OutputBuffer.ALIGNMENT) > 0)
- {
- int excess = OutputBuffer.ALIGNMENT - (amount % OutputBuffer.ALIGNMENT);
+ if ((amount % OutputBuffer.ALIGNMENT) > 0)
+ {
+ int excess = OutputBuffer.ALIGNMENT
+ - (amount % OutputBuffer.ALIGNMENT);
- for (int i = 0; i < excess; i++)
- _output.write(_byte, 0, 1);
-
- _output.flush();
- }
+ for (int i = 0; i < excess; i++)
+ _output.write(_byte, 0, 1);
+ }
}
-
-private final void initBuffer () throws IOException
+
+ private final void initBuffer () throws IOException
{
- String version = "#BE";
+ String version = "#BE";
- _output.writeBytes(version);
- _output.writeBoolean(true);
- _output.writeByte(16);
- _output.writeByte(32);
- _output.writeByte(64);
- _output.writeByte(0);
+ _output.writeBytes(version);
+ _output.writeBoolean(true);
+ _output.writeByte(16);
+ _output.writeByte(32);
+ _output.writeByte(64);
+ _output.writeByte(0);
}
-protected boolean _valid;
-
-protected static final int headerSize = 8;
-protected static final int ALIGNMENT = 4;
-
-private DataOutputStream _output;
-private DataInputStream _input;
-private ByteArrayOutputStream _outputStream;
-private ByteArrayInputStream _inputStream;
-
-private static final byte[] _byte = new byte[1];
-
+ protected boolean _valid;
+
+ protected static final int headerSize = 8;
+
+ protected static final int ALIGNMENT = 4;
+
+ private DataOutputStream _output;
+
+ private ByteArrayOutputStream _outputStream;
+
+ private static final byte[] _byte = new byte[1];
+
}
More information about the jboss-svn-commits
mailing list