Skip to content

Commit

Permalink
Simplify buffer enlarge
Browse files Browse the repository at this point in the history
  • Loading branch information
marianobarrios committed Oct 23, 2021
1 parent 7162f2d commit 92e2ac6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 41 deletions.
22 changes: 3 additions & 19 deletions src/main/java/tlschannel/impl/BufferHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
Expand Down
36 changes: 27 additions & 9 deletions src/main/java/tlschannel/impl/ByteBufferSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down
20 changes: 7 additions & 13 deletions src/main/java/tlschannel/impl/TlsChannelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 92e2ac6

Please sign in to comment.