Cast ByteBuffer to Buffer before calling position(int) (#26614)

* Cast ByteBuffer to Buffer before calling position(int)

* Add comments
This commit is contained in:
William Godbe 2020-10-06 10:17:57 -07:00 committed by GitHub
parent 8b741bd9c6
commit d5b5f08a4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 2 deletions

View File

@ -5,6 +5,7 @@ package com.microsoft.signalr.messagepack;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
@ -121,13 +122,17 @@ public class MessagePackHubProtocol implements HubProtocol {
// Check what the last message was
// If it was an invocation binding failure, we have to correct the position of the buffer
if (hubMessages.get(hubMessages.size() - 1).getMessageType() == HubMessageType.INVOCATION_BINDING_FAILURE) {
payload.position(payload.position() + (length - readBytes));
// Cast to a Buffer to avoid the Java 9+ behavior where ByteBuffer.position(int) overrides Buffer.position(int),
// Returning a ByteBuffer rather than a Buffer. This causes issues on Android - see https://github.com/dotnet/aspnetcore/pull/26614
((Buffer) payload).position(payload.position() + (length - readBytes));
} else {
throw new RuntimeException(String.format("MessagePack message was length %d but claimed to be length %d.", readBytes, length));
}
}
unpacker.close();
payload.position(payload.position() + readBytes);
// Cast to a Buffer to avoid the Java 9+ behavior where ByteBuffer.position(int) overrides Buffer.position(int),
// Returning a ByteBuffer rather than a Buffer. This causes issues on Android - see https://github.com/dotnet/aspnetcore/pull/26614
((Buffer) payload).position(payload.position() + readBytes);
} catch (MessagePackException | IOException ex) {
throw new RuntimeException("Error reading MessagePack data.", ex);
}