Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
package com.microsoft.signalr;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
public final class GsonHubProtocol implements HubProtocol {
private final Gson gson;
private static final String RECORD_SEPARATOR = "\u001e";
public GsonHubProtocol() {
this(new Gson());
}
public GsonHubProtocol(Gson gson) {
this.gson = gson;
}
@Override
public String getName() {
return "json";
}
@Override
public int getVersion() {
return 1;
}
@Override
public List parseMessages(ByteBuffer payload, InvocationBinder binder) {
String payloadStr;
// If the payload is readOnly, we have to copy the bytes from its array to make the payload string
if (payload.isReadOnly()) {
byte[] payloadBytes = new byte[payload.remaining()];
payload.get(payloadBytes, 0, payloadBytes.length);
payloadStr = new String(payloadBytes, StandardCharsets.UTF_8);
// Otherwise we can allocate directly from its array
} else {
// The position of the ByteBuffer may have been incremented - make sure we only grab the remaining bytes
payloadStr = new String(payload.array(), payload.position(), payload.remaining(), StandardCharsets.UTF_8);
}
if (payloadStr.length() == 0) {
return null;
}
if (!(payloadStr.substring(payloadStr.length() - 1).equals(RECORD_SEPARATOR))) {
throw new RuntimeException("Message is incomplete.");
}
String[] messages = payloadStr.split(RECORD_SEPARATOR);
List hubMessages = new ArrayList<>();
try {
for (String str : messages) {
HubMessageType messageType = null;
String invocationId = null;
String target = null;
String error = null;
ArrayList