Java Code Examples for org.apache.qpid.proton.codec.ReadableBuffer#position()

The following examples show how to use org.apache.qpid.proton.codec.ReadableBuffer#position() . 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: DeliveryImpl.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
private byte[] copyContents(ReadableBuffer buffer)
{
    byte[] copy = new byte[buffer.remaining()];

    if (buffer.hasArray())
    {
        System.arraycopy(buffer.array(), buffer.arrayOffset() + buffer.position(), copy, 0, buffer.remaining());
        buffer.position(buffer.limit());
    }
    else
    {
        buffer.get(copy, 0, buffer.remaining());
    }

    return copy;
}
 
Example 2
Source File: Binary.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
public static Binary create(ReadableBuffer buffer)
{
    if (buffer == null)
    {
        return null;
    }
    else if (!buffer.hasArray())
    {
        byte data[] = new byte [buffer.remaining()];
        ReadableBuffer dup = buffer.duplicate();
        dup.get(data);
        return new Binary(data);
    }
    else
    {
        return new Binary(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
    }
}
 
Example 3
Source File: DeliveryImplTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Test
public void testSendSingleReadableBufferWithPartialContent() throws Exception
{
    DeliveryImpl delivery = createSenderDelivery();

    byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 };
    byte[] expected = new byte[] { 3, 4, 5 };
    ReadableBuffer buffer = ReadableBuffer.ByteBufferReader.wrap(data);
    // Now move the position forward so we only send some of the data
    buffer.position(3);

    delivery.send(buffer);

    assertEquals(expected.length, delivery.pending());
    assertEquals(expected.length, delivery.getData().remaining());

    CompositeReadableBuffer composite = (CompositeReadableBuffer) delivery.getData();

    assertNotSame(data, composite.array());
    assertNotSame(expected, composite.array());
    assertArrayEquals(expected, composite.array());
}
 
Example 4
Source File: AMQPMessage.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
protected ReadableBuffer createCopyWithSkippedOrExplicitlySetDeliveryAnnotations() {
   // The original message had delivery annotations, or delivery annotations for the send buffer are set.
   // That means we must copy into a new buffer skipping the original delivery annotations section
   // (not meant to survive beyond this hop) and including the delivery annotations for the send buffer if set.
   ReadableBuffer duplicate = getData().duplicate();

   final ByteBuf result = PooledByteBufAllocator.DEFAULT.heapBuffer(getEncodeSize());
   result.writeBytes(duplicate.limit(encodedHeaderSize).byteBuffer());
   writeDeliveryAnnotationsForSendBuffer(result);
   duplicate.clear();
   // skip existing delivery annotations of the original message
   duplicate.position(encodedHeaderSize + encodedDeliveryAnnotationsSize);
   result.writeBytes(duplicate.byteBuffer());

   return new NettyReadable(result);
}
 
Example 5
Source File: AMQPLargeMessage.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public void parseHeader(ReadableBuffer buffer) {

      DecoderImpl decoder = TLSEncode.getDecoder();
      decoder.setBuffer(buffer);

      try {
         int constructorPos = buffer.position();
         TypeConstructor<?> constructor = decoder.readConstructor();
         if (Header.class.equals(constructor.getTypeClass())) {
            header = (Header) constructor.readValue();
            if (header.getTtl() != null) {
               expiration = System.currentTimeMillis() + header.getTtl().intValue();
            }
         }
      } finally {
         decoder.setBuffer(null);
         buffer.rewind();
      }
   }
 
Example 6
Source File: FrameWriterBuffer.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public void put(ReadableBuffer payload) {
    final int toCopy = payload.remaining();
    ensureRemaining(toCopy);

    if (payload.hasArray()) {
        System.arraycopy(payload.array(), payload.arrayOffset() + payload.position(), array, position, toCopy);
        payload.position(payload.position() + toCopy);
    } else {
        payload.get(array, position, toCopy);
    }

    position += toCopy;
}
 
Example 7
Source File: DeliveryImplTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testSendSingleReadableBufferWithOffsetAndPartialContent() throws Exception
{
    DeliveryImpl delivery = createSenderDelivery();

    byte[] bytes = new byte[] { 0, 1, 2, 3, 4, 5 };
    ByteBuffer data = ByteBuffer.wrap(bytes, 0, 5); // Wrap, miss out the last byte from array
    data.position(1); // Now move the position forward
    ByteBuffer dataSlice = data.slice(); // Now slice, causing us to have an array offset at start of array

    byte[] expected = new byte[] { 2, 3, 4 };

    ReadableBuffer buffer = ReadableBuffer.ByteBufferReader.wrap(dataSlice);
    // Now move the position forward so we only send some of the data
    buffer.position(1);

    assertEquals("Unexpected array offset", 1, buffer.arrayOffset());
    assertEquals("Unexpected remaining", 3, buffer.remaining());

    delivery.send(buffer);

    assertEquals(expected.length, delivery.pending());
    assertEquals(expected.length, delivery.getData().remaining());

    CompositeReadableBuffer composite = (CompositeReadableBuffer) delivery.getData();

    assertNotSame(bytes, composite.array());
    assertNotSame(expected, composite.array());
    assertArrayEquals(expected, composite.array());
}
 
