Author: rgodfrey
Date: Thu Sep  3 23:28:28 2015
New Revision: 1701147

URL: http://svn.apache.org/r1701147
Log:
QPID-6662 : Ensure byte buffers are zeroed when returned to the pool

Modified:
    
qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/QpidByteBuffer.java

Modified: 
qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/QpidByteBuffer.java
URL: 
http://svn.apache.org/viewvc/qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/QpidByteBuffer.java?rev=1701147&r1=1701146&r2=1701147&view=diff
==============================================================================
--- 
qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/QpidByteBuffer.java
 (original)
+++ 
qpid/java/trunk/common/src/main/java/org/apache/qpid/bytebuffer/QpidByteBuffer.java
 Thu Sep  3 23:28:28 2015
@@ -37,6 +37,7 @@ import java.util.concurrent.ConcurrentHa
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
+import java.util.concurrent.atomic.AtomicReference;
 
 import javax.net.ssl.SSLEngine;
 import javax.net.ssl.SSLEngineResult;
@@ -57,8 +58,9 @@ public final class QpidByteBuffer
             "_disposed");
 
     private static final ThreadLocal<QpidByteBuffer> _cachedBuffer = new 
ThreadLocal<>();
+    private static final AtomicReference<ByteBuffer> ZEROED = new 
AtomicReference<>();
 
-    private ByteBuffer _buffer;
+    private volatile ByteBuffer _buffer;
     private final ByteBufferRef _ref;
     @SuppressWarnings("unused")
     private volatile int _disposed;
@@ -400,8 +402,9 @@ public final class QpidByteBuffer
     {
         ByteBuffer buf = _buffer.slice();
         buf.position(offset);
+        buf.limit(offset+Math.min(length, buf.remaining()));
         buf = buf.slice();
-        buf.limit(Math.min(length, buf.remaining()));
+
         return new QpidByteBuffer(buf, _ref);
     }
 
@@ -593,7 +596,18 @@ public final class QpidByteBuffer
 
     static void returnToPool(final ByteBuffer buffer)
     {
-        final BufferPool pool = _pools.get(buffer.capacity());
+        final int bufferSize = buffer.capacity();
+        final BufferPool pool = _pools.get(bufferSize);
+        ByteBuffer zeroed;;
+        while((zeroed = ZEROED.get()) == null || zeroed.capacity() < 
bufferSize)
+        {
+            ZEROED.compareAndSet(zeroed, 
ByteBuffer.allocateDirect(bufferSize));
+        }
+        buffer.clear();
+        final ByteBuffer duplicate = zeroed.duplicate();
+        duplicate.limit(bufferSize);
+        buffer.put(duplicate);
+
         pool.returnBuffer(buffer);
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org
For additional commands, e-mail: commits-h...@qpid.apache.org

Reply via email to