Search in sources :

Example 1 with DecodeException

use of org.apache.qpid.protonj2.codec.DecodeException in project qpid-protonj2 by apache.

the class ApplicationPropertiesTypeDecoder method readArrayElements.

@Override
public ApplicationProperties[] readArrayElements(InputStream stream, StreamDecoderState state, int count) throws DecodeException {
    final StreamTypeDecoder<?> decoder = state.getDecoder().readNextTypeDecoder(stream, state);
    final ApplicationProperties[] result = new ApplicationProperties[count];
    if (decoder instanceof NullTypeDecoder) {
        for (int i = 0; i < count; ++i) {
            result[i] = new ApplicationProperties(null);
        }
        return result;
    }
    for (int i = 0; i < count; ++i) {
        result[i] = new ApplicationProperties(readMap(stream, state, checkIsExpectedTypeAndCast(MapTypeDecoder.class, decoder)));
    }
    return result;
}
Also used : NullTypeDecoder(org.apache.qpid.protonj2.codec.decoders.primitives.NullTypeDecoder) ApplicationProperties(org.apache.qpid.protonj2.types.messaging.ApplicationProperties) MapTypeDecoder(org.apache.qpid.protonj2.codec.decoders.primitives.MapTypeDecoder)

Example 2 with DecodeException

use of org.apache.qpid.protonj2.codec.DecodeException in project qpid-protonj2 by apache.

the class DeliveryAnnotationsTypeDecoder method readMap.

private Map<Symbol, Object> readMap(ProtonBuffer buffer, DecoderState state, MapTypeDecoder mapDecoder) throws DecodeException {
    final int size = mapDecoder.readSize(buffer);
    final int count = mapDecoder.readCount(buffer);
    if (count > buffer.getReadableBytes()) {
        throw new DecodeException(String.format("Map encoded size %d is specified to be greater than the amount " + "of data available (%d)", size, buffer.getReadableBytes()));
    }
    // Count include both key and value so we must include that in the loop
    final Map<Symbol, Object> map = new LinkedHashMap<>(count);
    for (int i = 0; i < count / 2; i++) {
        Symbol key = state.getDecoder().readSymbol(buffer, state);
        Object value = state.getDecoder().readObject(buffer, state);
        map.put(key, value);
    }
    return map;
}
Also used : Symbol(org.apache.qpid.protonj2.types.Symbol) DecodeException(org.apache.qpid.protonj2.codec.DecodeException) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with DecodeException

use of org.apache.qpid.protonj2.codec.DecodeException in project qpid-protonj2 by apache.

the class DeliveryAnnotationsTypeDecoder method readArrayElements.

@Override
public DeliveryAnnotations[] readArrayElements(ProtonBuffer buffer, DecoderState state, int count) throws DecodeException {
    final TypeDecoder<?> decoder = state.getDecoder().readNextTypeDecoder(buffer, state);
    final DeliveryAnnotations[] result = new DeliveryAnnotations[count];
    if (decoder instanceof NullTypeDecoder) {
        for (int i = 0; i < count; ++i) {
            decoder.readValue(buffer, state);
            result[i] = new DeliveryAnnotations(null);
        }
        return result;
    }
    for (int i = 0; i < count; ++i) {
        result[i] = new DeliveryAnnotations(readMap(buffer, state, checkIsExpectedTypeAndCast(MapTypeDecoder.class, decoder)));
    }
    return result;
}
Also used : NullTypeDecoder(org.apache.qpid.protonj2.codec.decoders.primitives.NullTypeDecoder) DeliveryAnnotations(org.apache.qpid.protonj2.types.messaging.DeliveryAnnotations) MapTypeDecoder(org.apache.qpid.protonj2.codec.decoders.primitives.MapTypeDecoder)

Example 4 with DecodeException

use of org.apache.qpid.protonj2.codec.DecodeException in project qpid-protonj2 by apache.

the class DeliveryAnnotationsTypeDecoder method readArrayElements.

