Skip to content

Commit e739eea

Browse files
authored
core: fix java.nio.ByteBuffer Java 9+ incompatible usage
1 parent b06f888 commit e739eea

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

core/src/main/java/io/grpc/internal/CompositeReadableBuffer.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.io.OutputStream;
21+
import java.nio.Buffer;
2122
import java.nio.ByteBuffer;
2223
import java.util.ArrayDeque;
2324
import java.util.Queue;
@@ -101,16 +102,18 @@ public int readInternal(ReadableBuffer buffer, int length) {
101102

102103
@Override
103104
public void readBytes(final ByteBuffer dest) {
105+
// Use Buffer instead of ByteBuffer for JDK 9+ compatibility.
106+
final Buffer destAsBuffer = dest;
104107
execute(new ReadOperation() {
105108
@Override
106109
public int readInternal(ReadableBuffer buffer, int length) {
107110
// Change the limit so that only lengthToCopy bytes are available.
108-
int prevLimit = dest.limit();
109-
dest.limit(dest.position() + length);
111+
int prevLimit = destAsBuffer.limit();
112+
destAsBuffer.limit(destAsBuffer.position() + length);
110113

111114
// Write the bytes and restore the original limit.
112115
buffer.readBytes(dest);
113-
dest.limit(prevLimit);
116+
destAsBuffer.limit(prevLimit);
114117
return 0;
115118
}
116119
}, dest.remaining());

core/src/main/java/io/grpc/internal/ReadableBuffers.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.IOException;
2424
import java.io.InputStream;
2525
import java.io.OutputStream;
26+
import java.nio.Buffer;
2627
import java.nio.ByteBuffer;
2728
import java.nio.charset.Charset;
2829

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

214216
ByteReadableBufferWrapper(ByteBuffer bytes) {
215217
this.bytes = Preconditions.checkNotNull(bytes, "bytes");
@@ -223,7 +225,7 @@ public int readableBytes() {
223225
@Override
224226
public int readUnsignedByte() {
225227
checkReadable(1);
226-
return bytes.get() & 0xFF;
228+
return ((ByteBuffer) bytes).get() & 0xFF;
227229
}
228230

229231
@Override
@@ -235,7 +237,7 @@ public void skipBytes(int length) {
235237
@Override
236238
public void readBytes(byte[] dest, int destOffset, int length) {
237239
checkReadable(length);
238-
bytes.get(dest, destOffset, length);
240+
((ByteBuffer) bytes).get(dest, destOffset, length);
239241
}
240242

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

251253
// Write the bytes and restore the original limit.
252-
dest.put(bytes);
254+
dest.put((ByteBuffer) bytes);
253255
bytes.limit(prevLimit);
254256
}
255257

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

270272
@Override
271273
public ByteReadableBufferWrapper readBytes(int length) {
272274
checkReadable(length);
273-
ByteBuffer buffer = bytes.duplicate();
274-
buffer.limit(bytes.position() + length);
275+
ByteBuffer buffer = ((ByteBuffer) bytes).duplicate();
276+
((Buffer) buffer).limit(bytes.position() + length);
275277
bytes.position(bytes.position() + length);
276278
return new ByteReadableBufferWrapper(buffer);
277279
}
@@ -283,7 +285,7 @@ public boolean hasArray() {
283285

284286
@Override
285287
public byte[] array() {
286-
return bytes.array();
288+
return ((ByteBuffer) bytes).array();
287289
}
288290

289291
@Override

0 commit comments

Comments
 (0)