Skip to content

Commit d5b5f08

Browse files
authored
Cast ByteBuffer to Buffer before calling position(int) (#26614)
* Cast ByteBuffer to Buffer before calling position(int) * Add comments
1 parent 8b741bd commit d5b5f08

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/SignalR/clients/java/signalr/messagepack/src/main/java/com/microsoft/signalr/messagepack/MessagePackHubProtocol.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.io.IOException;
77
import java.lang.reflect.Type;
8+
import java.nio.Buffer;
89
import java.nio.ByteBuffer;
910
import java.util.ArrayList;
1011
import java.util.Arrays;
@@ -121,13 +122,17 @@ public List<HubMessage> parseMessages(ByteBuffer payload, InvocationBinder binde
121122
// Check what the last message was
122123
// If it was an invocation binding failure, we have to correct the position of the buffer
123124
if (hubMessages.get(hubMessages.size() - 1).getMessageType() == HubMessageType.INVOCATION_BINDING_FAILURE) {
124-
payload.position(payload.position() + (length - readBytes));
125+
// Cast to a Buffer to avoid the Java 9+ behavior where ByteBuffer.position(int) overrides Buffer.position(int),
126+
// Returning a ByteBuffer rather than a Buffer. This causes issues on Android - see https://github.com/dotnet/aspnetcore/pull/26614
127+
((Buffer) payload).position(payload.position() + (length - readBytes));
125128
} else {
126129
throw new RuntimeException(String.format("MessagePack message was length %d but claimed to be length %d.", readBytes, length));
127130
}
128131
}
129132
unpacker.close();
130-
payload.position(payload.position() + readBytes);
133+
// Cast to a Buffer to avoid the Java 9+ behavior where ByteBuffer.position(int) overrides Buffer.position(int),
134+
// Returning a ByteBuffer rather than a Buffer. This causes issues on Android - see https://github.com/dotnet/aspnetcore/pull/26614
135+
((Buffer) payload).position(payload.position() + readBytes);
131136
} catch (MessagePackException | IOException ex) {
132137
throw new RuntimeException("Error reading MessagePack data.", ex);
133138
}

0 commit comments

Comments
 (0)