Search in sources :

Example 1 with LittleEndianBinaryReader

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);
    }
}
Also used : PMessage(net.morimekta.providence.PMessage) PServiceCallType(net.morimekta.providence.PServiceCallType) IOException(java.io.IOException) PField(net.morimekta.providence.descriptor.PField) PApplicationException(net.morimekta.providence.PApplicationException) PServiceCall(net.morimekta.providence.PServiceCall) LittleEndianBinaryReader(net.morimekta.util.io.LittleEndianBinaryReader) PServiceMethod(net.morimekta.providence.descriptor.PServiceMethod) Nonnull(javax.annotation.Nonnull)

Aggregations

IOException (java.io.IOException)1 Nonnull (javax.annotation.Nonnull)1 PApplicationException (net.morimekta.providence.PApplicationException)1 PMessage (net.morimekta.providence.PMessage)1 PServiceCall (net.morimekta.providence.PServiceCall)1 PServiceCallType (net.morimekta.providence.PServiceCallType)1 PField (net.morimekta.providence.descriptor.PField)1 PServiceMethod (net.morimekta.providence.descriptor.PServiceMethod)1 LittleEndianBinaryReader (net.morimekta.util.io.LittleEndianBinaryReader)1