From 92e2ac6af41137d5ded20ebe2ab4431687d61e06 Mon Sep 17 00:00:00 2001 From: Mariano Barrios Date: Sun, 24 Oct 2021 01:33:40 +0200 Subject: [PATCH] Simplify buffer enlarge --- .../java/tlschannel/impl/BufferHolder.java | 22 ++---------- .../java/tlschannel/impl/ByteBufferSet.java | 36 ++++++++++++++----- .../java/tlschannel/impl/TlsChannelImpl.java | 20 ++++------- 3 files changed, 37 insertions(+), 41 deletions(-) diff --git a/src/main/java/tlschannel/impl/BufferHolder.java b/src/main/java/tlschannel/impl/BufferHolder.java index a17417da..093b38a0 100644 --- a/src/main/java/tlschannel/impl/BufferHolder.java +++ b/src/main/java/tlschannel/impl/BufferHolder.java @@ -61,19 +61,6 @@ public boolean dispose() { } } - public void resize(int newCapacity) { - if (newCapacity > maxSize) - throw new IllegalArgumentException( - String.format( - "new capacity (%s) bigger than absolute max size (%s)", newCapacity, maxSize)); - logger.trace( - "resizing buffer {}, increasing from {} to {} (manual sizing)", - name, - buffer.capacity(), - newCapacity); - resizeImpl(newCapacity); - } - public void enlarge() { if (buffer.capacity() >= maxSize) { throw new IllegalStateException( @@ -82,14 +69,11 @@ public void enlarge() { } int newCapacity = Math.min(buffer.capacity() * 2, maxSize); logger.trace( - "enlarging buffer {}, increasing from {} to {} (automatic enlarge)", - name, - buffer.capacity(), - newCapacity); - resizeImpl(newCapacity); + "enlarging buffer {}, increasing from {} to {}", name, buffer.capacity(), newCapacity); + resize(newCapacity); } - private void resizeImpl(int newCapacity) { + private void resize(int newCapacity) { ByteBuffer newBuffer = allocator.allocate(newCapacity); buffer.flip(); newBuffer.put(buffer); diff --git a/src/main/java/tlschannel/impl/ByteBufferSet.java b/src/main/java/tlschannel/impl/ByteBufferSet.java index 594faf9a..e0ca048e 100644 --- a/src/main/java/tlschannel/impl/ByteBufferSet.java +++ b/src/main/java/tlschannel/impl/ByteBufferSet.java @@ -10,11 +10,19 @@ public class ByteBufferSet { public final int length; public ByteBufferSet(ByteBuffer[] array, int offset, int length) { - if (array == null) throw new NullPointerException(); - if (array.length < offset) throw new IndexOutOfBoundsException(); - if (array.length < offset + length) throw new IndexOutOfBoundsException(); + if (array == null) { + throw new NullPointerException(); + } + if (array.length < offset) { + throw new IndexOutOfBoundsException(); + } + if (array.length < offset + length) { + throw new IndexOutOfBoundsException(); + } for (int i = offset; i < offset + length; i++) { - if (array[i] == null) throw new NullPointerException(); + if (array[i] == null) { + throw new NullPointerException(); + } } this.array = array; this.offset = offset; @@ -48,7 +56,9 @@ public long position() { public int putRemaining(ByteBuffer from) { int totalBytes = 0; for (int i = offset; i < offset + length; i++) { - if (!from.hasRemaining()) break; + if (!from.hasRemaining()) { + break; + } ByteBuffer dstBuffer = array[i]; int bytes = Math.min(from.remaining(), dstBuffer.remaining()); ByteBufferUtil.copy(from, dstBuffer, bytes); @@ -67,7 +77,9 @@ public ByteBufferSet put(ByteBuffer from, int length) { int totalBytes = 0; for (int i = offset; i < offset + this.length; i++) { int pending = length - totalBytes; - if (pending == 0) break; + if (pending == 0) { + break; + } int bytes = Math.min(pending, (int) remaining()); ByteBuffer dstBuffer = array[i]; ByteBufferUtil.copy(from, dstBuffer, bytes); @@ -79,7 +91,9 @@ public ByteBufferSet put(ByteBuffer from, int length) { public int getRemaining(ByteBuffer dst) { int totalBytes = 0; for (int i = offset; i < offset + length; i++) { - if (!dst.hasRemaining()) break; + if (!dst.hasRemaining()) { + break; + } ByteBuffer srcBuffer = array[i]; int bytes = Math.min(dst.remaining(), srcBuffer.remaining()); ByteBufferUtil.copy(srcBuffer, dst, bytes); @@ -98,7 +112,9 @@ public ByteBufferSet get(ByteBuffer dst, int length) { int totalBytes = 0; for (int i = offset; i < offset + this.length; i++) { int pending = length - totalBytes; - if (pending == 0) break; + if (pending == 0) { + break; + } ByteBuffer srcBuffer = array[i]; int bytes = Math.min(pending, srcBuffer.remaining()); ByteBufferUtil.copy(srcBuffer, dst, bytes); @@ -113,7 +129,9 @@ public boolean hasRemaining() { public boolean isReadOnly() { for (int i = offset; i < offset + length; i++) { - if (array[i].isReadOnly()) return true; + if (array[i].isReadOnly()) { + return true; + } } return false; } diff --git a/src/main/java/tlschannel/impl/TlsChannelImpl.java b/src/main/java/tlschannel/impl/TlsChannelImpl.java index 534402fc..1edf51a8 100644 --- a/src/main/java/tlschannel/impl/TlsChannelImpl.java +++ b/src/main/java/tlschannel/impl/TlsChannelImpl.java @@ -261,6 +261,7 @@ private SSLEngineResult unwrapLoop() throws SSLException { Util.assertTrue(inPlain.nullOrEmpty()); SSLEngineResult result = callEngineUnwrap(effDest); HandshakeStatus status = engine.getHandshakeStatus(); + /* * Note that data can be returned even in case of overflow, in that * case, just return the data. @@ -274,6 +275,7 @@ private SSLEngineResult unwrapLoop() throws SSLException { if (result.getStatus() == Status.BUFFER_UNDERFLOW) { return result; } + if (result.getHandshakeStatus() == HandshakeStatus.FINISHED || status == HandshakeStatus.NEED_TASK || status == HandshakeStatus.NEED_WRAP) { @@ -283,15 +285,17 @@ private SSLEngineResult unwrapLoop() throws SSLException { if (effDest == suppliedInPlain) { /* * The client-supplier buffer is not big enough. Use the - * internal inPlain buffer, also ensure that it is bigger + * internal inPlain buffer. Also ensure that it is bigger * than the too-small supplied one. */ inPlain.prepare(); - ensureInPlainCapacity( - Math.min(((int) suppliedInPlain.remaining()) * 2, maxTlsPacketSize)); + if (inPlain.buffer.capacity() <= suppliedInPlain.remaining()) { + inPlain.enlarge(); + } } else { inPlain.enlarge(); } + // inPlain changed, re-create the wrapper effDest = new ByteBufferSet(inPlain.buffer); } @@ -420,16 +424,6 @@ private SSLEngineResult callEngineWrap(ByteBufferSet source) throws SSLException } } - private void ensureInPlainCapacity(int newCapacity) { - if (inPlain.buffer.capacity() < newCapacity) { - logger.trace( - "inPlain buffer too small, increasing from {} to {}", - inPlain.buffer.capacity(), - newCapacity); - inPlain.resize(newCapacity); - } - } - private void writeToChannel() throws IOException { if (outEncrypted.buffer.position() == 0) { return;