Skip to content

Commit

Permalink
core: fix java.nio.ByteBuffer Java 9+ incompatible usage
Browse files Browse the repository at this point in the history
  • Loading branch information
dapengzhang0 committed Mar 18, 2020
1 parent b06f888 commit e739eea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
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

0 comments on commit e739eea

Please sign in to comment.