From 44bce7575f7e66d899efcab679ba1583c276adb0 Mon Sep 17 00:00:00 2001 From: Sandy McArthur Date: Fri, 11 Dec 2015 01:14:50 -0500 Subject: [PATCH] Avoids the slow reflection needed to resize an undersized array to hold all objects in the collection. --- core/src/main/java/net/tomp2p/storage/Data.java | 4 +++- core/src/main/java/net/tomp2p/storage/DataBuffer.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/net/tomp2p/storage/Data.java b/core/src/main/java/net/tomp2p/storage/Data.java index 95874e619..012d4d1d8 100644 --- a/core/src/main/java/net/tomp2p/storage/Data.java +++ b/core/src/main/java/net/tomp2p/storage/Data.java @@ -26,6 +26,7 @@ import java.security.SignatureException; import java.util.BitSet; import java.util.HashSet; +import java.util.List; import java.util.Set; import net.tomp2p.connection.DSASignatureFactory; @@ -816,7 +817,8 @@ public byte[] toBytes() { * @return The ByteBuffers that is the payload. We do not make a copy here */ public ByteBuffer[] toByteBuffers() { - return buffer.bufferList().toArray(new ByteBuffer[0]); + List byteBuffers = buffer.bufferList(); + return byteBuffers.toArray(new ByteBuffer[byteBuffers.size()]); } public PublicKey publicKey() { diff --git a/core/src/main/java/net/tomp2p/storage/DataBuffer.java b/core/src/main/java/net/tomp2p/storage/DataBuffer.java index 2dbf77969..e5ef4d5b1 100644 --- a/core/src/main/java/net/tomp2p/storage/DataBuffer.java +++ b/core/src/main/java/net/tomp2p/storage/DataBuffer.java @@ -161,7 +161,7 @@ public ByteBuf toByteBuf() { } else { final DataBuffer copy = shallowCopyIntern(); //wrap does a slice, so a derived buffer, not increasing ref count - return Unpooled.wrappedBuffer(copy.buffers.toArray(new ByteBuf[0])); + return Unpooled.wrappedBuffer(copy.buffers.toArray(new ByteBuf[copy.buffers.size()])); } }