Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: fix java.nio.ByteBuffer Java 9+ incompatible usage #6839

Merged
merged 1 commit into from Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -18,6 +18,7 @@

import java.io.IOException;
import java.io.OutputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.ArrayDeque;
import java.util.Queue;
Expand Down Expand Up @@ -101,16 +102,18 @@ public int readInternal(ReadableBuffer buffer, int length) {

@Override
public void readBytes(final ByteBuffer dest) {
// Use Buffer instead of ByteBuffer for JDK 9+ compatibility.
final Buffer destAsBuffer = dest;
execute(new ReadOperation() {
@Override
public int readInternal(ReadableBuffer buffer, int length) {
// Change the limit so that only lengthToCopy bytes are available.
int prevLimit = dest.limit();
dest.limit(dest.position() + length);
int prevLimit = destAsBuffer.limit();
destAsBuffer.limit(destAsBuffer.position() + length);

// Write the bytes and restore the original limit.
buffer.readBytes(dest);
dest.limit(prevLimit);
destAsBuffer.limit(prevLimit);
return 0;
}
}, dest.remaining());
Expand Down
18 changes: 10 additions & 8 deletions core/src/main/java/io/grpc/internal/ReadableBuffers.java
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

Expand Down Expand Up @@ -209,7 +210,8 @@ public int arrayOffset() {
* A {@link ReadableBuffer} that is backed by a {@link ByteBuffer}.
*/
private static class ByteReadableBufferWrapper extends AbstractReadableBuffer {
final ByteBuffer bytes;
// Use Buffer instead of ByteBuffer for JDK 9+ compatibility.
final Buffer bytes;

ByteReadableBufferWrapper(ByteBuffer bytes) {
this.bytes = Preconditions.checkNotNull(bytes, "bytes");
Expand All @@ -223,7 +225,7 @@ public int readableBytes() {
@Override
public int readUnsignedByte() {
checkReadable(1);
return bytes.get() & 0xFF;
return ((ByteBuffer) bytes).get() & 0xFF;
}

@Override
Expand All @@ -235,7 +237,7 @@ public void skipBytes(int length) {
@Override
public void readBytes(byte[] dest, int destOffset, int length) {
checkReadable(length);
bytes.get(dest, destOffset, length);
((ByteBuffer) bytes).get(dest, destOffset, length);
}

@Override
Expand All @@ -249,7 +251,7 @@ public void readBytes(ByteBuffer dest) {
bytes.limit(bytes.position() + length);

// Write the bytes and restore the original limit.
dest.put(bytes);
dest.put((ByteBuffer) bytes);
bytes.limit(prevLimit);
}

Expand All @@ -262,16 +264,16 @@ public void readBytes(OutputStream dest, int length) throws IOException {
} else {
// The buffer doesn't support array(). Copy the data to an intermediate buffer.
byte[] array = new byte[length];
bytes.get(array);
((ByteBuffer) bytes).get(array);
dest.write(array);
}
}

@Override
public ByteReadableBufferWrapper readBytes(int length) {
checkReadable(length);
ByteBuffer buffer = bytes.duplicate();
buffer.limit(bytes.position() + length);
ByteBuffer buffer = ((ByteBuffer) bytes).duplicate();
((Buffer) buffer).limit(bytes.position() + length);
bytes.position(bytes.position() + length);
return new ByteReadableBufferWrapper(buffer);
}
Expand All @@ -283,7 +285,7 @@ public boolean hasArray() {

@Override
public byte[] array() {
return bytes.array();
return ((ByteBuffer) bytes).array();
}

@Override
Expand Down