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;
}
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;
}
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;
}
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;
}
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();
}
}
Aggregations