@Override
public DeliveryAnnotations[] readArrayElements(InputStream stream, StreamDecoderState state, int count) throws DecodeException {
    final StreamTypeDecoder<?> decoder = state.getDecoder().readNextTypeDecoder(stream, state);
    final DeliveryAnnotations[] result = new DeliveryAnnotations[count];
    if (decoder instanceof NullTypeDecoder) {
        for (int i = 0; i < count; ++i) {
            decoder.readValue(stream, state);
            result[i] = new DeliveryAnnotations(null);
        }
        return result;
    }
    for (int i = 0; i < count; ++i) {
        result[i] = new DeliveryAnnotations(readMap(stream, state, checkIsExpectedTypeAndCast(MapTypeDecoder.class, decoder)));
    }
    return result;
}
Also used : NullTypeDecoder(org.apache.qpid.protonj2.codec.decoders.primitives.NullTypeDecoder) DeliveryAnnotations(org.apache.qpid.protonj2.types.messaging.DeliveryAnnotations) MapTypeDecoder(org.apache.qpid.protonj2.codec.decoders.primitives.MapTypeDecoder)

Example 5 with DecodeException

use of org.apache.qpid.protonj2.codec.DecodeException in project qpid-protonj2 by apache.

the class ProtonStreamDecoderState method internalDecodeUTF8.

private static String internalDecodeUTF8(final InputStream stream, final int length, final char[] chars, final byte stoppageByte, final int offset, final CharsetDecoder decoder) throws IOException {
    final CharBuffer out = CharBuffer.wrap(chars);
    out.position(offset);
    // Create a buffer from the remaining portion of the buffer and then use the decoder to complete the work
    // remember to move the main buffer position to consume the data processed.
    final byte[] trailingBytes = new byte[length - offset];
    trailingBytes[0] = stoppageByte;
    stream.read(trailingBytes, 1, trailingBytes.length - 1);
    ByteBuffer byteBuffer = ByteBuffer.wrap(trailingBytes);
    try {
        for (; ; ) {
            CoderResult cr = byteBuffer.hasRemaining() ? decoder.decode(byteBuffer, out, true) : CoderResult.UNDERFLOW;
            if (cr.isUnderflow()) {
                cr = decoder.flush(out);
            }
            if (cr.isUnderflow()) {
                break;
            }
            // The char buffer should have been sufficient here but wasn't so we know
            // that there was some encoding issue on the other end.
            cr.throwException();
        }
        return out.flip().toString();
    } catch (CharacterCodingException e) {
        throw new DecodeException("Cannot parse encoded UTF8 String", e);
    } finally {
        decoder.reset();
    }
}
Also used : CharBuffer(java.nio.CharBuffer) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer) DecodeException(org.apache.qpid.protonj2.codec.DecodeException) CoderResult(java.nio.charset.CoderResult)

Aggregations

DecodeException (org.apache.qpid.protonj2.codec.DecodeException)240 ProtonBuffer (org.apache.qpid.protonj2.buffer.ProtonBuffer)180 InputStream (java.io.InputStream)167 ProtonBufferInputStream (org.apache.qpid.protonj2.buffer.ProtonBufferInputStream)167 Symbol (org.apache.qpid.protonj2.types.Symbol)20 Test (org.junit.jupiter.api.Test)16 MapTypeDecoder (org.apache.qpid.protonj2.codec.decoders.primitives.MapTypeDecoder)11 NullTypeDecoder (org.apache.qpid.protonj2.codec.decoders.primitives.NullTypeDecoder)11 ErrorCondition (org.apache.qpid.protonj2.types.transport.ErrorCondition)9 ListTypeDecoder (org.apache.qpid.protonj2.codec.decoders.primitives.ListTypeDecoder)8 LinkedHashMap (java.util.LinkedHashMap)7 Binary (org.apache.qpid.protonj2.types.Binary)4 UnsignedInteger (org.apache.qpid.protonj2.types.UnsignedInteger)4 AmqpSequence (org.apache.qpid.protonj2.types.messaging.AmqpSequence)4 Data (org.apache.qpid.protonj2.types.messaging.Data)4 Header (org.apache.qpid.protonj2.types.messaging.Header)4 DeliveryState (org.apache.qpid.protonj2.types.transport.DeliveryState)4 IOException (java.io.IOException)3 UUID (java.util.UUID)3 Footer (org.apache.qpid.protonj2.types.messaging.Footer)3