Example 8
Source File: AMQPMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
protected <T> T scanForMessageSection(int scanStartPosition, Class...targetTypes) {
   ensureMessageDataScanned();

   // In cases where we parsed out enough to know the value is not encoded in the message
   // we can exit without doing any reads or buffer hopping.
   if (scanStartPosition == VALUE_NOT_PRESENT) {
      return null;
   }

   ReadableBuffer buffer = getData().duplicate().position(0);
   final DecoderImpl decoder = TLSEncode.getDecoder();

   buffer.position(scanStartPosition);

   T section = null;

   decoder.setBuffer(buffer);
   try {
      while (buffer.hasRemaining()) {
         TypeConstructor<?> constructor = decoder.readConstructor();
         for (Class<?> type : targetTypes) {
            if (type.equals(constructor.getTypeClass())) {
               section = (T) constructor.readValue();
               return section;
            }
         }

         constructor.skipValue();
      }
   } finally {
      decoder.setBuffer(null);
   }

   return section;
}
 
Example 9
Source File: AMQPMessageSymbolSearch.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private static boolean lookupOnSection(IdentityHashMap<Class<?>, Boolean> stopSet, final Class section, final ReadableBuffer data, final KMPNeedle[] needles, final int startAt) {
   DecoderImpl decoder = TLSEncode.getDecoder();
   final int position = data.position();
   decoder.setBuffer(data.position(startAt));
   try {
      while (data.hasRemaining()) {
         TypeConstructor<?> constructor = decoder.readConstructor();
         final Class<?> typeClass = constructor.getTypeClass();
         if (MSG_ANNOTATIONS_STOPSET.containsKey(typeClass)) {
            if (section.equals(typeClass)) {
               final int start = data.position();
               constructor.skipValue();
               final int end = data.position();
               for (int i = 0, count = needles.length; i < count; i++) {
                  final int foundIndex = needles[i].searchInto(data, start, end);
                  if (foundIndex != -1) {
                     return true;
                  }
               }
            }
            return false;
         }
         constructor.skipValue();
      }
      return false;
   } finally {
      decoder.setBuffer(null);
      data.position(position);
   }
}
 
Example 10
Source File: DeliveryImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testAfterSendNoCopyClearsExternalReadableBuffer() throws Exception
{
    DeliveryImpl delivery = createSenderDelivery();

    byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 };
    ReadableBuffer buffer = ReadableBuffer.ByteBufferReader.wrap(data);

    delivery.sendNoCopy(buffer);

    ReadableBuffer sendBuffer = delivery.getData();

    assertEquals(data.length, sendBuffer.remaining());
    assertSame(buffer, sendBuffer);

    sendBuffer.position(sendBuffer.limit());

    delivery.afterSend();

    assertNotSame(buffer, delivery.getData());
}
 
Example 11
Source File: DeliveryImplTest.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testAfterSendNoCopyPreservesExternalReadableBufferIfNotDrained() throws Exception
{
    DeliveryImpl delivery = createSenderDelivery();

    byte[] data = new byte[] { 0, 1, 2, 3, 4, 5 };
    ReadableBuffer buffer = ReadableBuffer.ByteBufferReader.wrap(data);

    delivery.sendNoCopy(buffer);

    ReadableBuffer sendBuffer = delivery.getData();

    assertEquals(data.length, sendBuffer.remaining());
    assertSame(buffer, sendBuffer);

    sendBuffer.position(sendBuffer.limit() - 1);

    delivery.afterSend();

    assertSame(buffer, delivery.getData());
}
 
Example 12
Source File: AMQPMessage.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
protected synchronized void scanMessageData() {
   this.messageDataScanned = MessageDataScanningStatus.SCANNED.code;
   DecoderImpl decoder = TLSEncode.getDecoder();
   decoder.setBuffer(getData().rewind());

   resetMessageData();

   ReadableBuffer data = getData();

   try {
      while (data.hasRemaining()) {
         int constructorPos = data.position();
         TypeConstructor<?> constructor = decoder.readConstructor();
         if (Header.class.equals(constructor.getTypeClass())) {
            header = (Header) constructor.readValue();
            headerPosition = constructorPos;
            encodedHeaderSize = data.position();
            if (header.getTtl() != null) {
               expiration = System.currentTimeMillis() + header.getTtl().intValue();
            }
         } else if (DeliveryAnnotations.class.equals(constructor.getTypeClass())) {
            // Don't decode these as they are not used by the broker at all and are
            // discarded on send, mark for lazy decode if ever needed.
            constructor.skipValue();
            deliveryAnnotationsPosition = constructorPos;
            encodedDeliveryAnnotationsSize = data.position() - constructorPos;
         } else if (MessageAnnotations.class.equals(constructor.getTypeClass())) {
            messageAnnotationsPosition = constructorPos;
            messageAnnotations = (MessageAnnotations) constructor.readValue();
         } else if (Properties.class.equals(constructor.getTypeClass())) {
            propertiesPosition = constructorPos;
            properties = (Properties) constructor.readValue();

            if (properties.getAbsoluteExpiryTime() != null && properties.getAbsoluteExpiryTime().getTime() > 0) {
               expiration = properties.getAbsoluteExpiryTime().getTime();
            }
         } else if (ApplicationProperties.class.equals(constructor.getTypeClass())) {
            // Lazy decoding will start at the TypeConstructor of these ApplicationProperties
            // but we scan past it to grab the location of the possible body and footer section.
            applicationPropertiesPosition = constructorPos;
            constructor.skipValue();
            remainingBodyPosition = data.hasRemaining() ? data.position() : VALUE_NOT_PRESENT;
            break;
         } else {
            // This will be either the body or a Footer section which will be treated as an immutable
            // and be copied as is when re-encoding the message.
            remainingBodyPosition = constructorPos;
            break;
         }
      }
   } finally {
      decoder.setByteBuffer(null);
      data.rewind();
   }
}