Author: trustin
Date: Wed Dec 13 00:59:40 2006
New Revision: 486551
URL: http://svn.apache.org/viewvc?view=rev&rev=486551
Log:
Made policy on derived buffers more strict and robust by disallowing expansion
of the parent buffer, which causes disparity between the parent and its derived
buffers.
Modified:
mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java
mina/trunk/core/src/main/java/org/apache/mina/common/ByteBufferProxy.java
mina/trunk/core/src/main/java/org/apache/mina/common/SimpleByteBufferAllocator.java
mina/trunk/core/src/main/java/org/apache/mina/common/support/BaseByteBuffer.java
mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
Modified: mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java?view=diff&rev=486551&r1=486550&r2=486551
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java
(original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java Wed
Dec 13 00:59:40 2006
@@ -108,9 +108,10 @@
* Derived buffers are the buffers which were created by
* [EMAIL PROTECTED] #duplicate()}, [EMAIL PROTECTED] #slice()}, or [EMAIL
PROTECTED] #asReadOnlyBuffer()}.
* They are useful especially when you broadcast the same messages to
- * multiple [EMAIL PROTECTED] IoSession}s. Please note that the derived
buffers are
- * not auto-expandable. Trying to expand a derived buffer will raise
- * [EMAIL PROTECTED] IllegalStateException}.
+ * multiple [EMAIL PROTECTED] IoSession}s. Please note that the buffer
derived from and
+ * its derived buffers are not both auto-expandable. Trying to call
+ * [EMAIL PROTECTED] #setAutoExpand(boolean)} with <tt>true</tt> parameter will
+ * raise an [EMAIL PROTECTED] IllegalStateException}.
* </p>
*
* <h2>Changing Buffer Allocation Policy</h2>
Modified:
mina/trunk/core/src/main/java/org/apache/mina/common/ByteBufferProxy.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/ByteBufferProxy.java?view=diff&rev=486551&r1=486550&r2=486551
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/ByteBufferProxy.java
(original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/ByteBufferProxy.java
Wed Dec 13 00:59:40 2006
@@ -68,7 +68,7 @@
{
return buf.isDirect();
}
-
+
public java.nio.ByteBuffer buf()
{
return buf.buf();
Modified:
mina/trunk/core/src/main/java/org/apache/mina/common/SimpleByteBufferAllocator.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/SimpleByteBufferAllocator.java?view=diff&rev=486551&r1=486550&r2=486551
==============================================================================
---
mina/trunk/core/src/main/java/org/apache/mina/common/SimpleByteBufferAllocator.java
(original)
+++
mina/trunk/core/src/main/java/org/apache/mina/common/SimpleByteBufferAllocator.java
Wed Dec 13 00:59:40 2006
@@ -49,12 +49,12 @@
{
nioBuffer = java.nio.ByteBuffer.allocate( capacity );
}
- return new SimpleByteBuffer( nioBuffer, false );
+ return new SimpleByteBuffer( nioBuffer, true );
}
public ByteBuffer wrap( java.nio.ByteBuffer nioBuffer )
{
- return new SimpleByteBuffer( nioBuffer, false );
+ return new SimpleByteBuffer( nioBuffer, true );
}
public void dispose()
@@ -64,12 +64,11 @@
private static class SimpleByteBuffer extends BaseByteBuffer
{
private java.nio.ByteBuffer buf;
- private boolean derived;
- protected SimpleByteBuffer( java.nio.ByteBuffer buf, boolean derived )
+ protected SimpleByteBuffer( java.nio.ByteBuffer buf, boolean
autoExpandAllowed )
{
+ super( autoExpandAllowed );
this.buf = buf;
- this.derived = derived;
buf.order( ByteOrder.BIG_ENDIAN );
}
@@ -80,11 +79,6 @@
protected void capacity0( int requestedCapacity )
{
- if( derived )
- {
- throw new IllegalStateException( "Derived buffers cannot be
expanded." );
- }
-
int newCapacity = MINIMUM_CAPACITY;
while( newCapacity < requestedCapacity )
{
@@ -108,16 +102,19 @@
this.buf = newBuf;
}
- public ByteBuffer duplicate() {
- return new SimpleByteBuffer( this.buf.duplicate(), true );
+ protected ByteBuffer duplicate0()
+ {
+ return new SimpleByteBuffer( this.buf.duplicate(), false );
}
- public ByteBuffer slice() {
- return new SimpleByteBuffer( this.buf.slice(), true );
+ protected ByteBuffer slice0()
+ {
+ return new SimpleByteBuffer( this.buf.slice(), false );
}
- public ByteBuffer asReadOnlyBuffer() {
- return new SimpleByteBuffer( this.buf.asReadOnlyBuffer(), true );
+ protected ByteBuffer asReadOnlyBuffer0()
+ {
+ return new SimpleByteBuffer( this.buf.asReadOnlyBuffer(), false );
}
public byte[] array()
Modified:
mina/trunk/core/src/main/java/org/apache/mina/common/support/BaseByteBuffer.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/support/BaseByteBuffer.java?view=diff&rev=486551&r1=486550&r2=486551
==============================================================================
---
mina/trunk/core/src/main/java/org/apache/mina/common/support/BaseByteBuffer.java
(original)
+++
mina/trunk/core/src/main/java/org/apache/mina/common/support/BaseByteBuffer.java
Wed Dec 13 00:59:40 2006
@@ -44,6 +44,7 @@
public abstract class BaseByteBuffer extends ByteBuffer
{
private boolean autoExpand;
+ private boolean autoExpandAllowed = true;
/**
* We don't have any access to Buffer.markValue(), so we need to track it
down,
@@ -53,6 +54,11 @@
protected BaseByteBuffer()
{
+ this( true );
+ }
+ protected BaseByteBuffer( boolean autoExpandAllowed )
+ {
+ this.autoExpandAllowed = autoExpandAllowed;
}
public boolean isDirect()
@@ -101,11 +107,16 @@
public boolean isAutoExpand()
{
- return autoExpand;
+ return autoExpand && autoExpandAllowed;
}
public ByteBuffer setAutoExpand( boolean autoExpand )
{
+ if( !autoExpandAllowed )
+ {
+ throw new IllegalStateException(
+ "Derived buffers can't be auto-expandable." );
+ }
this.autoExpand = autoExpand;
return this;
}
@@ -433,4 +444,43 @@
{
return buf().asDoubleBuffer();
}
+
+ @Override
+ public final ByteBuffer asReadOnlyBuffer()
+ {
+ autoExpandAllowed = false;
+ return asReadOnlyBuffer0();
+ }
+
+ /**
+ * Implement this method to return the unexpandable read only version of
+ * this buffer.
+ */
+ protected abstract ByteBuffer asReadOnlyBuffer0();
+
+ @Override
+ public final ByteBuffer duplicate()
+ {
+ autoExpandAllowed = false;
+ return duplicate0();
+ }
+
+ /**
+ * Implement this method to return the unexpandable duplicate of this
+ * buffer.
+ */
+ protected abstract ByteBuffer duplicate0();
+
+ @Override
+ public final ByteBuffer slice()
+ {
+ autoExpandAllowed = false;
+ return slice0();
+ }
+
+ /**
+ * Implement this method to return the unexpandable slice of this
+ * buffer.
+ */
+ protected abstract ByteBuffer slice0();
}
Modified:
mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java?view=diff&rev=486551&r1=486550&r2=486551
==============================================================================
--- mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
(original)
+++ mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
Wed Dec 13 00:59:40 2006
@@ -573,14 +573,24 @@
Assert.assertSame( original.buf().array(), duplicate.buf().array() );
// Try to expand.
+ original = ByteBuffer.allocate( 16 );
+ original.setAutoExpand( true );
+ duplicate = original.duplicate();
+ Assert.assertFalse( original.isAutoExpand() );
+
+ try
+ {
+ original.setAutoExpand( true );
+ Assert.fail();
+ }
+ catch( IllegalStateException e )
+ {
+ // OK
+ }
+
try
{
- original = ByteBuffer.allocate( 16 );
- duplicate = original.duplicate();
duplicate.setAutoExpand( true );
- duplicate.putString(
- "A very very very very looooooong string",
- Charset.forName( "ISO-8859-1" ).newEncoder() );
Assert.fail();
}
catch( IllegalStateException e )