You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2005/12/07 05:06:05 UTC
svn commit: r354698 - in
/directory/network/trunk/src/java/org/apache/mina/common: ByteBuffer.java
ByteBufferProxy.java
Author: trustin
Date: Tue Dec 6 20:06:01 2005
New Revision: 354698
URL: http://svn.apache.org/viewcvs?rev=354698&view=rev
Log:
Resolved issue: DIRMINA-139 (ByteBuffer.expand() for explicit expansion)
* Added ByteBuffer.expand()
* Pulled up most implementations of ByteBufferImpl to ByteBuffer to avoid unnecessary reimplementations
* Changed compareTo(ByteBuffer) to compareTo(Object) for JDK 1.4 compatibility; ByteBuffer now implements Comparable.
Modified:
directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java
directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java
Modified: directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java?rev=354698&r1=354697&r2=354698&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java Tue Dec 6 20:06:01 2005
@@ -124,7 +124,7 @@
* @author The Apache Directory Project (dev@directory.apache.org)
* @version $Rev$, $Date$
*/
-public abstract class ByteBuffer
+public abstract class ByteBuffer implements Comparable
{
private static final int MINIMUM_CAPACITY = 1;
@@ -324,6 +324,23 @@
public abstract ByteBuffer setAutoExpand( boolean autoExpand );
/**
+ * Changes the capacity and limit of this buffer so this buffer get
+ * the specified <tt>expectedRemaining</tt> room from the current position.
+ * This method works even if you didn't set <tt>autoExpand</tt> to
+ * <tt>true</tt>.
+ */
+ public abstract ByteBuffer expand( int expectedRemaining );
+
+ /**
+ * Changes the capacity and limit of this buffer so this buffer get
+ * the specified <tt>expectedRemaining</tt> room from the specified
+ * <tt>pos</tt>.
+ * This method works even if you didn't set <tt>autoExpand</tt> to
+ * <tt>true</tt>.
+ */
+ public abstract ByteBuffer expand( int pos, int expectedRemaining );
+
+ /**
* Returns <tt>true</tt> if and only if this buffer is returned back
* to the buffer pool when released.
* <p>
@@ -419,7 +436,10 @@
/**
* @see java.nio.Buffer#hasRemaining()
*/
- public abstract boolean hasRemaining();
+ public boolean hasRemaining()
+ {
+ return remaining() > 0;
+ }
/**
* @see java.nio.ByteBuffer#get()
@@ -429,7 +449,10 @@
/**
* Reads one unsigned byte as a short integer.
*/
- public abstract short getUnsigned();
+ public short getUnsigned()
+ {
+ return ( short ) ( get() & 0xff );
+ }
/**
* @see java.nio.ByteBuffer#put(byte)
@@ -444,7 +467,10 @@
/**
* Reads one byte as an unsigned short integer.
*/
- public abstract short getUnsigned( int index );
+ public short getUnsigned( int index )
+ {
+ return ( short ) ( get( index ) & 0xff );
+ }
/**
* @see java.nio.ByteBuffer#put(int, byte)
@@ -459,7 +485,10 @@
/**
* @see java.nio.ByteBuffer#get(byte[])
*/
- public abstract ByteBuffer get( byte[] dst );
+ public ByteBuffer get( byte[] dst )
+ {
+ return get( dst, 0, dst.length );
+ }
/**
* Writes the content of the specified <tt>src</tt> into this buffer.
@@ -469,7 +498,10 @@
/**
* Writes the content of the specified <tt>src</tt> into this buffer.
*/
- public abstract ByteBuffer put( ByteBuffer src );
+ public ByteBuffer put( ByteBuffer src )
+ {
+ return put( src.buf() );
+ }
/**
* @see java.nio.ByteBuffer#put(byte[], int, int)
@@ -479,20 +511,97 @@
/**
* @see java.nio.ByteBuffer#put(byte[])
*/
- public abstract ByteBuffer put( byte[] src );
+ public ByteBuffer put( byte[] src )
+ {
+ return put( src, 0, src.length );
+ }
/**
* @see java.nio.ByteBuffer#compact()
*/
public abstract ByteBuffer compact();
- public abstract String toString();
+ public String toString()
+ {
+ StringBuffer buf = new StringBuffer();
+ if( isDirect() )
+ {
+ buf.append( "DirectBuffer" );
+ }
+ else
+ {
+ buf.append( "HeapBuffer" );
+ }
+ buf.append( "[pos=" );
+ buf.append( position() );
+ buf.append( " lim=" );
+ buf.append( limit() );
+ buf.append( " cap=" );
+ buf.append( capacity() );
+ buf.append( ": " );
+ buf.append( getHexDump() );
+ buf.append( ']' );
+ return buf.toString();
+ }
- public abstract int hashCode();
+ public int hashCode()
+ {
+ int h = 1;
+ int p = position();
+ for( int i = limit() - 1; i >= p; i -- )
+ {
+ h = 31 * h + get( i );
+ }
+ return h;
+ }
- public abstract boolean equals( Object ob );
+ public boolean equals( Object o )
+ {
+ if( !( o instanceof ByteBuffer ) )
+ {
+ return false;
+ }
+
+ ByteBuffer that = ( ByteBuffer ) o;
+ if( this.remaining() != that.remaining() )
+ {
+ return false;
+ }
+
+ int p = this.position();
+ for( int i = this.limit() - 1, j = that.limit() - 1; i >= p; i --, j --)
+ {
+ byte v1 = this.get( i );
+ byte v2 = that.get( j );
+ if( v1 != v2 )
+ {
+ return false;
+ }
+ }
+ return true;
+ }
- public abstract int compareTo( ByteBuffer that );
+ public int compareTo( Object o )
+ {
+ ByteBuffer that = ( ByteBuffer ) o;
+ int n = this.position() + Math.min( this.remaining(), that.remaining() );
+ for( int i = this.position(), j = that.position(); i < n; i ++, j ++ )
+ {
+ byte v1 = this.get( i );
+ byte v2 = that.get( j );
+ if( v1 == v2 )
+ {
+ continue;
+ }
+ if( v1 < v2 )
+ {
+ return -1;
+ }
+
+ return +1;
+ }
+ return this.remaining() - that.remaining();
+ }
/**
* @see java.nio.ByteBuffer#order()
@@ -537,7 +646,10 @@
/**
* Reads two bytes unsigned integer.
*/
- public abstract int getUnsignedShort();
+ public int getUnsignedShort()
+ {
+ return getShort() & 0xffff;
+ }
/**
* @see java.nio.ByteBuffer#putShort(short)
@@ -552,7 +664,10 @@
/**
* Reads two bytes unsigned integer.
*/
- public abstract int getUnsignedShort( int index );
+ public int getUnsignedShort( int index )
+ {
+ return getShort( index ) & 0xffff;
+ }
/**
* @see java.nio.ByteBuffer#putShort(int, short)
@@ -572,7 +687,10 @@
/**
* Reads four bytes unsigned integer.
*/
- public abstract long getUnsignedInt();
+ public long getUnsignedInt()
+ {
+ return getInt() & 0xffffffffL;
+ }
/**
* @see java.nio.ByteBuffer#putInt(int)
@@ -587,7 +705,10 @@
/**
* Reads four bytes unsigned integer.
*/
- public abstract long getUnsignedInt( int index );
+ public long getUnsignedInt( int index )
+ {
+ return getInt( index ) & 0xffffffffL;
+ }
/**
* @see java.nio.ByteBuffer#putInt(int, int)
@@ -774,7 +895,10 @@
/**
* Returns hexdump of this buffer.
*/
- public abstract String getHexDump();
+ public String getHexDump()
+ {
+ return ByteBufferHexDumper.getHexdump( this );
+ }
////////////////////////////////
// String getters and putters //
@@ -785,7 +909,104 @@
* specified <code>decoder</code> and returns it. This method reads
* until the limit of this buffer if no <tt>NUL</tt> is found.
*/
- public abstract String getString( CharsetDecoder decoder ) throws CharacterCodingException;
+ public String getString( CharsetDecoder decoder ) throws CharacterCodingException
+ {
+ if( !hasRemaining() )
+ {
+ return "";
+ }
+
+ boolean utf16 = decoder.charset().name().startsWith( "UTF-16" );
+
+ int oldPos = position();
+ int oldLimit = limit();
+ int end;
+
+ if( !utf16 )
+ {
+ while( hasRemaining() )
+ {
+ if( get() == 0 )
+ {
+ break;
+ }
+ }
+
+ end = position();
+ if( end == oldLimit )
+ {
+ limit( end );
+ }
+ else
+ {
+ limit( end - 1 );
+ }
+ }
+ else
+ {
+ while( remaining() >= 2 )
+ {
+ if( ( get() == 0 ) && ( get() == 0 ) )
+ {
+ break;
+ }
+ }
+
+ end = position();
+ if( end == oldLimit || end == oldLimit - 1 )
+ {
+ limit( end );
+ }
+ else
+ {
+ limit( end - 2 );
+ }
+ }
+
+ position( oldPos );
+ if( !hasRemaining() )
+ {
+ limit( oldLimit );
+ position( end );
+ return "";
+ }
+ decoder.reset();
+
+ int expectedLength = (int) ( remaining() * decoder.averageCharsPerByte() );
+ CharBuffer out = CharBuffer.allocate( expectedLength );
+ for( ;; )
+ {
+ CoderResult cr;
+ if ( hasRemaining() )
+ {
+ cr = decoder.decode( buf(), out, true );
+ }
+ else
+ {
+ cr = decoder.flush( out );
+ }
+
+ if ( cr.isUnderflow() )
+ {
+ break;
+ }
+
+ if ( cr.isOverflow() )
+ {
+ CharBuffer o = CharBuffer.allocate( out.capacity() + expectedLength );
+ out.flip();
+ o.put(out);
+ out = o;
+ continue;
+ }
+
+ cr.throwException();
+ }
+
+ limit( oldLimit );
+ position( end );
+ return out.flip().toString();
+ }
/**
* Reads a <code>NUL</code>-terminated string from this buffer using the
@@ -793,7 +1014,120 @@
*
* @param fieldSize the maximum number of bytes to read
*/
- public abstract String getString( int fieldSize, CharsetDecoder decoder ) throws CharacterCodingException;
+ public String getString( int fieldSize, CharsetDecoder decoder ) throws CharacterCodingException
+ {
+ checkFieldSize( fieldSize );
+
+ if( fieldSize == 0 )
+ {
+ return "";
+ }
+
+ if( !hasRemaining() )
+ {
+ return "";
+ }
+
+ boolean utf16 = decoder.charset().name().startsWith( "UTF-16" );
+
+ if( utf16 && ( ( fieldSize & 1 ) != 0 ) )
+ {
+ throw new IllegalArgumentException( "fieldSize is not even." );
+ }
+
+ int i;
+ int oldPos = position();
+ int oldLimit = limit();
+ int end = position() + fieldSize;
+
+ if( oldLimit < end )
+ {
+ throw new BufferUnderflowException();
+ }
+
+ if( !utf16 )
+ {
+ for( i = 0; i < fieldSize; i ++ )
+ {
+ if( get() == 0 )
+ {
+ break;
+ }
+ }
+
+ if( i == fieldSize )
+ {
+ limit( end );
+ }
+ else
+ {
+ limit( position() - 1 );
+ }
+ }
+ else
+ {
+ for( i = 0; i < fieldSize; i += 2 )
+ {
+ if( ( get() == 0 ) && ( get() == 0 ) )
+ {
+ break;
+ }
+ }
+
+ if( i == fieldSize )
+ {
+ limit( end );
+ }
+ else
+ {
+ limit( position() - 2 );
+ }
+ }
+
+ position( oldPos );
+ if( !hasRemaining() )
+ {
+ limit( oldLimit );
+ position( end );
+ return "";
+ }
+ decoder.reset();
+
+ int expectedLength = (int) ( remaining() * decoder.averageCharsPerByte() );
+ CharBuffer out = CharBuffer.allocate( expectedLength );
+ for( ;; )
+ {
+ CoderResult cr;
+ if ( hasRemaining() )
+ {
+ cr = decoder.decode( buf(), out, true );
+ }
+ else
+ {
+ cr = decoder.flush( out );
+ }
+
+ if ( cr.isUnderflow() )
+ {
+ break;
+ }
+
+ if ( cr.isOverflow() )
+ {
+ CharBuffer o = CharBuffer.allocate( out.capacity() + expectedLength );
+ out.flip();
+ o.put(out);
+ out = o;
+ continue;
+ }
+
+ cr.throwException();
+ }
+
+ limit( oldLimit );
+ position( end );
+ return out.flip().toString();
+ }
/**
* Writes the content of <code>in</code> into this buffer using the
@@ -802,7 +1136,43 @@
*
* @throws BufferOverflowException if the specified string doesn't fit
*/
- public abstract ByteBuffer putString( CharSequence in, CharsetEncoder encoder ) throws CharacterCodingException;
+ public ByteBuffer putString(
+ CharSequence val, CharsetEncoder encoder ) throws CharacterCodingException
+ {
+ if( val.length() == 0 )
+ {
+ return this;
+ }
+
+ CharBuffer in = CharBuffer.wrap( val );
+ int expectedLength = (int) (in.remaining() * encoder.averageBytesPerChar());
+
+ encoder.reset();
+
+ for (;;) {
+ CoderResult cr;
+ if( in.hasRemaining() )
+ {
+ cr = encoder.encode( in, buf(), true );
+ }
+ else
+ {
+ cr = encoder.flush( buf() );
+ }
+
+ if( cr.isUnderflow() )
+ {
+ break;
+ }
+ if( cr.isOverflow() && isAutoExpand() )
+ {
+ autoExpand( expectedLength );
+ continue;
+ }
+ cr.throwException();
+ }
+ return this;
+ }
/**
* Writes the content of <code>in</code> into this buffer as a
@@ -818,8 +1188,86 @@
*
* @param fieldSize the maximum number of bytes to write
*/
- public abstract ByteBuffer putString(
- CharSequence in, int fieldSize, CharsetEncoder encoder ) throws CharacterCodingException;
+ public ByteBuffer putString(
+ CharSequence val, int fieldSize, CharsetEncoder encoder ) throws CharacterCodingException
+ {
+ checkFieldSize( fieldSize );
+
+ if( fieldSize == 0 )
+ return this;
+
+ autoExpand( fieldSize );
+
+ boolean utf16 = encoder.charset().name().startsWith( "UTF-16" );
+
+ if( utf16 && ( ( fieldSize & 1 ) != 0 ) )
+ {
+ throw new IllegalArgumentException( "fieldSize is not even." );
+ }
+
+ int oldLimit = limit();
+ int end = position() + fieldSize;
+
+ if( oldLimit < end )
+ {
+ throw new BufferOverflowException();
+ }
+
+ if( val.length() == 0 )
+ {
+ if( !utf16 )
+ {
+ put( ( byte ) 0x00 );
+ }
+ else
+ {
+ put( ( byte ) 0x00 );
+ put( ( byte ) 0x00 );
+ }
+ position( end );
+ return this;
+ }
+
+ CharBuffer in = CharBuffer.wrap( val );
+ limit( end );
+ encoder.reset();
+
+ for (;;) {
+ CoderResult cr;
+ if( in.hasRemaining() )
+ {
+ cr = encoder.encode( in, buf(), true );
+ }
+ else
+ {
+ cr = encoder.flush( buf() );
+ }
+
+ if( cr.isUnderflow() || cr.isOverflow() )
+ {
+ break;
+ }
+ cr.throwException();
+ }
+
+ limit( oldLimit );
+
+ if( position() < end )
+ {
+ if( !utf16 )
+ {
+ put( ( byte ) 0x00 );
+ }
+ else
+ {
+ put( ( byte ) 0x00 );
+ put( ( byte ) 0x00 );
+ }
+ }
+
+ position( end );
+ return this;
+ }
/**
* Reads a string which has a 16-bit length field before the actual
@@ -837,23 +1285,104 @@
*
* @param prefixLength the length of the length field (1, 2, or 4)
*/
- public abstract String getPrefixedString( int prefixLength, CharsetDecoder decoder ) throws CharacterCodingException;
-
- /**
- * Writes the content of <code>in</code> into this buffer as a
- * string which has a 16-bit length field before the actual
- * encoded string, using the specified <code>encoder</code>.
- * This method is a shortcut for <tt>putPrefixedString(in, 2, 0, encoder)</tt>.
- *
- * @throws BufferOverflowException if the specified string doesn't fit
- */
- public ByteBuffer putPrefixedString( CharSequence in, CharsetEncoder encoder ) throws CharacterCodingException
+ public String getPrefixedString( int prefixLength, CharsetDecoder decoder ) throws CharacterCodingException
{
- return putPrefixedString( in, 2, 0, encoder );
- }
-
- /**
- * Writes the content of <code>in</code> into this buffer as a
+ int fieldSize;
+
+ switch( prefixLength )
+ {
+ case 1:
+ fieldSize = getUnsigned();
+ break;
+ case 2:
+ fieldSize = getUnsignedShort();
+ break;
+ case 4:
+ fieldSize = getInt();
+ break;
+ default:
+ throw new IllegalArgumentException( "prefixLength: " + prefixLength );
+ }
+
+ if( fieldSize < 0 )
+ {
+ throw new BufferDataException( "Invalid fieldSize: " + fieldSize );
+ }
+
+ if( fieldSize == 0 )
+ {
+ return "";
+ }
+
+ boolean utf16 = decoder.charset().name().startsWith( "UTF-16" );
+
+ if( utf16 && ( ( fieldSize & 1 ) != 0 ) )
+ {
+ throw new BufferDataException( "fieldSize is not even for a UTF-16 string." );
+ }
+
+ int oldLimit = limit();
+ int end = position() + fieldSize;
+
+ if( oldLimit < end )
+ {
+ throw new BufferUnderflowException();
+ }
+
+ limit( end );
+ decoder.reset();
+
+ int expectedLength = (int) ( remaining() * decoder.averageCharsPerByte() );
+ CharBuffer out = CharBuffer.allocate( expectedLength );
+ for( ;; )
+ {
+ CoderResult cr;
+ if ( hasRemaining() )
+ {
+ cr = decoder.decode( buf(), out, true );
+ }
+ else
+ {
+ cr = decoder.flush( out );
+ }
+
+ if ( cr.isUnderflow() )
+ {
+ break;
+ }
+
+ if ( cr.isOverflow() )
+ {
+ CharBuffer o = CharBuffer.allocate( out.capacity() + expectedLength );
+ out.flip();
+ o.put(out);
+ out = o;
+ continue;
+ }
+
+ cr.throwException();
+ }
+
+ limit( oldLimit );
+ position( end );
+ return out.flip().toString();
+ }
+
+ /**
+ * Writes the content of <code>in</code> into this buffer as a
+ * string which has a 16-bit length field before the actual
+ * encoded string, using the specified <code>encoder</code>.
+ * This method is a shortcut for <tt>putPrefixedString(in, 2, 0, encoder)</tt>.
+ *
+ * @throws BufferOverflowException if the specified string doesn't fit
+ */
+ public ByteBuffer putPrefixedString( CharSequence in, CharsetEncoder encoder ) throws CharacterCodingException
+ {
+ return putPrefixedString( in, 2, 0, encoder );
+ }
+
+ /**
+ * Writes the content of <code>in</code> into this buffer as a
* string which has a 16-bit length field before the actual
* encoded string, using the specified <code>encoder</code>.
* This method is a shortcut for <tt>putPrefixedString(in, prefixLength, 0, encoder)</tt>.
@@ -894,7 +1423,114 @@
*
* @throws BufferOverflowException if the specified string doesn't fit
*/
- public abstract ByteBuffer putPrefixedString( CharSequence in, int prefixLength, int padding, byte padValue, CharsetEncoder encoder ) throws CharacterCodingException;
+ public ByteBuffer putPrefixedString( CharSequence val, int prefixLength, int padding, byte padValue, CharsetEncoder encoder ) throws CharacterCodingException
+ {
+ int maxLength;
+ switch( prefixLength )
+ {
+ case 1:
+ maxLength = 255;
+ break;
+ case 2:
+ maxLength = 65535;
+ break;
+ case 4:
+ maxLength = Integer.MAX_VALUE;
+ break;
+ default:
+ throw new IllegalArgumentException( "prefixLength: " + prefixLength );
+ }
+
+ if( val.length() > maxLength )
+ {
+ throw new IllegalArgumentException( "The specified string is too long." );
+ }
+ if( val.length() == 0 )
+ {
+ switch( prefixLength )
+ {
+ case 1:
+ put( ( byte ) 0 );
+ break;
+ case 2:
+ putShort( ( short ) 0 );
+ break;
+ case 4:
+ putInt( 0 );
+ break;
+ }
+ return this;
+ }
+
+ int padMask;
+ switch( padding )
+ {
+ case 0:
+ case 1:
+ padMask = 0;
+ break;
+ case 2:
+ padMask = 1;
+ break;
+ case 4:
+ padMask = 3;
+ break;
+ default:
+ throw new IllegalArgumentException( "padding: " + padding );
+ }
+
+ CharBuffer in = CharBuffer.wrap( val );
+ int expectedLength = (int) (in.remaining() * encoder.averageBytesPerChar());
+
+ skip( prefixLength ); // make a room for the length field
+ int oldPos = position();
+ encoder.reset();
+
+ for (;;) {
+ CoderResult cr;
+ if( in.hasRemaining() )
+ {
+ cr = encoder.encode( in, buf(), true );
+ }
+ else
+ {
+ cr = encoder.flush( buf() );
+ }
+
+ if( position() - oldPos > maxLength )
+ {
+ throw new IllegalArgumentException( "The specified string is too long." );
+ }
+
+ if( cr.isUnderflow() )
+ {
+ break;
+ }
+ if( cr.isOverflow() && isAutoExpand() )
+ {
+ autoExpand( expectedLength );
+ continue;
+ }
+ cr.throwException();
+ }
+
+ // Write the length field
+ fill( padValue, padding - ( ( position() - oldPos ) & padMask ) );
+ int length = position() - oldPos;
+ switch( prefixLength )
+ {
+ case 1:
+ put( oldPos - 1, ( byte ) length );
+ break;
+ case 2:
+ putShort( oldPos - 2, ( short ) length );
+ break;
+ case 4:
+ putInt( oldPos - 4, length );
+ break;
+ }
+ return this;
+ }
/**
* Reads a Java object from the buffer using the context {@link ClassLoader}
@@ -908,12 +1544,75 @@
/**
* Reads a Java object from the buffer using the specified <tt>classLoader</tt>.
*/
- public abstract Object getObject( ClassLoader classLoader ) throws ClassNotFoundException;
+ public Object getObject( final ClassLoader classLoader ) throws ClassNotFoundException
+ {
+ int length = getInt();
+ if( length > remaining() )
+ {
+ throw new BufferUnderflowException();
+ }
+ if( length <= 4 )
+ {
+ throw new BufferDataException( "Object length should be greater than 4: " + length );
+ }
+ int oldLimit = limit();
+ limit( position() + length );
+ try
+ {
+ ObjectInputStream in = new ObjectInputStream( asInputStream() )
+ {
+ protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException
+ {
+ String className = readUTF();
+ Class clazz = Class.forName( className, true, classLoader );
+ ObjectStreamClass descriptor = ObjectStreamClass.lookup(clazz);
+ return descriptor;
+ }
+ };
+ return in.readObject();
+ }
+ catch( IOException e )
+ {
+ throw new BufferDataException( e );
+ }
+ finally
+ {
+ limit( oldLimit );
+ }
+ }
+
/**
* Writes the specified Java object to the buffer.
*/
- public abstract ByteBuffer putObject( Object o );
+ public ByteBuffer putObject( Object o )
+ {
+ int oldPos = position();
+ skip( 4 ); // Make a room for the length field.
+ try
+ {
+ ObjectOutputStream out = new ObjectOutputStream( asOutputStream() )
+ {
+ protected void writeClassDescriptor( ObjectStreamClass desc ) throws IOException
+ {
+ writeUTF( desc.getName() );
+ }
+ };
+ out.writeObject( o );
+ out.flush();
+ }
+ catch( IOException e )
+ {
+ throw new BufferDataException( e );
+ }
+
+ // Fill the length field
+ int newPos = position();
+ position( oldPos );
+ putInt( newPos - oldPos - 4 );
+ position( newPos );
+ return this;
+ }
//////////////////////////
// Skip or fill methods //
@@ -923,31 +1622,176 @@
* Forwards the position of this buffer as the specified <code>size</code>
* bytes.
*/
- public abstract ByteBuffer skip( int size );
+ public ByteBuffer skip( int size )
+ {
+ autoExpand( size );
+ return position( position() + size );
+ }
/**
* Fills this buffer with the specified value.
* This method moves buffer position forward.
*/
- public abstract ByteBuffer fill( byte value, int size );
+ public ByteBuffer fill( byte value, int size )
+ {
+ autoExpand( size );
+ int q = size >>> 3;
+ int r = size & 7;
+
+ if( q > 0 )
+ {
+ int intValue = value | ( value << 8 ) | ( value << 16 )
+ | ( value << 24 );
+ long longValue = intValue;
+ longValue <<= 32;
+ longValue |= intValue;
+
+ for( int i = q; i > 0; i -- )
+ {
+ putLong( longValue );
+ }
+ }
+
+ q = r >>> 2;
+ r = r & 3;
+
+ if( q > 0 )
+ {
+ int intValue = value | ( value << 8 ) | ( value << 16 )
+ | ( value << 24 );
+ putInt( intValue );
+ }
+
+ q = r >> 1;
+ r = r & 1;
+
+ if( q > 0 )
+ {
+ short shortValue = ( short ) ( value | ( value << 8 ) );
+ putShort( shortValue );
+ }
+
+ if( r > 0 )
+ {
+ put( value );
+ }
+
+ return this;
+ }
/**
* Fills this buffer with the specified value.
* This method does not change buffer position.
*/
- public abstract ByteBuffer fillAndReset( byte value, int size );
+ public ByteBuffer fillAndReset( byte value, int size )
+ {
+ autoExpand( size );
+ int pos = position();
+ try
+ {
+ fill( value, size );
+ }
+ finally
+ {
+ position( pos );
+ }
+ return this;
+ }
/**
* Fills this buffer with <code>NUL (0x00)</code>.
* This method moves buffer position forward.
*/
- public abstract ByteBuffer fill( int size );
+ public ByteBuffer fill( int size )
+ {
+ autoExpand( size );
+ int q = size >>> 3;
+ int r = size & 7;
+
+ for( int i = q; i > 0; i -- )
+ {
+ putLong( 0L );
+ }
+
+ q = r >>> 2;
+ r = r & 3;
+
+ if( q > 0 )
+ {
+ putInt( 0 );
+ }
+
+ q = r >> 1;
+ r = r & 1;
+
+ if( q > 0 )
+ {
+ putShort( ( short ) 0 );
+ }
+
+ if( r > 0 )
+ {
+ put( ( byte ) 0 );
+ }
+
+ return this;
+ }
/**
* Fills this buffer with <code>NUL (0x00)</code>.
* This method does not change buffer position.
*/
- public abstract ByteBuffer fillAndReset( int size );
+ public ByteBuffer fillAndReset( int size )
+ {
+ autoExpand( size );
+ int pos = position();
+ try
+ {
+ fill( size );
+ }
+ finally
+ {
+ position( pos );
+ }
+
+ return this;
+ }
+
+ /**
+ * This method forwards the call to {@link #expand(int)} only when
+ * <tt>autoExpand</tt> property is <tt>true</tt>.
+ */
+ protected ByteBuffer autoExpand( int expectedRemaining )
+ {
+ if( isAutoExpand() )
+ {
+ expand( expectedRemaining );
+ }
+ return this;
+ }
+
+ /**
+ * This method forwards the call to {@link #expand(int)} only when
+ * <tt>autoExpand</tt> property is <tt>true</tt>.
+ */
+ protected ByteBuffer autoExpand( int pos, int expectedRemaining )
+ {
+ if( isAutoExpand() )
+ {
+ expand( pos, expectedRemaining );
+ }
+ return this;
+ }
+
+
+ private static void checkFieldSize( int fieldSize )
+ {
+ if( fieldSize < 0 )
+ {
+ throw new IllegalArgumentException(
+ "fieldSize cannot be negative: " + fieldSize );
+ }
+ }
private static class DefaultByteBuffer extends ByteBuffer
{
@@ -1109,21 +1953,11 @@
return buf.remaining();
}
- public boolean hasRemaining()
- {
- return buf.hasRemaining();
- }
-
public byte get()
{
return buf.get();
}
- public short getUnsigned()
- {
- return ( short ) ( get() & 0xff );
- }
-
public ByteBuffer put( byte b )
{
autoExpand( 1 );
@@ -1136,11 +1970,6 @@
return buf.get( index );
}
- public short getUnsigned( int index )
- {
- return ( short ) ( get( index ) & 0xff );
- }
-
public ByteBuffer put( int index, byte b )
{
autoExpand( index, 1 );
@@ -1154,12 +1983,6 @@
return this;
}
- public ByteBuffer get( byte[] dst )
- {
- buf.get( dst );
- return this;
- }
-
public ByteBuffer put( java.nio.ByteBuffer src )
{
autoExpand( src.remaining() );
@@ -1167,13 +1990,6 @@
return this;
}
- public ByteBuffer put( ByteBuffer src )
- {
- autoExpand( src.remaining() );
- buf.put( src.buf() );
- return this;
- }
-
public ByteBuffer put( byte[] src, int offset, int length )
{
autoExpand( length );
@@ -1181,38 +1997,12 @@
return this;
}
- public ByteBuffer put( byte[] src )
- {
- autoExpand( src.length );
- buf.put( src );
- return this;
- }
-
public ByteBuffer compact()
{
buf.compact();
return this;
}
- public String toString()
- {
- return buf.toString() + " (" + getHexDump() + ')';
- }
-
- public int hashCode()
- {
- return buf.hashCode();
- }
-
- public boolean equals( Object ob )
- {
- if( !( ob instanceof ByteBuffer ) )
- return false;
-
- ByteBuffer that = ( ByteBuffer ) ob;
- return this.buf.equals( that.buf() );
- }
-
public int compareTo( ByteBuffer that )
{
return this.buf.compareTo( that.buf() );
@@ -1263,11 +2053,6 @@
return buf.getShort();
}
- public int getUnsignedShort()
- {
- return getShort() & 0xffff;
- }
-
public ByteBuffer putShort( short value )
{
autoExpand( 2 );
@@ -1280,11 +2065,6 @@
return buf.getShort( index );
}
- public int getUnsignedShort( int index )
- {
- return getShort( index ) & 0xffff;
- }
-
public ByteBuffer putShort( int index, short value )
{
autoExpand( index, 2 );
@@ -1302,11 +2082,6 @@
return buf.getInt();
}
- public long getUnsignedInt()
- {
- return getInt() & 0xffffffffL;
- }
-
public ByteBuffer putInt( int value )
{
autoExpand( 4 );
@@ -1319,11 +2094,6 @@
return buf.getInt( index );
}
- public long getUnsignedInt( int index )
- {
- return getInt( index ) & 0xffffffffL;
- }
-
public ByteBuffer putInt( int index, int value )
{
autoExpand( index, 4 );
@@ -1423,748 +2193,34 @@
return buf.asDoubleBuffer();
}
- public String getHexDump()
+ public ByteBuffer expand( int expectedRemaining )
{
- return ByteBufferHexDumper.getHexdump( this );
+ if( autoExpand )
+ {
+ int pos = buf.position();
+ int limit = buf.limit();
+ int end = pos + expectedRemaining;
+ if( end > limit ) {
+ ensureCapacity( end );
+ buf.limit( end );
+ }
+ }
+ return this;
}
-
- public String getString( CharsetDecoder decoder ) throws CharacterCodingException
+
+ public ByteBuffer expand( int pos, int expectedRemaining )
{
- if( !buf.hasRemaining() )
+ if( autoExpand )
{
- return "";
+ int limit = buf.limit();
+ int end = pos + expectedRemaining;
+ if( end > limit ) {
+ ensureCapacity( end );
+ buf.limit( end );
+ }
}
-
- boolean utf16 = decoder.charset().name().startsWith( "UTF-16" );
-
- int oldPos = buf.position();
- int oldLimit = buf.limit();
- int end;
-
- if( !utf16 )
- {
- while( buf.hasRemaining() )
- {
- if( buf.get() == 0 )
- {
- break;
- }
- }
-
- end = buf.position();
- if( end == oldLimit )
- {
- buf.limit( end );
- }
- else
- {
- buf.limit( end - 1 );
- }
- }
- else
- {
- while( buf.remaining() >= 2 )
- {
- if( ( buf.get() == 0 ) && ( buf.get() == 0 ) )
- {
- break;
- }
- }
-
- end = buf.position();
- if( end == oldLimit || end == oldLimit - 1 )
- {
- buf.limit( end );
- }
- else
- {
- buf.limit( end - 2 );
- }
- }
-
- buf.position( oldPos );
- if( !buf.hasRemaining() )
- {
- buf.limit( oldLimit );
- buf.position( end );
- return "";
- }
- decoder.reset();
-
- int expectedLength = (int) ( buf.remaining() * decoder.averageCharsPerByte() );
- CharBuffer out = CharBuffer.allocate( expectedLength );
- for( ;; )
- {
- CoderResult cr;
- if ( buf.hasRemaining() )
- {
- cr = decoder.decode( buf, out, true );
- }
- else
- {
- cr = decoder.flush( out );
- }
-
- if ( cr.isUnderflow() )
- {
- break;
- }
-
- if ( cr.isOverflow() )
- {
- CharBuffer o = CharBuffer.allocate( out.capacity() + expectedLength );
- out.flip();
- o.put(out);
- out = o;
- continue;
- }
-
- cr.throwException();
- }
-
- buf.limit( oldLimit );
- buf.position( end );
- return out.flip().toString();
- }
-
- public String getString( int fieldSize, CharsetDecoder decoder ) throws CharacterCodingException
- {
- checkFieldSize( fieldSize );
-
- if( fieldSize == 0 )
- {
- return "";
- }
-
- if( !buf.hasRemaining() )
- {
- return "";
- }
-
- boolean utf16 = decoder.charset().name().startsWith( "UTF-16" );
-
- if( utf16 && ( ( fieldSize & 1 ) != 0 ) )
- {
- throw new IllegalArgumentException( "fieldSize is not even." );
- }
-
- int i;
- int oldPos = buf.position();
- int oldLimit = buf.limit();
- int end = buf.position() + fieldSize;
-
- if( oldLimit < end )
- {
- throw new BufferUnderflowException();
- }
-
- if( !utf16 )
- {
- for( i = 0; i < fieldSize; i ++ )
- {
- if( buf.get() == 0 )
- {
- break;
- }
- }
-
- if( i == fieldSize )
- {
- buf.limit( end );
- }
- else
- {
- buf.limit( buf.position() - 1 );
- }
- }
- else
- {
- for( i = 0; i < fieldSize; i += 2 )
- {
- if( ( buf.get() == 0 ) && ( buf.get() == 0 ) )
- {
- break;
- }
- }
-
- if( i == fieldSize )
- {
- buf.limit( end );
- }
- else
- {
- buf.limit( buf.position() - 2 );
- }
- }
-
- buf.position( oldPos );
- if( !buf.hasRemaining() )
- {
- buf.limit( oldLimit );
- buf.position( end );
- return "";
- }
- decoder.reset();
-
- int expectedLength = (int) ( buf.remaining() * decoder.averageCharsPerByte() );
- CharBuffer out = CharBuffer.allocate( expectedLength );
- for( ;; )
- {
- CoderResult cr;
- if ( buf.hasRemaining() )
- {
- cr = decoder.decode( buf, out, true );
- }
- else
- {
- cr = decoder.flush( out );
- }
-
- if ( cr.isUnderflow() )
- {
- break;
- }
-
- if ( cr.isOverflow() )
- {
- CharBuffer o = CharBuffer.allocate( out.capacity() + expectedLength );
- out.flip();
- o.put(out);
- out = o;
- continue;
- }
-
- cr.throwException();
- }
-
- buf.limit( oldLimit );
- buf.position( end );
- return out.flip().toString();
- }
-
- public ByteBuffer putString(
- CharSequence val, int fieldSize, CharsetEncoder encoder ) throws CharacterCodingException
- {
- checkFieldSize( fieldSize );
-
- if( fieldSize == 0 )
- return this;
-
- autoExpand( fieldSize );
-
- boolean utf16 = encoder.charset().name().startsWith( "UTF-16" );
-
- if( utf16 && ( ( fieldSize & 1 ) != 0 ) )
- {
- throw new IllegalArgumentException( "fieldSize is not even." );
- }
-
- int oldLimit = buf.limit();
- int end = buf.position() + fieldSize;
-
- if( oldLimit < end )
- {
- throw new BufferOverflowException();
- }
-
- if( val.length() == 0 )
- {
- if( !utf16 )
- {
- buf.put( ( byte ) 0x00 );
- }
- else
- {
- buf.put( ( byte ) 0x00 );
- buf.put( ( byte ) 0x00 );
- }
- buf.position( end );
- return this;
- }
-
- CharBuffer in = CharBuffer.wrap( val );
- buf.limit( end );
- encoder.reset();
-
- for (;;) {
- CoderResult cr;
- if( in.hasRemaining() )
- {
- cr = encoder.encode( in, buf(), true );
- }
- else
- {
- cr = encoder.flush( buf() );
- }
-
- if( cr.isUnderflow() || cr.isOverflow() )
- {
- break;
- }
- cr.throwException();
- }
-
- buf.limit( oldLimit );
-
- if( buf.position() < end )
- {
- if( !utf16 )
- {
- buf.put( ( byte ) 0x00 );
- }
- else
- {
- buf.put( ( byte ) 0x00 );
- buf.put( ( byte ) 0x00 );
- }
- }
-
- buf.position( end );
- return this;
- }
-
- public ByteBuffer putString(
- CharSequence val, CharsetEncoder encoder ) throws CharacterCodingException
- {
- if( val.length() == 0 )
- {
- return this;
- }
-
- CharBuffer in = CharBuffer.wrap( val );
- int expectedLength = (int) (in.remaining() * encoder.averageBytesPerChar());
-
- encoder.reset();
-
- for (;;) {
- CoderResult cr;
- if( in.hasRemaining() )
- {
- cr = encoder.encode( in, buf(), true );
- }
- else
- {
- cr = encoder.flush( buf() );
- }
-
- if( cr.isUnderflow() )
- {
- break;
- }
- if( cr.isOverflow() && autoExpand )
- {
- autoExpand( expectedLength );
- continue;
- }
- cr.throwException();
- }
- return this;
- }
-
- public String getPrefixedString( int prefixLength, CharsetDecoder decoder ) throws CharacterCodingException
- {
- int fieldSize;
-
- switch( prefixLength )
- {
- case 1:
- fieldSize = getUnsigned();
- break;
- case 2:
- fieldSize = getUnsignedShort();
- break;
- case 4:
- fieldSize = getInt();
- break;
- default:
- throw new IllegalArgumentException( "prefixLength: " + prefixLength );
- }
-
- if( fieldSize < 0 )
- {
- throw new BufferDataException( "Invalid fieldSize: " + fieldSize );
- }
-
- if( fieldSize == 0 )
- {
- return "";
- }
-
- boolean utf16 = decoder.charset().name().startsWith( "UTF-16" );
-
- if( utf16 && ( ( fieldSize & 1 ) != 0 ) )
- {
- throw new BufferDataException( "fieldSize is not even for a UTF-16 string." );
- }
-
- int oldLimit = buf.limit();
- int end = buf.position() + fieldSize;
-
- if( oldLimit < end )
- {
- throw new BufferUnderflowException();
- }
-
- buf.limit( end );
- decoder.reset();
-
- int expectedLength = (int) ( buf.remaining() * decoder.averageCharsPerByte() );
- CharBuffer out = CharBuffer.allocate( expectedLength );
- for( ;; )
- {
- CoderResult cr;
- if ( buf.hasRemaining() )
- {
- cr = decoder.decode( buf, out, true );
- }
- else
- {
- cr = decoder.flush( out );
- }
-
- if ( cr.isUnderflow() )
- {
- break;
- }
-
- if ( cr.isOverflow() )
- {
- CharBuffer o = CharBuffer.allocate( out.capacity() + expectedLength );
- out.flip();
- o.put(out);
- out = o;
- continue;
- }
-
- cr.throwException();
- }
-
- buf.limit( oldLimit );
- buf.position( end );
- return out.flip().toString();
- }
-
- public ByteBuffer putPrefixedString( CharSequence val, int prefixLength, int padding, byte padValue, CharsetEncoder encoder ) throws CharacterCodingException
- {
- int maxLength;
- switch( prefixLength )
- {
- case 1:
- maxLength = 255;
- break;
- case 2:
- maxLength = 65535;
- break;
- case 4:
- maxLength = Integer.MAX_VALUE;
- break;
- default:
- throw new IllegalArgumentException( "prefixLength: " + prefixLength );
- }
-
- if( val.length() > maxLength )
- {
- throw new IllegalArgumentException( "The specified string is too long." );
- }
- if( val.length() == 0 )
- {
- switch( prefixLength )
- {
- case 1:
- put( ( byte ) 0 );
- break;
- case 2:
- putShort( ( short ) 0 );
- break;
- case 4:
- putInt( 0 );
- break;
- }
- return this;
- }
-
- int padMask;
- switch( padding )
- {
- case 0:
- case 1:
- padMask = 0;
- break;
- case 2:
- padMask = 1;
- break;
- case 4:
- padMask = 3;
- break;
- default:
- throw new IllegalArgumentException( "padding: " + padding );
- }
-
- CharBuffer in = CharBuffer.wrap( val );
- int expectedLength = (int) (in.remaining() * encoder.averageBytesPerChar());
-
- skip( prefixLength ); // make a room for the length field
- int oldPos = position();
- encoder.reset();
-
- for (;;) {
- CoderResult cr;
- if( in.hasRemaining() )
- {
- cr = encoder.encode( in, buf(), true );
- }
- else
- {
- cr = encoder.flush( buf() );
- }
-
- if( position() - oldPos > maxLength )
- {
- throw new IllegalArgumentException( "The specified string is too long." );
- }
-
- if( cr.isUnderflow() )
- {
- break;
- }
- if( cr.isOverflow() && autoExpand )
- {
- autoExpand( expectedLength );
- continue;
- }
- cr.throwException();
- }
-
- // Write the length field
- fill( padValue, padding - ( ( position() - oldPos ) & padMask ) );
- int length = position() - oldPos;
- switch( prefixLength )
- {
- case 1:
- put( oldPos - 1, ( byte ) length );
- break;
- case 2:
- putShort( oldPos - 2, ( short ) length );
- break;
- case 4:
- putInt( oldPos - 4, length );
- break;
- }
- return this;
- }
-
- public Object getObject( final ClassLoader classLoader ) throws ClassNotFoundException
- {
- int length = getInt();
- if( length > remaining() )
- {
- throw new BufferUnderflowException();
- }
- if( length <= 4 )
- {
- throw new BufferDataException( "Object length should be greater than 4: " + length );
- }
-
- int oldLimit = limit();
- limit( position() + length );
- try
- {
- ObjectInputStream in = new ObjectInputStream( asInputStream() )
- {
- protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException
- {
- String className = readUTF();
- Class clazz = Class.forName( className, true, classLoader );
- ObjectStreamClass descriptor = ObjectStreamClass.lookup(clazz);
- return descriptor;
- }
- };
- return in.readObject();
- }
- catch( IOException e )
- {
- throw new BufferDataException( e );
- }
- finally
- {
- limit( oldLimit );
- }
- }
-
- public ByteBuffer putObject( Object o )
- {
- int oldPos = position();
- skip( 4 ); // Make a room for the length field.
- try
- {
- ObjectOutputStream out = new ObjectOutputStream( asOutputStream() )
- {
- protected void writeClassDescriptor( ObjectStreamClass desc ) throws IOException
- {
- writeUTF( desc.getName() );
- }
- };
- out.writeObject( o );
- out.flush();
- }
- catch( IOException e )
- {
- throw new BufferDataException( e );
- }
-
- // Fill the length field
- int newPos = position();
- position( oldPos );
- putInt( newPos - oldPos - 4 );
- position( newPos );
- return this;
- }
-
- public ByteBuffer skip( int size )
- {
- autoExpand( size );
- return position( position() + size );
- }
-
- public ByteBuffer fill( byte value, int size )
- {
- autoExpand( size );
- int q = size >>> 3;
- int r = size & 7;
-
- if( q > 0 )
- {
- int intValue = value | ( value << 8 ) | ( value << 16 )
- | ( value << 24 );
- long longValue = intValue;
- longValue <<= 32;
- longValue |= intValue;
-
- for( int i = q; i > 0; i -- )
- {
- buf.putLong( longValue );
- }
- }
-
- q = r >>> 2;
- r = r & 3;
-
- if( q > 0 )
- {
- int intValue = value | ( value << 8 ) | ( value << 16 )
- | ( value << 24 );
- buf.putInt( intValue );
- }
-
- q = r >> 1;
- r = r & 1;
-
- if( q > 0 )
- {
- short shortValue = ( short ) ( value | ( value << 8 ) );
- buf.putShort( shortValue );
- }
-
- if( r > 0 )
- {
- buf.put( value );
- }
-
- return this;
- }
-
- public ByteBuffer fillAndReset( byte value, int size )
- {
- autoExpand( size );
- int pos = buf.position();
- try
- {
- fill( value, size );
- }
- finally
- {
- buf.position( pos );
- }
- return this;
- }
-
- public ByteBuffer fill( int size )
- {
- autoExpand( size );
- int q = size >>> 3;
- int r = size & 7;
-
- for( int i = q; i > 0; i -- )
- {
- buf.putLong( 0L );
- }
-
- q = r >>> 2;
- r = r & 3;
-
- if( q > 0 )
- {
- buf.putInt( 0 );
- }
-
- q = r >> 1;
- r = r & 1;
-
- if( q > 0 )
- {
- buf.putShort( ( short ) 0 );
- }
-
- if( r > 0 )
- {
- buf.put( ( byte ) 0 );
- }
-
- return this;
- }
-
- public ByteBuffer fillAndReset( int size )
- {
- autoExpand( size );
- int pos = buf.position();
- try
- {
- fill( size );
- }
- finally
- {
- buf.position( pos );
- }
-
return this;
}
-
- private void autoExpand( int delta )
- {
- if( autoExpand )
- {
- int pos = buf.position();
- int limit = buf.limit();
- int end = pos + delta;
- if( end > limit ) {
- ensureCapacity( end );
- buf.limit( end );
- }
- }
- }
-
- private void autoExpand( int pos, int delta )
- {
- if( autoExpand )
- {
- int limit = buf.limit();
- int end = pos + delta;
- if( end > limit ) {
- ensureCapacity( end ); // expand by 50%
- buf.limit( end );
- }
- }
- }
private void ensureCapacity( int requestedCapacity )
{
@@ -2194,15 +2250,5 @@
this.buf = newBuf;
release0( oldBuf );
}
-
- private static void checkFieldSize( int fieldSize )
- {
- if( fieldSize < 0 )
- {
- throw new IllegalArgumentException(
- "fieldSize cannot be negative: " + fieldSize );
- }
- }
-
}
}
Modified: directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java?rev=354698&r1=354697&r2=354698&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java Tue Dec 6 20:06:01 2005
@@ -250,9 +250,9 @@
return buf.equals( ob );
}
- public int compareTo( ByteBuffer that )
+ public int compareTo( Object o )
{
- return buf.compareTo( that );
+ return buf.compareTo( o );
}
public ByteOrder order()
@@ -499,6 +499,12 @@
return this;
}
+ public ByteBuffer putPrefixedString( CharSequence in, int prefixLength, CharsetEncoder encoder ) throws CharacterCodingException
+ {
+ buf.putPrefixedString( in, prefixLength, encoder );
+ return this;
+ }
+
public ByteBuffer putPrefixedString( CharSequence in, int prefixLength, int padding, CharsetEncoder encoder )
throws CharacterCodingException
{
@@ -554,6 +560,18 @@
return this;
}
+ public ByteBuffer expand( int pos, int expectedRemaining )
+ {
+ buf.expand( pos, expectedRemaining );
+ return this;
+ }
+
+ public ByteBuffer expand( int expectedRemaining )
+ {
+ buf.expand( expectedRemaining );
+ return this;
+ }
+
public boolean isPooled()
{
return buf.isPooled();