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, alts, cronet: fix ByteBuffer covariant method usages #7349

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -102,18 +102,15 @@ 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.
voidzcy marked this conversation as resolved.
Show resolved Hide resolved
int prevLimit = destAsBuffer.limit();
destAsBuffer.limit(destAsBuffer.position() + length);
int prevLimit = dest.limit();
((Buffer) dest).limit(dest.position() + length);

// Write the bytes and restore the original limit.
buffer.readBytes(dest);
destAsBuffer.limit(prevLimit);
((Buffer) dest).limit(prevLimit);
return 0;
}
}, dest.remaining());
Expand Down
23 changes: 11 additions & 12 deletions core/src/main/java/io/grpc/internal/ReadableBuffers.java
Expand Up @@ -210,8 +210,7 @@ public int arrayOffset() {
* A {@link ReadableBuffer} that is backed by a {@link ByteBuffer}.
*/
private static class ByteReadableBufferWrapper extends AbstractReadableBuffer {
// Use Buffer instead of ByteBuffer for JDK 9+ compatibility.
final Buffer bytes;
final ByteBuffer bytes;

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

@Override
public void skipBytes(int length) {
checkReadable(length);
bytes.position(bytes.position() + length);
((Buffer) bytes).position(bytes.position() + length);
}

@Override
public void readBytes(byte[] dest, int destOffset, int length) {
checkReadable(length);
((ByteBuffer) bytes).get(dest, destOffset, length);
bytes.get(dest, destOffset, length);
}

@Override
Expand All @@ -248,10 +247,10 @@ public void readBytes(ByteBuffer dest) {

// Change the limit so that only length bytes are available.
int prevLimit = bytes.limit();
bytes.limit(bytes.position() + length);
((Buffer) bytes).limit(bytes.position() + length);

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

Expand All @@ -260,21 +259,21 @@ public void readBytes(OutputStream dest, int length) throws IOException {
checkReadable(length);
if (hasArray()) {
dest.write(array(), arrayOffset(), length);
bytes.position(bytes.position() + length);
((Buffer) bytes).position(bytes.position() + length);
} else {
// The buffer doesn't support array(). Copy the data to an intermediate buffer.
byte[] array = new byte[length];
((ByteBuffer) bytes).get(array);
bytes.get(array);
dest.write(array);
}
}

@Override
public ByteReadableBufferWrapper readBytes(int length) {
checkReadable(length);
ByteBuffer buffer = ((ByteBuffer) bytes).duplicate();
ByteBuffer buffer = bytes.duplicate();
((Buffer) buffer).limit(bytes.position() + length);
bytes.position(bytes.position() + length);
((Buffer) bytes).position(bytes.position() + length);
return new ByteReadableBufferWrapper(buffer);
}

Expand All @@ -285,7 +284,7 @@ public boolean hasArray() {

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

@Override
Expand Down
Expand Up @@ -23,6 +23,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -119,17 +120,17 @@ public void readByteBufferShouldSucceed() {
ByteBuffer byteBuffer = ByteBuffer.allocate(EXPECTED_VALUE.length());
int remaining = EXPECTED_VALUE.length();

byteBuffer.limit(1);
((Buffer) byteBuffer).limit(1);
composite.readBytes(byteBuffer);
remaining--;
assertEquals(remaining, composite.readableBytes());

byteBuffer.limit(byteBuffer.limit() + 5);
((Buffer) byteBuffer).limit(byteBuffer.limit() + 5);
composite.readBytes(byteBuffer);
remaining -= 5;
assertEquals(remaining, composite.readableBytes());

byteBuffer.limit(byteBuffer.limit() + remaining);
((Buffer) byteBuffer).limit(byteBuffer.limit() + remaining);
composite.readBytes(byteBuffer);
assertEquals(0, composite.readableBytes());
assertEquals(EXPECTED_VALUE, new String(byteBuffer.array(), UTF_8));
Expand Down
Expand Up @@ -21,6 +21,7 @@
import static org.junit.Assert.assertEquals;

import java.io.ByteArrayOutputStream;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.junit.Test;
Expand Down Expand Up @@ -86,7 +87,7 @@ public void readToByteBufferShouldSucceed() {
ReadableBuffer buffer = buffer();
ByteBuffer byteBuffer = ByteBuffer.allocate(msg.length());
buffer.readBytes(byteBuffer);
byteBuffer.flip();
((Buffer) byteBuffer).flip();
byte[] array = new byte[msg.length()];
byteBuffer.get(array);
assertArrayEquals(msg.getBytes(UTF_8), array);
Expand All @@ -98,7 +99,7 @@ public void partialReadToByteBufferShouldSucceed() {
ReadableBuffer buffer = buffer();
ByteBuffer byteBuffer = ByteBuffer.allocate(2);
buffer.readBytes(byteBuffer);
byteBuffer.flip();
((Buffer) byteBuffer).flip();
byte[] array = new byte[2];
byteBuffer.get(array);
assertArrayEquals(new byte[]{'h', 'e'}, array);
Expand Down