|
From: Michael R. <mr...@us...> - 2004-08-12 12:54:34
|
Update of /cvsroot/openorb/OpenORB/src/main/org/openorb/orb/io In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv6600/OpenORB/src/main/org/openorb/orb/io Modified Files: MarshalBuffer.java Scrap.java StorageBuffer.java Log Message: Various improvements, see WHATSNEW for details Index: MarshalBuffer.java =================================================================== RCS file: /cvsroot/openorb/OpenORB/src/main/org/openorb/orb/io/MarshalBuffer.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -d -r1.8 -r1.9 --- MarshalBuffer.java 19 Feb 2004 07:21:31 -0000 1.8 +++ MarshalBuffer.java 12 Aug 2004 12:54:24 -0000 1.9 @@ -8,8 +8,10 @@ package org.openorb.orb.io; +import org.apache.avalon.framework.logger.Logger; import org.omg.CORBA.OctetSeqHolder; import org.omg.CORBA.IntHolder; +import org.openorb.orb.util.Trace; /** * MarshalBuffers serve as a sink for data to be marshaled into. Fragmentation @@ -20,6 +22,35 @@ */ public class MarshalBuffer { + private static final boolean DEBUG_ENABLED = + Boolean.getBoolean( System.getProperty( "openorb.debug.enabled" ) ); + + private Logger m_logger; + + private org.omg.CORBA.SystemException m_cancel_exception = null; + + private Scrap m_head; + private Scrap m_tail; + + private OctetSeqHolder m_last_buffer = null; + + private boolean m_allow_fragment = false; + private boolean m_in_fragment = false; + + private boolean m_header_fragment = true; + private HeaderData m_last_header = null; + + private boolean m_block_fragment = true; + private BlockData m_last_block = null; + + // informed when size grows and buffer is closed. + private Listener m_listener = null; + private Object m_listener_cookie = null; + private boolean m_in_listener = false; + + // dummy buffer to marshal data into once cancel has been called. + private byte [] m_ignore_buffer = null; + /** * Construct marshal buffer without listener. Use the fragment and * lastFragment methods to extract data from the buffer. @@ -28,10 +59,10 @@ { m_tail = new Scrap(); m_head = m_tail; - m_tail.m_fBuffer = new byte[ Scrap.DEFAULT_SCRAP ]; + m_tail.m_fBuffer = new byte[ Scrap.SCRAP_SIZE_DEFAULT ]; m_tail.m_fOffset = 0; m_tail.m_fLength = 0; - m_tail.m_fMode = Scrap.MODE_NORMAL; + m_tail.m_fMode = Scrap.SCRAP_MODE_NORMAL; m_tail.m_fPosition = 0; } @@ -49,6 +80,30 @@ } /** + * Enable logging for this class. + * + * @param logger The logger instance. + */ + public void enableLogging( Logger logger ) + { + m_logger = logger; + } + + /** + * Return the logger for this class. + * + * @return The logger instance. + */ + protected Logger getLogger() + { + if ( null == m_logger ) + { + Trace.signalIllegalCondition( null, "The logger has not been set!" ); + } + return m_logger; + } + + /** * Returns true if not connected to a listener. To get the data from * the buffer use lastFragment() */ @@ -58,8 +113,10 @@ } /** - * count of all bytes inserted into the buffer, including previous - * fragments + * Counts of all bytes inserted into the buffer, including previous + * fragments. + * + * @return The number of all bytes inserted into this buffer. */ public int size() { @@ -71,7 +128,9 @@ } /** - * count of all bytes available for extracting into a fragment. + * Count of all bytes available for extracting into a fragment. + * + * @return The bumber fo bytes available for a fragment. */ public int available() { @@ -103,9 +162,9 @@ } /** - * Alocate space at end of buffer. The dest scrap is modified so - * it's contents contain the allocated space. The allocated space is - * considered to be scratch space, it's buffer is only avalable + * Alocate space at end of buffer. The destination scrap is modified so + * its contents contain the allocated space. The allocated space is + * considered to be scratch space, its buffer is only available * until the next call. * * @param buf Out parameter, holds pointer to scratch space on @@ -116,6 +175,12 @@ */ public void alloc( OctetSeqHolder buf, IntHolder off, int len ) { + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isMedium() ) + { + getLogger().debug( "Entering method 'alloc(" + buf.value + ", " + off.value + ", " + + len + ")'..." ); + } + if ( !prealloc() ) { if ( m_ignore_buffer == null || m_ignore_buffer.length < len ) @@ -142,6 +207,11 @@ m_tail.m_fLength += len; m_tail.m_fPosition += len; + + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isHigh() ) + { + getLogger().debug( "Found space in tail element: " + m_tail ); + } } else { @@ -149,19 +219,19 @@ // newly allocated space in new tail Scrap nex = new Scrap(); - if ( len > Scrap.DEFAULT_SCRAP ) + if ( len > Scrap.SCRAP_SIZE_DEFAULT ) { nex.m_fBuffer = new byte[ len ]; } else { - nex.m_fBuffer = new byte[ Scrap.DEFAULT_SCRAP ]; + nex.m_fBuffer = new byte[ Scrap.SCRAP_SIZE_DEFAULT ]; } nex.m_fOffset = 0; nex.m_fLength = len; - nex.m_fMode = Scrap.MODE_NORMAL; + nex.m_fMode = Scrap.SCRAP_MODE_NORMAL; nex.m_fPosition = m_tail.m_fPosition + len; @@ -172,6 +242,11 @@ m_tail.m_fNext = nex; m_tail = nex; + + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isHigh() ) + { + getLogger().debug( "Added new element: " + nex ); + } } m_last_buffer = buf; @@ -184,6 +259,12 @@ */ public void append( byte [] buf, int off, int len ) { + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isMedium() ) + { + getLogger().debug( "Entering method 'append(" + buf + ", " + off + ", " + + len + ")'..." ); + } + if ( !prealloc() ) { return; @@ -196,9 +277,9 @@ { return; } - if ( len < Scrap.DEFAULT_SCRAP ) + if ( len < Scrap.SCRAP_SIZE_DEFAULT ) { - // append by copy. Not worth doing otherwise realy. + // append by copy. Not worth doing otherwise, really! if ( m_tail.m_fBuffer.length - m_tail.m_fLength - m_tail.m_fOffset >= len ) { // tail contains entire append buffer. @@ -207,6 +288,11 @@ m_tail.m_fLength += len; m_tail.m_fPosition += len; + + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isHigh() ) + { + getLogger().debug( "Found space in tail element: " + m_tail ); + } } else { @@ -219,16 +305,21 @@ m_tail.m_fPosition += rem; Scrap nex = new Scrap(); - nex.m_fBuffer = new byte[ Scrap.DEFAULT_SCRAP ]; + nex.m_fBuffer = new byte[ Scrap.SCRAP_SIZE_DEFAULT ]; nex.m_fOffset = 0; nex.m_fLength = len - rem; - nex.m_fMode = Scrap.MODE_NORMAL; + nex.m_fMode = Scrap.SCRAP_MODE_NORMAL; nex.m_fPosition = m_tail.m_fPosition + len - rem; System.arraycopy( buf, off + rem, nex.m_fBuffer, 0, len - rem ); m_tail.m_fNext = nex; m_tail = nex; + + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isHigh() ) + { + getLogger().debug( "Added new element: " + nex ); + } } return; @@ -245,29 +336,34 @@ nex.m_fBuffer = m_tail.m_fBuffer; nex.m_fOffset = m_tail.m_fOffset; nex.m_fLength = 0; - nex.m_fMode = Scrap.MODE_NORMAL; + nex.m_fMode = Scrap.SCRAP_MODE_NORMAL; nex.m_fPosition = m_tail.m_fPosition + len; m_tail.m_fBuffer = buf; m_tail.m_fOffset = off; m_tail.m_fLength = len; - m_tail.m_fMode = Scrap.MODE_READONLY; + m_tail.m_fMode = Scrap.SCRAP_MODE_READONLY; m_tail.m_fPosition = nex.m_fPosition; m_tail.m_fNext = nex; m_tail = nex; + + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isHigh() ) + { + getLogger().debug( "Added new element before 0-length tail: " + nex ); + } } else { // make current tail shared, append a readonly scrap then append // a normal scrap with the rest of the old tail's buffer. - m_tail.m_fMode = Scrap.MODE_SHARED; + m_tail.m_fMode = Scrap.SCRAP_MODE_SHARED; Scrap ro = new Scrap(); ro.m_fBuffer = buf; ro.m_fOffset = off; ro.m_fLength = len; - ro.m_fMode = Scrap.MODE_READONLY; + ro.m_fMode = Scrap.SCRAP_MODE_READONLY; ro.m_fPosition = m_tail.m_fPosition + len; Scrap nex = new Scrap(); @@ -275,11 +371,16 @@ nex.m_fOffset = m_tail.m_fOffset + m_tail.m_fLength; nex.m_fLength = 0; nex.m_fPosition = ro.m_fPosition; - nex.m_fMode = Scrap.MODE_NORMAL; + nex.m_fMode = Scrap.SCRAP_MODE_NORMAL; m_tail.m_fNext = ro; ro.m_fNext = nex; m_tail = nex; + + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isHigh() ) + { + getLogger().debug( "Added two elements (RO, SHARED) to tail: " + ro + ", " + nex ); + } } postalloc(); @@ -287,9 +388,16 @@ /** * Insert padding. + * + * @param len The number of bytes to add. */ public void pad( int len ) { + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isMedium() ) + { + getLogger().debug( "Entering method 'pad(" + len + ")'..." ); + } + if ( !prealloc() ) { return; @@ -309,6 +417,11 @@ // all the padding fits in the current scrap m_tail.m_fLength += len; m_tail.m_fPosition += len; + + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isHigh() ) + { + getLogger().debug( "Found space in tail element: " + m_tail ); + } } else { @@ -318,14 +431,19 @@ // and the rest into a new scrap. Scrap nex = new Scrap(); - nex.m_fBuffer = new byte[ Scrap.DEFAULT_SCRAP ]; + nex.m_fBuffer = new byte[ Scrap.SCRAP_SIZE_DEFAULT ]; nex.m_fOffset = 0; nex.m_fLength = len - rem; - nex.m_fMode = Scrap.MODE_NORMAL; + nex.m_fMode = Scrap.SCRAP_MODE_NORMAL; nex.m_fPosition = m_tail.m_fPosition + nex.m_fLength; m_tail.m_fNext = nex; m_tail = nex; + + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isHigh() ) + { + getLogger().debug( "Added new element: " + nex ); + } } postalloc(); @@ -345,6 +463,12 @@ */ public void addHeader( HeaderGenerator gen, int len, boolean frag, Object cookie ) { + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isMedium() ) + { + getLogger().debug( "Entering method 'addHeader(" + gen + ", " + len + ", " + + frag + ", " + cookie + ")'..." ); + } + if ( m_in_listener && !m_in_fragment ) { throw new IllegalStateException( "Cannot modify buffer while calling listener" ); @@ -407,6 +531,12 @@ */ public void beginBlock( BlockGenerator gen, int len, boolean frag, Object cookie ) { + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isMedium() ) + { + getLogger().debug( "Entering method 'beginBlock(" + gen + ", " + len + ", " + + frag + ", " + cookie + ")'..." ); + } + if ( m_in_listener && !m_in_fragment ) { throw new IllegalStateException( "Cannot modify buffer while calling listener" ); @@ -461,6 +591,11 @@ */ public void endBlock() { + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isMedium() ) + { + getLogger().debug( "Entering method 'endBlock()'..." ); + } + if ( m_in_listener && !m_in_fragment ) { throw new IllegalStateException( "Cannot modify buffer while calling listener" ); @@ -485,6 +620,11 @@ */ public void close() { + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isMedium() ) + { + getLogger().debug( "Entering method 'close()'..." ); + } + if ( m_in_listener ) { throw new IllegalStateException( "Cannot close buffer while calling listener." ); @@ -500,8 +640,14 @@ if ( m_listener != null ) { m_in_listener = true; - m_listener.bufferClosed( this, m_tail.m_fPosition, m_listener_cookie ); - m_in_listener = false; + try + { + m_listener.bufferClosed( this, m_tail.m_fPosition, m_listener_cookie ); + } + finally + { + m_in_listener = false; + } } m_head = null; @@ -517,6 +663,11 @@ */ public void cancel( org.omg.CORBA.SystemException ex ) { + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isMedium() ) + { + getLogger().debug( "Entering method 'cancel(" + ex + ")'..." ); + } + if ( !prealloc() ) { return; @@ -524,7 +675,6 @@ if ( m_listener != null ) { m_in_listener = true; - try { m_listener.bufferCanceled( this, ex, m_listener_cookie ); @@ -533,8 +683,10 @@ { m_cancel_exception = sex; } - - m_in_listener = false; + finally + { + m_in_listener = false; + } } else { @@ -554,11 +706,16 @@ } /** - * Prepare fragment. The returned storage buffer will be exactly the length - * specified. + * Prepare a fragment message. + * The returned storage buffer will be exactly the length specified. */ public StorageBuffer fragment( int len ) { + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isMedium() ) + { + getLogger().debug( "Entering method 'fragment(" + len + ")'..." ); + } + if ( !prealloc() ) { return null; @@ -628,7 +785,7 @@ { int olap = mid.m_fPosition - endpos; nexthead = new Scrap(); - nexthead.m_fMode = Scrap.MODE_SHARED | mid.m_fMode; + nexthead.m_fMode = Scrap.SCRAP_MODE_SHARED | mid.m_fMode; nexthead.m_fBuffer = mid.m_fBuffer; nexthead.m_fPosition = mid.m_fPosition; nexthead.m_fOffset = mid.m_fOffset + mid.m_fLength - olap; @@ -638,7 +795,7 @@ mid.m_fLength = mid.m_fLength - olap; mid.m_fPosition = endpos; mid.m_fNext = null; - mid.m_fMode = Scrap.MODE_SHARED | mid.m_fMode; + mid.m_fMode = Scrap.SCRAP_MODE_SHARED | mid.m_fMode; } mid = null; @@ -647,10 +804,10 @@ // now begin the next message. m_tail = new Scrap(); m_head = m_tail; - m_tail.m_fBuffer = new byte[ Scrap.DEFAULT_SCRAP ]; + m_tail.m_fBuffer = new byte[ Scrap.SCRAP_SIZE_DEFAULT ]; m_tail.m_fOffset = 0; m_tail.m_fLength = 0; - m_tail.m_fMode = Scrap.MODE_NORMAL; + m_tail.m_fMode = Scrap.SCRAP_MODE_NORMAL; m_tail.m_fPosition = endpos; // call begin block on all the header blocks @@ -710,10 +867,16 @@ } /** - * Return the last fragment. This also closes the buffer. + * Return the last fragment. The size of the last fragment will be less + * than the default MAX_FRAGMENT_SIZE. This also closes the buffer. */ public StorageBuffer lastFragment() { + if ( DEBUG_ENABLED && getLogger().isDebugEnabled() && Trace.isMedium() ) + { + getLogger().debug( "Entering method 'lastFragment()'..." ); + } + if ( !prealloc() ) { return null; @@ -728,6 +891,7 @@ throw new IllegalStateException( "Attempt to close buffer without closing all blocks" ); } + // fill in the headers. while ( m_last_header != null ) { @@ -739,7 +903,7 @@ } Scrap msghead = m_head; - int len = m_tail.m_fPosition; + int len = m_tail.m_fPosition - m_head.m_fPosition + m_head.m_fLength; // go to closed state. m_tail = null; @@ -817,17 +981,38 @@ int length, MarshalBuffer buffer, Object cookie ); } + /** + * This interface is used by the MarshalBuffer to send important messages + * to a registered listener. + * + * The messages are: + * <ul> + * <li>availIncreased</li> + * <li>bufferClosed</li> + * <li>bufferCanceled</li> + * </ul> + */ public static interface Listener extends java.util.EventListener { /** - * called whenever the size of the buffer increaces or flush is called + * Called whenever the size of the buffer increases or flush is called * while fragemntation is enabled. + * + * @param buffer The marshal buffer instance for which this method + * is called. + * @param available The number of bytes stored in the buffer. + * @param cookie The cookie passed to the addHeader operation. */ void availIncreaced( MarshalBuffer buffer, int available, Object cookie ); /** - * called when the buffer is closed. + * Called when the buffer is closed. + * + * @param buffer The marshal buffer instance for which this method + * is called. + * @param available The number of bytes stored in the buffer. + * @param cookie The cookie passed to the addHeader operation. */ void bufferClosed( MarshalBuffer buffer, int available, Object cookie ); @@ -835,22 +1020,40 @@ * Called when the marshal sequence is canceled by calling the cancel * operation. This should either throw a system exception or simply return * if the cancel will be handled later on. + * + * @param buffer The marshal buffer instance for which this method + * is called. + * @param ex The system exception to be thrown. + * @param cookie The cookie passed to the addHeader operation. */ void bufferCanceled( MarshalBuffer buffer, org.omg.CORBA.SystemException ex, Object cookie ); } + /** + * The prealloc method is called for each method that allocates another + * piece of buffer memory. It performs various checks that must be done for + * each buffer operation in order not to destroy buffer consistency. + * When the member m_cancel_exception is set the exception will be + * thrown by this method. + * + * @return True when the method, from which prealloc is called, may + * continue, false otherwise. + */ private boolean prealloc() { if ( m_cancel_exception != null ) { throw m_cancel_exception; } + + // No head element has been allocated yet if ( m_head == null ) { return false; } - // invalidate last allocated buffer. + + // Invalidate last allocated buffer if ( m_last_buffer != null ) { m_last_buffer.value = null; @@ -861,6 +1064,10 @@ return true; } + /** + * This method calls the Listener.availIncreaced method after the + * buffer memory has been increased. + */ private void postalloc() { // check for fragment request @@ -871,9 +1078,17 @@ && !m_in_fragment ) { m_in_listener = true; - m_listener.availIncreaced( this, - m_tail.m_fPosition - m_head.m_fPosition + m_head.m_fLength, m_listener_cookie ); - m_in_listener = false; + try + { + m_listener.availIncreaced( + this, + m_tail.m_fPosition - m_head.m_fPosition + m_head.m_fLength, + m_listener_cookie ); + } + finally + { + m_in_listener = false; + } } } @@ -905,30 +1120,6 @@ private int m_length; } - private org.omg.CORBA.SystemException m_cancel_exception = null; - - private Scrap m_head; - private Scrap m_tail; - - private OctetSeqHolder m_last_buffer = null; - - private boolean m_allow_fragment = false; - private boolean m_in_fragment = false; - - private boolean m_header_fragment = true; - private HeaderData m_last_header = null; - - private boolean m_block_fragment = true; - private BlockData m_last_block = null; - - // informed when size grows and buffer is closed. - private Listener m_listener = null; - private Object m_listener_cookie = null; - private boolean m_in_listener = false; - - // dummy buffer to marshal data into once cancel has been called. - private byte [] m_ignore_buffer = null; - public static void main( String [] args ) { MarshalBuffer mbuf = new MarshalBuffer(); Index: Scrap.java =================================================================== RCS file: /cvsroot/openorb/OpenORB/src/main/org/openorb/orb/io/Scrap.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- Scrap.java 10 Feb 2004 21:02:50 -0000 1.4 +++ Scrap.java 12 Aug 2004 12:54:24 -0000 1.5 @@ -8,35 +8,110 @@ package org.openorb.orb.io; +import org.openorb.util.HexPrintStream; + /** * Small scrap of data. * This is a simple struct (no behaviour) for a small piece of - * data received from a fragmented GIOP message. + * data inside a marshalling stream. + * + * <pre> + * m_head m_tail + * m_fPosition m_fPosition + * |-----------------|---------------------------------------| + * | ____fNext___ | + * | | | | + * +----------------------------------+ +----------------------------------+ + * | | | | | | + * | |-----fLength-----| | | |-----fLength-----| | + * | +--------------------------+ | | +--------------------------+ | + * | | m_fBuffer | | | | m_fBuffer | | + * | +--------------------------+ | | +--------------------------+ | + * | |-----m_fBuffer.length-----| | | |-----m_fBuffer.length-----| | + * | | | | + * +----------------------------------+ +----------------------------------+ + * + * </pre> * * @author Chris Wood * @version $Revision$ $Date$ */ class Scrap { - /** Default size of a scrap's buffer. */ - public static final int DEFAULT_SCRAP = 2048; + /** Default size of a scrap's buffer: 2 kBytes. */ + public static final int SCRAP_SIZE_DEFAULT = 2048; /** Normal internal scrap. */ - public static final int MODE_NORMAL = 0x00; + public static final int SCRAP_MODE_NORMAL = 0x00; /** A scrap that shares buffer space with a buffer further down the list. */ - public static final int MODE_SHARED = 0x01; + public static final int SCRAP_MODE_SHARED = 0x01; /** The scrap's buffer space is readonly. Clone it to make it read-write. */ - public static final int MODE_READONLY = 0x03; + public static final int SCRAP_MODE_READONLY = 0x03; + + /** A counter that identifies this instance. */ + private static int s_global_index = 0; + /** This scrap's byte buffer. */ public byte [] m_fBuffer; + + /** ??? */ public int m_fOffset; + + /** The number of valid bytes inside the buffer. */ public int m_fLength; + + /** The overall position of this scrap inside the top level container (array). */ public int m_fPosition; - /** The mode of this scrap. See MODE_* constants in this class. */ - public int m_fMode; + /** The mode of this scrap. See SCRAP_MODE_* constants in this class. */ + public int m_fMode = SCRAP_MODE_NORMAL; /** Next scrap instance in the list. */ public Scrap m_fNext = null; -} + /** The unique index of this Scrap instance. */ + private int m_index; + + /** + * Constructor. + */ + public Scrap() + { + m_index = ++s_global_index; + } + + /** + * Return a string representation of this instance. + * + * @return A string describing this instance. + */ + public String toString() + { + StringBuffer sb = new StringBuffer(); + sb.append( "Scrap [" ); + sb.append( "idx=" ); + sb.append( m_index ); + sb.append( ", buf[8]='" ); + sb.append( HexPrintStream.toHex( m_fBuffer[0] ) ); + sb.append( HexPrintStream.toHex( m_fBuffer[1] ) ); + sb.append( HexPrintStream.toHex( m_fBuffer[2] ) ); + sb.append( HexPrintStream.toHex( m_fBuffer[3] ) ); + sb.append( " " ); + sb.append( HexPrintStream.toHex( m_fBuffer[4] ) ); + sb.append( HexPrintStream.toHex( m_fBuffer[5] ) ); + sb.append( HexPrintStream.toHex( m_fBuffer[6] ) ); + sb.append( HexPrintStream.toHex( m_fBuffer[7] ) ); + sb.append( "' , buf_size=" ); + sb.append( ( ( m_fBuffer != null ) ? ( "" + m_fBuffer.length ) : "null" ) ); + sb.append( ", off=" ); + sb.append( m_fOffset ); + sb.append( ", len=" ); + sb.append( m_fLength ); + sb.append( ", pos=" ); + sb.append( m_fPosition ); + sb.append( ", mode=" ); + sb.append( m_fMode ); + sb.append( "]" ); + return sb.toString(); + } +} Index: StorageBuffer.java =================================================================== RCS file: /cvsroot/openorb/OpenORB/src/main/org/openorb/orb/io/StorageBuffer.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- StorageBuffer.java 10 Feb 2004 21:46:22 -0000 1.5 +++ StorageBuffer.java 12 Aug 2004 12:54:24 -0000 1.6 @@ -17,33 +17,33 @@ import org.omg.CORBA.IntHolder; import org.omg.CORBA.OctetSeqHolder; +import org.openorb.orb.util.Trace; + /** * A storage buffer holds binary data for further processing. MarshalBuffers * and BufferSource objects output a stream of StorageBuffers. + * The SocketQueue class uses StorageBuffer parameters when sending data. + * CDRInpuStream uses StorageBuffer instance to unmarshal GIOP data from. + * This class always represents a full IIOP message and it is used to + * the client side for unmarshalling and on the server side for marshalling + * GIOP into the IIOP. * * @author Unknown */ public class StorageBuffer { - /** */ private int m_avail; - /** */ private Scrap m_mark; - /** */ private int m_markavail; - /** */ private Scrap m_head; - /** */ private Scrap m_temp_head = new Scrap(); - /** */ private Scrap m_tail; - /** */ private boolean m_read_write_mode; /** @@ -68,7 +68,7 @@ m_head.m_fPosition = len; - m_head.m_fMode = Scrap.MODE_READONLY; + m_head.m_fMode = Scrap.SCRAP_MODE_READONLY; } /** @@ -83,7 +83,7 @@ /** * Public constructor, read part from buffer and remainder from stream. * A whole buffer is always read. If the reading thread is interrupted - * the buffer will be completley read and the interrupted status reset. + * the buffer will be completely read and the interrupted status reset. * * @param buf Prefix bytes. Copied. * @param off Offset into prefix buffer. @@ -121,7 +121,7 @@ t.m_fOffset = 0; t.m_fLength = len; t.m_fPosition = len; - t.m_fMode = Scrap.MODE_NORMAL; + t.m_fMode = Scrap.SCRAP_MODE_NORMAL; System.arraycopy( buf, off, t.m_fBuffer, 0, len ); @@ -131,14 +131,14 @@ } else { - int alloc = ( total_len > Scrap.DEFAULT_SCRAP ) - ? Scrap.DEFAULT_SCRAP : total_len; + int alloc = ( total_len > Scrap.SCRAP_SIZE_DEFAULT ) + ? Scrap.SCRAP_SIZE_DEFAULT : total_len; t.m_fBuffer = new byte[ alloc ]; t.m_fOffset = 0; t.m_fLength = alloc; t.m_fPosition = alloc; - t.m_fMode = Scrap.MODE_NORMAL; + t.m_fMode = Scrap.SCRAP_MODE_NORMAL; System.arraycopy( buf, off, t.m_fBuffer, 0, len ); alloc -= len; @@ -179,14 +179,14 @@ // when we have more fragments... while ( total_len > 0 ) { - int alloc = ( total_len > Scrap.DEFAULT_SCRAP ) - ? Scrap.DEFAULT_SCRAP : total_len; + int alloc = ( total_len > Scrap.SCRAP_SIZE_DEFAULT ) + ? Scrap.SCRAP_SIZE_DEFAULT : total_len; Scrap nex = new Scrap(); nex.m_fBuffer = new byte[ alloc ]; nex.m_fOffset = 0; nex.m_fLength = alloc; nex.m_fPosition = t.m_fPosition + alloc; - nex.m_fMode = Scrap.MODE_NORMAL; + nex.m_fMode = Scrap.SCRAP_MODE_NORMAL; int pos = 0; @@ -277,20 +277,24 @@ { throw new EOFException( "Buffer is empty" ); } + boolean interrupt = Thread.interrupted(); + // for debugging + Trace.isGIOPHeaderOK( m_head.m_fBuffer, 0 ); + if ( m_read_write_mode ) { while ( m_head != m_tail ) { - if ( m_head.m_fMode == Scrap.MODE_READONLY ) + if ( m_head.m_fMode == Scrap.SCRAP_MODE_READONLY ) { byte [] tmp = new byte[ m_head.m_fLength ]; System.arraycopy( m_head.m_fBuffer, m_head.m_fOffset, tmp, 0, m_head.m_fLength ); m_head.m_fBuffer = tmp; m_head.m_fOffset = 0; - m_head.m_fMode = Scrap.MODE_NORMAL; + m_head.m_fMode = Scrap.SCRAP_MODE_NORMAL; } int d = 0; @@ -368,13 +372,13 @@ { throw new IndexOutOfBoundsException(); } - if ( m_read_write_mode && m_head.m_fMode == Scrap.MODE_READONLY ) + if ( m_read_write_mode && m_head.m_fMode == Scrap.SCRAP_MODE_READONLY ) { byte [] copy = new byte[ m_head.m_fLength ]; System.arraycopy( m_head.m_fBuffer, m_head.m_fOffset, copy, 0, m_head.m_fLength ); m_head.m_fBuffer = copy; m_head.m_fOffset = 0; - m_head.m_fMode = Scrap.MODE_NORMAL; + m_head.m_fMode = Scrap.SCRAP_MODE_NORMAL; } buf.value = m_head.m_fBuffer; @@ -396,7 +400,7 @@ m_temp_head.m_fBuffer = m_head.m_fBuffer; m_temp_head.m_fOffset = m_head.m_fOffset + olen; m_temp_head.m_fLength = m_head.m_fLength - olen; - m_temp_head.m_fMode = m_head.m_fMode | Scrap.MODE_SHARED; + m_temp_head.m_fMode = m_head.m_fMode | Scrap.SCRAP_MODE_SHARED; m_temp_head.m_fPosition = m_head.m_fPosition; m_temp_head.m_fNext = m_head.m_fNext; m_head = m_temp_head; @@ -462,7 +466,7 @@ m_temp_head.m_fBuffer = m_head.m_fBuffer; m_temp_head.m_fOffset = m_head.m_fOffset + len.value; m_temp_head.m_fLength = m_head.m_fLength - len.value; - m_temp_head.m_fMode = m_head.m_fMode | Scrap.MODE_SHARED; + m_temp_head.m_fMode = m_head.m_fMode | Scrap.SCRAP_MODE_SHARED; m_temp_head.m_fPosition = m_head.m_fPosition; m_temp_head.m_fNext = m_head.m_fNext; m_head = m_temp_head; @@ -491,36 +495,38 @@ return new byte[ 0 ]; } if ( m_head.m_fLength != m_head.m_fBuffer.length || m_head.m_fNext != m_tail - || m_head.m_fMode != Scrap.MODE_NORMAL || m_head.m_fOffset != 0 ) + || m_head.m_fMode != Scrap.SCRAP_MODE_NORMAL || m_head.m_fOffset != 0 ) { byte [] buf = new byte[ m_avail ]; Scrap nex = m_head; - int disp = m_head.m_fPosition - m_head.m_fLength; + int dest = 0; while ( nex != m_tail ) { try { - System.arraycopy( nex.m_fBuffer, nex.m_fOffset, buf, disp, nex.m_fLength ); + System.arraycopy( nex.m_fBuffer, nex.m_fOffset, buf, dest, nex.m_fLength ); } catch ( Exception ex ) { + Trace.signalIllegalCondition( null, + "An exception occured during linearization!" ); // This may fail with the following exceptions: // IndexOutOfBoundsException, NullPointerException, ArrayStoreException // in either case copy was not successful, so return null; return null; } - disp += nex.m_fLength; + dest += nex.m_fLength; nex = nex.m_fNext; } m_head.m_fBuffer = buf; m_head.m_fOffset = 0; - m_head.m_fLength = m_avail; - m_head.m_fMode = Scrap.MODE_NORMAL; + m_head.m_fMode = Scrap.SCRAP_MODE_NORMAL; m_head.m_fPosition = m_head.m_fPosition - m_head.m_fLength + m_avail; + m_head.m_fLength = m_avail; m_head.m_fNext = m_tail; } |