Skip to content

Commit

Permalink
Add some comments and assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
marianobarrios committed Jun 20, 2020
1 parent 8c656f6 commit 9aae055
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/tlschannel/impl/ByteBufferSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public long remaining() {
return ret;
}

public long position() {
long ret = 0;
for (int i = offset; i < offset + length; i++) {
ret += array[i].position();
}
return ret;
}

public int putRemaining(ByteBuffer from) {
int totalBytes = 0;
for (int i = offset; i < offset + length; i++) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/tlschannel/impl/TlsChannelImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,26 @@ public long read(ByteBufferSet dest) throws IOException, NeedsTaskException {
if (invalid || shutdownSent) {
throw new ClosedChannelException();
}

long originalDestPosition = dest.position();
suppliedInPlain = dest;
bytesToReturn = inPlain.nullOrEmpty() ? 0 : inPlain.buffer.position();

while (true) {

// return bytes are soon as we have them
if (bytesToReturn > 0) {
if (inPlain.nullOrEmpty()) {
// if there is not in internal buffer, that means that the bytes must be in the supplied
// buffer
Util.assertTrue(dest.position() == originalDestPosition + bytesToReturn);
return bytesToReturn;
} else {
Util.assertTrue(inPlain.buffer.position() == bytesToReturn);
return transferPendingPlain(dest);
}
}

if (shutdownReceived) {
return -1;
}
Expand Down Expand Up @@ -223,6 +232,7 @@ private void handleTask() throws NeedsTaskException {
}
}

/** Copies bytes from the internal input plain buffer to the supplied buffer. */
private int transferPendingPlain(ByteBufferSet dstBuffers) {
inPlain.buffer.flip(); // will read
int bytes = dstBuffers.putRemaining(inPlain.buffer);
Expand Down

0 comments on commit 9aae055

Please sign in to comment.