org.apache.qpid.proton.codec.WritableBuffer Java Examples
The following examples show how to use
org.apache.qpid.proton.codec.WritableBuffer.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AmqpCoreConverter.java From activemq-artemis with Apache License 2.0 | 6 votes |
private static void encodeUnsupportedMessagePropertyType(ServerJMSMessage jms, String key, Object value) throws JMSException { final ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(); final EncoderImpl encoder = TLSEncode.getEncoder(); try { encoder.setByteBuffer(new NettyWritable(buffer)); encoder.writeObject(value); final byte[] encodedBytes = new byte[buffer.writerIndex()]; buffer.readBytes(encodedBytes); setProperty(jms, key, encodedBytes); } finally { encoder.setByteBuffer((WritableBuffer) null); buffer.release(); } }
Example #2
Source File: FastPathApplicationPropertiesType.java From qpid-proton-j with Apache License 2.0 | 6 votes |
@Override public void write(ApplicationProperties val) { WritableBuffer buffer = getEncoder().getBuffer(); buffer.put(EncodingCodes.DESCRIBED_TYPE_INDICATOR); buffer.put(EncodingCodes.SMALLULONG); buffer.put(DESCRIPTOR_CODE); MapType mapType = (MapType) getEncoder().getType(val.getValue()); mapType.setKeyEncoding(stringType); try { mapType.write(val.getValue()); } finally { mapType.setKeyEncoding(null); } }
Example #3
Source File: FastPathMessageAnnotationsType.java From qpid-proton-j with Apache License 2.0 | 6 votes |
@Override public void write(MessageAnnotations val) { WritableBuffer buffer = getEncoder().getBuffer(); buffer.put(EncodingCodes.DESCRIBED_TYPE_INDICATOR); buffer.put(EncodingCodes.SMALLULONG); buffer.put(DESCRIPTOR_CODE); MapType mapType = (MapType) getEncoder().getType(val.getValue()); mapType.setKeyEncoding(symbolType); try { mapType.write(val.getValue()); } finally { mapType.setKeyEncoding(null); } }
Example #4
Source File: FastPathDeliveryAnnotationsType.java From qpid-proton-j with Apache License 2.0 | 6 votes |
@Override public void write(DeliveryAnnotations val) { WritableBuffer buffer = getEncoder().getBuffer(); buffer.put(EncodingCodes.DESCRIBED_TYPE_INDICATOR); buffer.put(EncodingCodes.SMALLULONG); buffer.put(DESCRIPTOR_CODE); MapType mapType = (MapType) getEncoder().getType(val.getValue()); mapType.setKeyEncoding(symbolType); try { mapType.write(val.getValue()); } finally { mapType.setKeyEncoding(null); } }
Example #5
Source File: NettyReadable.java From activemq-artemis with Apache License 2.0 | 6 votes |
@Override public ReadableBuffer get(WritableBuffer target) { int start = target.position(); if (buffer.hasArray()) { target.put(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), buffer.readableBytes()); } else { target.put(buffer.nioBuffer()); } int written = target.position() - start; buffer.readerIndex(buffer.readerIndex() + written); return this; }
Example #6
Source File: TestConversions.java From activemq-artemis with Apache License 2.0 | 6 votes |
private byte[] encodeObject(Object toEncode) { ByteBuf scratch = Unpooled.buffer(); EncoderImpl encoder = TLSEncode.getEncoder(); encoder.setByteBuffer(new NettyWritable(scratch)); try { encoder.writeObject(toEncode); } finally { encoder.setByteBuffer((WritableBuffer) null); } byte[] result = new byte[scratch.writerIndex()]; scratch.readBytes(result); return result; }
Example #7
Source File: ProtonReadableBufferImpl.java From vertx-proton with Apache License 2.0 | 6 votes |
@Override public ReadableBuffer get(WritableBuffer target) { int start = target.position(); if (buffer.hasArray()) { target.put(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), buffer.readableBytes()); } else { target.put(buffer.nioBuffer()); } int written = target.position() - start; buffer.readerIndex(buffer.readerIndex() + written); return this; }
Example #8
Source File: AmqpReadableBuffer.java From qpid-jms with Apache License 2.0 | 6 votes |
@Override public ReadableBuffer get(WritableBuffer target) { int start = target.position(); if (buffer.hasArray()) { target.put(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), buffer.readableBytes()); } else { target.put(buffer.nioBuffer()); } int written = target.position() - start; buffer.readerIndex(buffer.readerIndex() + written); return this; }
Example #9
Source File: DeliveryImpl.java From qpid-proton-j with Apache License 2.0 | 6 votes |
int recv(final WritableBuffer buffer) { final int consumed; if (_dataBuffer != null && _dataBuffer.hasRemaining()) { consumed = Math.min(buffer.remaining(), _dataBuffer.remaining()); buffer.put(_dataBuffer); _dataBuffer.reclaimRead(); } else { consumed = 0; } return (_complete && consumed == 0) ? Transport.END_OF_STREAM : consumed; }
Example #10
Source File: ReceiverImpl.java From qpid-proton-j with Apache License 2.0 | 6 votes |
@Override public int recv(final WritableBuffer buffer) { if (_current == null) { throw new IllegalStateException("no current delivery"); } int consumed = _current.recv(buffer); if (consumed > 0) { getSession().incrementIncomingBytes(-consumed); if (getSession().getTransportSession().getIncomingWindowSize().equals(UnsignedInteger.ZERO)) { modified(); } } return consumed; }
Example #11
Source File: AmqpFramer.java From qpid-proton-j with Apache License 2.0 | 5 votes |
/** * @param amqpFrameType indicates either AMQP or SASL * @param frameBody is currently expected to be a {@link FrameBody} or a {@link SaslFrameBody} */ public byte[] generateFrame(int channel, byte[] extendedHeader, Object frameBody, byte amqpFrameType) { assertEquals("Extended header must be multiple of 4 bytes", 0, extendedHeader.length % 4); int numberOfExtendedHeaderFourByteWords = extendedHeader.length / 4; ByteBuffer buffer = ByteBuffer.allocate(1024); buffer.position(8); // leave hole for frame header _encoder.setByteBuffer(new WritableBuffer.ByteBufferWrapper(buffer)); // write extended header - maybe empty buffer.put(extendedHeader); // write frame body if (frameBody != null) { _encoder.writeObject(frameBody); } int frameSize = buffer.position(); int framePreambleSizeInFourByteWords = 2; byte dataOffsetFourByteWords = (byte)(framePreambleSizeInFourByteWords + numberOfExtendedHeaderFourByteWords); buffer.rewind(); buffer.putInt(frameSize); buffer.put(dataOffsetFourByteWords); buffer.put(amqpFrameType); buffer.putShort((short)channel); byte[] target = new byte[frameSize]; buffer.rewind(); buffer.get(target, 0, frameSize); return target; }
Example #12
Source File: AmqpReadableBuffer.java From activemq-artemis with Apache License 2.0 | 5 votes |
@Override public ReadableBuffer get(WritableBuffer target) { int start = target.position(); if (this.buffer.hasArray()) { target.put(this.buffer.array(), this.buffer.arrayOffset() + this.buffer.position(), this.buffer.remaining()); } else { target.put(this.buffer); } int written = target.position() - start; this.buffer.position(this.buffer.position() + written); return this; }
Example #13
Source File: DeliveryImplTest.java From qpid-proton-j with Apache License 2.0 | 5 votes |
@Test public void testRecvWriteableBufferWithEmptyDelivery() throws Exception { DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); WritableBuffer buffer = WritableBuffer.ByteBufferWrapper.allocate(5); assertEquals(0, delivery.recv(buffer)); }
Example #14
Source File: MessageImplTest.java From qpid-proton-j with Apache License 2.0 | 5 votes |
void doMessageEncodingWithDataBodySectionTestImplUsingWritableBuffer(int bytesLength) { byte[] bytes = generateByteArray(bytesLength); byte[] expectedBytes = generateExpectedDataSectionBytes(bytes); ByteBufferWrapper encodedBytes = WritableBuffer.ByteBufferWrapper.allocate(expectedBytes.length); Message msg = Message.Factory.create(); msg.setBody(new Data(new Binary(bytes))); int encodedLength = msg.encode(encodedBytes); assertArrayEquals("Encoded bytes do not match expectation", expectedBytes, encodedBytes.byteBuffer().array()); assertEquals("Encoded length different than expected length", encodedLength, encodedBytes.position()); }
Example #15
Source File: StringsBenchmark.java From qpid-proton-j with Apache License 2.0 | 5 votes |
@Benchmark public WritableBuffer encodeStringMessage() { buffer.position(0); message.encode(buffer); return buffer; }
Example #16
Source File: MessageAnnotationsInjectExtractAdapterTest.java From hono with Eclipse Public License 2.0 | 5 votes |
/** * Verifies that the same entries injected via the {@code MessageAnnotationsInjectAdapter} are extracted via the * {@code MessageAnnotationsExtractAdapter}. * Also verifies that there are no errors during encoding/decoding of the message with the injected entries. */ @Test public void testInjectAndExtract() { final Map<String, String> testEntries = new HashMap<>(); testEntries.put("key1", "value1"); testEntries.put("key2", "value2"); final Message message = ProtonHelper.message(); // inject the properties final MessageAnnotationsInjectAdapter injectAdapter = new MessageAnnotationsInjectAdapter(message, propertiesMapName); testEntries.forEach((key, value) -> { injectAdapter.put(key, value); }); // encode the message final WritableBuffer.ByteBufferWrapper buffer = WritableBuffer.ByteBufferWrapper.allocate(100); message.encode(buffer); // decode the message final Message decodedMessage = ProtonHelper.message(); decodedMessage.decode(buffer.toReadableBuffer()); // extract the properties from the decoded message final MessageAnnotationsExtractAdapter extractAdapter = new MessageAnnotationsExtractAdapter(decodedMessage, propertiesMapName); extractAdapter.iterator().forEachRemaining(extractedEntry -> { assertThat(extractedEntry.getValue()).isEqualTo(testEntries.get(extractedEntry.getKey())); }); }
Example #17
Source File: AMQPMessage.java From activemq-artemis with Apache License 2.0 | 5 votes |
protected int getDeliveryAnnotationsForSendBufferSize() { if (deliveryAnnotationsForSendBuffer == null || deliveryAnnotationsForSendBuffer.getValue().isEmpty()) { return 0; } DroppingWritableBuffer droppingWritableBuffer = new DroppingWritableBuffer(); TLSEncode.getEncoder().setByteBuffer(droppingWritableBuffer); TLSEncode.getEncoder().writeObject(deliveryAnnotationsForSendBuffer); TLSEncode.getEncoder().setByteBuffer((WritableBuffer) null); return droppingWritableBuffer.position() + 1; }
Example #18
Source File: AMQPMessage.java From activemq-artemis with Apache License 2.0 | 5 votes |
protected void writeDeliveryAnnotationsForSendBuffer(ByteBuf result) { if (deliveryAnnotationsForSendBuffer != null && !deliveryAnnotationsForSendBuffer.getValue().isEmpty()) { TLSEncode.getEncoder().setByteBuffer(new NettyWritable(result)); TLSEncode.getEncoder().writeObject(deliveryAnnotationsForSendBuffer); TLSEncode.getEncoder().setByteBuffer((WritableBuffer) null); } }
Example #19
Source File: FastPathAmqpSequenceType.java From qpid-proton-j with Apache License 2.0 | 5 votes |
@Override public void write(AmqpSequence sequence) { WritableBuffer buffer = getEncoder().getBuffer(); buffer.put(EncodingCodes.DESCRIBED_TYPE_INDICATOR); buffer.put(EncodingCodes.SMALLULONG); buffer.put(DESCRIPTOR_CODE); getEncoder().writeObject(sequence.getValue()); }
Example #20
Source File: FastPathFooterType.java From qpid-proton-j with Apache License 2.0 | 5 votes |
@Override public void write(Footer val) { WritableBuffer buffer = getEncoder().getBuffer(); buffer.put(EncodingCodes.DESCRIBED_TYPE_INDICATOR); buffer.put(EncodingCodes.SMALLULONG); buffer.put(DESCRIPTOR_CODE); MapType mapType = (MapType) getEncoder().getType(val.getValue()); mapType.write(val.getValue()); }
Example #21
Source File: FastPathAmqpValueType.java From qpid-proton-j with Apache License 2.0 | 5 votes |
@Override public void write(AmqpValue value) { WritableBuffer buffer = getEncoder().getBuffer(); buffer.put(EncodingCodes.DESCRIBED_TYPE_INDICATOR); buffer.put(EncodingCodes.SMALLULONG); buffer.put(DESCRIPTOR_CODE); getEncoder().writeObject(value.getValue()); }
Example #22
Source File: FastPathDataType.java From qpid-proton-j with Apache License 2.0 | 5 votes |
@Override public void write(Data data) { WritableBuffer buffer = getEncoder().getBuffer(); buffer.put(EncodingCodes.DESCRIBED_TYPE_INDICATOR); buffer.put(EncodingCodes.SMALLULONG); buffer.put(DESCRIPTOR_CODE); getEncoder().writeBinary(data.getValue()); }
Example #23
Source File: AmqpCodec.java From qpid-jms with Apache License 2.0 | 5 votes |
/** * Given an AMQP Section encode it and return the buffer holding the encoded value * * @param section * the AMQP Section value to encode. * * @return a buffer holding the encoded bytes of the given AMQP Section object. */ public static ByteBuf encode(Section section) { if (section == null) { return null; } AmqpWritableBuffer buffer = new AmqpWritableBuffer(); EncoderImpl encoder = getEncoder(); encoder.setByteBuffer(buffer); encoder.writeObject(section); encoder.setByteBuffer((WritableBuffer) null); return buffer.getBuffer(); }
Example #24
Source File: Symbol.java From qpid-proton-j with Apache License 2.0 | 4 votes |
public void writeTo(WritableBuffer buffer) { buffer.put(_underlyingBytes, 0, _underlyingBytes.length); }
Example #25
Source File: AMQPStandardMessage.java From activemq-artemis with Apache License 2.0 | 4 votes |
@Override protected synchronized void encodeMessage() { this.modified = false; this.messageDataScanned = MessageDataScanningStatus.NOT_SCANNED.code; int estimated = Math.max(1500, data != null ? data.capacity() + 1000 : 0); ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(estimated); EncoderImpl encoder = TLSEncode.getEncoder(); try { NettyWritable writable = new NettyWritable(buffer); encoder.setByteBuffer(writable); if (header != null) { encoder.writeObject(header); } // We currently do not encode any delivery annotations but it is conceivable // that at some point they may need to be preserved, this is where that needs // to happen. if (messageAnnotations != null) { encoder.writeObject(messageAnnotations); } if (properties != null) { encoder.writeObject(properties); } // Whenever possible avoid encoding sections we don't need to by // checking if application properties where loaded or added and // encoding only in that case. if (applicationProperties != null) { encoder.writeObject(applicationProperties); // Now raw write the remainder body and footer if present. if (data != null && remainingBodyPosition != VALUE_NOT_PRESENT) { writable.put(data.position(remainingBodyPosition)); } } else if (data != null && applicationPropertiesPosition != VALUE_NOT_PRESENT) { // Writes out ApplicationProperties, Body and Footer in one go if present. writable.put(data.position(applicationPropertiesPosition)); } else if (data != null && remainingBodyPosition != VALUE_NOT_PRESENT) { // No Application properties at all so raw write Body and Footer sections writable.put(data.position(remainingBodyPosition)); } byte[] bytes = new byte[buffer.writerIndex()]; buffer.readBytes(bytes); data = ReadableBuffer.ByteBufferReader.wrap(ByteBuffer.wrap(bytes)); } finally { encoder.setByteBuffer((WritableBuffer) null); buffer.release(); } }
Example #26
Source File: AmqpCodec.java From qpid-jms with Apache License 2.0 | 4 votes |
/** * Given a Message instance, encode the Message to the wire level representation * of that Message. * * @param message * the Message that is to be encoded into the wire level representation. * * @return a buffer containing the wire level representation of the input Message. */ public static ByteBuf encodeMessage(AmqpJmsMessageFacade message) { EncoderDecoderContext context = TLS_CODEC.get(); AmqpWritableBuffer buffer = new AmqpWritableBuffer(); EncoderImpl encoder = context.encoder; encoder.setByteBuffer(buffer); Header header = message.getHeader(); DeliveryAnnotations deliveryAnnotations = message.getDeliveryAnnotations(); MessageAnnotations messageAnnotations = message.getMessageAnnotations(); Properties properties = message.getProperties(); ApplicationProperties applicationProperties = message.getApplicationProperties(); Section body = message.getBody(); Footer footer = message.getFooter(); if (header != null) { encoder.writeObject(header); } if (deliveryAnnotations != null) { encoder.writeObject(deliveryAnnotations); } if (messageAnnotations != null) { // Ensure annotations contain required message type and destination type data AmqpDestinationHelper.setReplyToAnnotationFromDestination(message.getReplyTo(), messageAnnotations); AmqpDestinationHelper.setToAnnotationFromDestination(message.getDestination(), messageAnnotations); messageAnnotations.getValue().put(AmqpMessageSupport.JMS_MSG_TYPE, message.getJmsMsgType()); encoder.writeObject(messageAnnotations); } else { buffer.put(getCachedMessageAnnotationsBuffer(message, context)); } if (properties != null) { encoder.writeObject(properties); } if (applicationProperties != null) { encoder.writeObject(applicationProperties); } if (body != null) { encoder.writeObject(body); } if (footer != null) { encoder.writeObject(footer); } encoder.setByteBuffer((WritableBuffer) null); return buffer.getBuffer(); }
Example #27
Source File: AmqpCodec.java From qpid-jms with Apache License 2.0 | 4 votes |
private static ReadableBuffer populateMessageAnnotationsCacheEntry(AmqpJmsMessageFacade message, Integer entryKey, EncoderDecoderContext context) { ReadableBuffer result = GLOBAL_ANNOTATIONS_CACHE.get(entryKey); if (result == null) { MessageAnnotations messageAnnotations = new MessageAnnotations(new HashMap<>()); // Sets the Reply To annotation which will likely not be present most of the time so do it first // to avoid extra work within the map operations. AmqpDestinationHelper.setReplyToAnnotationFromDestination(message.getReplyTo(), messageAnnotations); // Sets the To value's destination annotation set and a known JMS destination type likely to always // be present but we do allow of edge case of unknown types which won't encode an annotation. AmqpDestinationHelper.setToAnnotationFromDestination(message.getDestination(), messageAnnotations); // Now store the message type which we know will always be present so do it last to ensure // the previous calls don't need to compare anything to this value in the map during add or remove messageAnnotations.getValue().put(AmqpMessageSupport.JMS_MSG_TYPE, message.getJmsMsgType()); // This is the maximum possible encoding size that could appear for all the possible data we // store in the cached buffer if the codec was to do the worst possible encode of these types. // We could do a custom encoding to make it minimal which would result in a max of 70 bytes. ByteBuffer buffer = ByteBuffer.allocate(124); WritableBuffer oldBuffer = context.encoder.getBuffer(); context.encoder.setByteBuffer(buffer); context.encoder.writeObject(messageAnnotations); context.encoder.setByteBuffer(oldBuffer); buffer.flip(); result = ReadableBuffer.ByteBufferReader.wrap(buffer); // Race on populating the global cache could duplicate work but we should avoid keeping // both copies around in memory. ReadableBuffer previous = GLOBAL_ANNOTATIONS_CACHE.putIfAbsent(entryKey, result); if (previous != null) { result = previous.duplicate(); } else { result = result.duplicate(); } } else { result = result.duplicate(); } context.messageAnnotationsCache.put(entryKey, result); return result; }
Example #28
Source File: DeliveryImplTest.java From qpid-proton-j with Apache License 2.0 | 4 votes |
@Test public void testRecvWritableWhenIncomingIsOneArray() throws Exception { DeliveryImpl delivery = new DeliveryImpl(null, Mockito.mock(LinkImpl.class), null); byte[] data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; int length = data.length; delivery.append(data); assertEquals(length, delivery.available()); ByteBufferWrapper buffer = WritableBuffer.ByteBufferWrapper.allocate(length); assertEquals(length, delivery.recv(buffer)); ByteBuffer received = buffer.byteBuffer(); for (int i = 0; i < length; ++i) { assertEquals(received.get(i), data[i]); } assertEquals(0, delivery.recv(WritableBuffer.ByteBufferWrapper.allocate(length))); }
Example #29
Source File: FastPathAcceptedType.java From qpid-proton-j with Apache License 2.0 | 4 votes |
@Override public void write(Accepted accepted) { WritableBuffer buffer = getEncoder().getBuffer(); buffer.put(ACCEPTED_ENCODED_BYTES, 0, ACCEPTED_ENCODED_BYTES.length); }
Example #30
Source File: FastPathHeaderType.java From qpid-proton-j with Apache License 2.0 | 4 votes |
@Override public void write(Header value) { WritableBuffer buffer = getEncoder().getBuffer(); int count = getElementCount(value); byte encodingCode = deduceEncodingCode(value, count); buffer.put(EncodingCodes.DESCRIBED_TYPE_INDICATOR); buffer.put(EncodingCodes.SMALLULONG); buffer.put(DESCRIPTOR_CODE); buffer.put(encodingCode); // Optimized step, no other data to be written. if (encodingCode == EncodingCodes.LIST0) { return; } final int fieldWidth; if (encodingCode == EncodingCodes.LIST8) { fieldWidth = 1; } else { fieldWidth = 4; } int startIndex = buffer.position(); // Reserve space for the size and write the count of list elements. if (fieldWidth == 1) { buffer.put((byte) 0); buffer.put((byte) count); } else { buffer.putInt(0); buffer.putInt(count); } // Write the list elements and then compute total size written. for (int i = 0; i < count; ++i) { writeElement(value, i); } // Move back and write the size int endIndex = buffer.position(); int writeSize = endIndex - startIndex - fieldWidth; buffer.position(startIndex); if (fieldWidth == 1) { buffer.put((byte) writeSize); } else { buffer.putInt(writeSize); } buffer.position(endIndex); }