use of net.morimekta.util.io.LittleEndianBinaryReader in project providence by morimekta.
the class FastBinarySerializer method deserialize.
@Nonnull
@Override
@SuppressWarnings("unchecked")
public <Message extends PMessage<Message, Field>, Field extends PField> PServiceCall<Message, Field> deserialize(@Nonnull InputStream is, @Nonnull PService service) throws SerializerException {
String methodName = null;
int sequence = 0;
PServiceCallType type = null;
try {
LittleEndianBinaryReader in = new LittleEndianBinaryReader(is);
// Max method name length: 255 chars.
int tag = in.readIntVarint();
int len = tag >>> 3;
int typeKey = tag & 0x07;
methodName = new String(in.expectBytes(len), UTF_8);
sequence = in.readIntVarint();
type = PServiceCallType.findById(typeKey);
if (type == null) {
throw new SerializerException("Invalid call type " + typeKey).setExceptionType(PApplicationExceptionType.INVALID_MESSAGE_TYPE);
} else if (type == PServiceCallType.EXCEPTION) {
PApplicationException ex = readMessage(in, PApplicationException.kDescriptor);
return (PServiceCall<Message, Field>) new PServiceCall<>(methodName, type, sequence, ex);
}
PServiceMethod method = service.getMethod(methodName);
if (method == null) {
throw new SerializerException("No such method %s on %s", methodName, service.getQualifiedName()).setExceptionType(PApplicationExceptionType.UNKNOWN_METHOD);
}
@SuppressWarnings("unchecked") PMessageDescriptor<Message, Field> descriptor = isRequestCallType(type) ? method.getRequestType() : method.getResponseType();
if (descriptor == null) {
throw new SerializerException("No such %s descriptor for %s", isRequestCallType(type) ? "request" : "response", service.getQualifiedName()).setExceptionType(PApplicationExceptionType.UNKNOWN_METHOD);
}
Message message = readMessage(in, descriptor);
return new PServiceCall<>(methodName, type, sequence, message);
} catch (SerializerException e) {
throw new SerializerException(e).setCallType(type).setMethodName(methodName).setSequenceNo(sequence);
} catch (IOException e) {
throw new SerializerException(e, e.getMessage()).setCallType(type).setMethodName(methodName).setSequenceNo(sequence);
}
}
Aggregations