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

The following examples show how to use org.apache.qpid.proton.codec.ReadableBuffer#get() . 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: FastPathDeliveryAnnotationsType.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
private static TypeConstructor<?> findNextDecoder(DecoderImpl decoder, ReadableBuffer buffer, TypeConstructor<?> previousConstructor) {
    if (previousConstructor == null) {
        return decoder.readConstructor();
    } else {
        byte encodingCode = buffer.get(buffer.position());
        if (encodingCode == EncodingCodes.DESCRIBED_TYPE_INDICATOR || !(previousConstructor instanceof PrimitiveTypeEncoding<?>)) {
            previousConstructor = decoder.readConstructor();
        } else {
            PrimitiveTypeEncoding<?> primitiveConstructor = (PrimitiveTypeEncoding<?>) previousConstructor;
            if (encodingCode != primitiveConstructor.getEncodingCode()) {
                previousConstructor = decoder.readConstructor();
            } else {
                // consume the encoding code byte for real
                encodingCode = buffer.get();
            }
        }
    }

    if (previousConstructor == null) {
        throw new DecodeException("Unknown constructor found in Map encoding: ");
    }

    return previousConstructor;
}
 
Example 3
Source File: FastPathMessageAnnotationsType.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
private static TypeConstructor<?> findNextDecoder(DecoderImpl decoder, ReadableBuffer buffer, TypeConstructor<?> previousConstructor) {
    if (previousConstructor == null) {
        return decoder.readConstructor();
    } else {
        byte encodingCode = buffer.get(buffer.position());
        if (encodingCode == EncodingCodes.DESCRIBED_TYPE_INDICATOR || !(previousConstructor instanceof PrimitiveTypeEncoding<?>)) {
            previousConstructor = decoder.readConstructor();
        } else {
            PrimitiveTypeEncoding<?> primitiveConstructor = (PrimitiveTypeEncoding<?>) previousConstructor;
            if (encodingCode != primitiveConstructor.getEncodingCode()) {
                previousConstructor = decoder.readConstructor();
            } else {
                // consume the encoding code byte for real
                encodingCode = buffer.get();
            }
        }
    }

    if (previousConstructor == null) {
        throw new DecodeException("Unknown constructor found in Map encoding: ");
    }

    return previousConstructor;
}
 
Example 4
Source File: AMQPLargeMessage.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public void addBytes(ReadableBuffer data) throws Exception {

      // We need to parse the header on the first add,
      // as it will contain information if the message is durable or not
      if (header == null && largeBody.getStoredBodySize() <= 0) {
         parseHeader(data);
      }

      if (data.hasArray() && data.remaining() == data.array().length) {
         //System.out.println("Received " + data.array().length + "::" + ByteUtil.formatGroup(ByteUtil.bytesToHex(data.array()), 8, 16));
         largeBody.addBytes(data.array());
      } else {
         byte[] bytes = new byte[data.remaining()];
         data.get(bytes);
         //System.out.println("Finishing " + bytes.length + ByteUtil.formatGroup(ByteUtil.bytesToHex(bytes), 8, 16));
         largeBody.addBytes(bytes);
      }
   }
 
Example 5
Source File: FastPathApplicationPropertiesType.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
private static TypeConstructor<?> findNextDecoder(DecoderImpl decoder, ReadableBuffer buffer, TypeConstructor<?> previousConstructor) {
    if (previousConstructor == null) {
        return decoder.readConstructor();
    } else {
        byte encodingCode = buffer.get(buffer.position());
        if (encodingCode == EncodingCodes.DESCRIBED_TYPE_INDICATOR || !(previousConstructor instanceof PrimitiveTypeEncoding<?>)) {
            previousConstructor = decoder.readConstructor();
        } else {
            PrimitiveTypeEncoding<?> primitiveConstructor = (PrimitiveTypeEncoding<?>) previousConstructor;
            if (encodingCode != primitiveConstructor.getEncodingCode()) {
                previousConstructor = decoder.readConstructor();
            } else {
                // consume the encoding code byte for real
                encodingCode = buffer.get();
            }
        }
    }

    if (previousConstructor == null) {
        throw new DecodeException("Unknown constructor found in Map encoding: ");
    }

    return previousConstructor;
}
 
Example 6
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 7
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 8
Source File: ProtonReceiverImpl.java    From vertx-proton with Apache License 2.0 5 votes vote down vote up
private ReadableBuffer completePartial(final ReadableBuffer finalContent) {
  int pending = finalContent.remaining();
  if(pending > 0) {
    byte[] chunk = new byte[pending];
    finalContent.get(chunk);

    splitContent.append(chunk);
  }

  ReadableBuffer data = splitContent;
  splitContent = null;

  return data;
}
 
Example 9
Source File: FastPathDataType.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public Data readValue() {
    ReadableBuffer buffer = getDecoder().getBuffer();
    byte encodingCode = buffer.get();

    int size = 0;

    switch (encodingCode) {
        case EncodingCodes.VBIN8:
            size = buffer.get() & 0xFF;
            break;
        case EncodingCodes.VBIN32:
            size = buffer.getInt();
            break;
        case EncodingCodes.NULL:
            return new Data(null);
        default:
            throw new ProtonException("Expected Binary type but found encoding: " + encodingCode);
    }

    if (size > buffer.remaining()) {
        throw new IllegalArgumentException("Binary data size " + size + " is specified to be greater than the " +
                                           "amount of data available ("+ buffer.remaining()+")");
    }

    byte[] data = new byte[size];
    buffer.get(data, 0, size);

    return new Data(new Binary(data));
}
 
Example 10
Source File: ProtonReceiverImpl.java    From vertx-proton with Apache License 2.0 5 votes vote down vote up
private void handlePartial(final Receiver receiver, final Delivery delivery) {
  if (sessionIncomingCapacity <= 0 || maxFrameSize <= 0 || session.getIncomingBytes() < windowFullThreshhold) {
    // No window, or there is still capacity, so do nothing.
  } else {
    // The session window could be effectively full, we need to
    // read part of the delivery content to ensure there is
    // room made for receiving more of the delivery.
    if(delivery.available() > 0) {
      ReadableBuffer buff = receiver.recv();

      if(splitContent == null && buff instanceof CompositeReadableBuffer) {
        // Its a composite and there is no prior partial content, use it.
        splitContent = (CompositeReadableBuffer) buff;
      } else {
        int remaining = buff.remaining();
        if(remaining > 0) {
          if (splitContent == null) {
            splitContent = new CompositeReadableBuffer();
          }

          byte[] chunk = new byte[remaining];
          buff.get(chunk);

          splitContent.append(chunk);
        }
      }
    }
  }
}
 
Example 11
Source File: AMQPStandardMessage.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public org.apache.activemq.artemis.api.core.Message copy() {
   ensureDataIsValid();

   ReadableBuffer view = data.duplicate().rewind();
   byte[] newData = new byte[view.remaining()];

   // Copy the full message contents with delivery annotations as they will
   // be trimmed on send and may become useful on the broker at a later time.
   view.get(newData);

   AMQPStandardMessage newEncode = new AMQPStandardMessage(this.messageFormat, newData, extraProperties, coreMessageObjectPools);
   newEncode.setMessageID(this.getMessageID());
   return newEncode;
}
 
Example 12
Source File: AmqpWritableBuffer.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Override
public void put(ReadableBuffer buffer) {
    buffer.get(this);
}
 
Example 13
Source File: ProtonWritableBufferImpl.java    From vertx-proton with Apache License 2.0 4 votes vote down vote up
@Override
public void put(ReadableBuffer buffer) {
  buffer.get(this);
}
 
Example 14
Source File: FastPathApplicationPropertiesType.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Override
public ApplicationProperties readValue() {
    DecoderImpl decoder = getDecoder();
    ReadableBuffer buffer = decoder.getBuffer();

    final int size;
    final int count;

    byte encodingCode = buffer.get();

    switch (encodingCode) {
        case EncodingCodes.MAP8:
            size = buffer.get() & 0xFF;
            count = buffer.get() & 0xFF;
            break;
        case EncodingCodes.MAP32:
            size = buffer.getInt();
            count = buffer.getInt();
            break;
        case EncodingCodes.NULL:
            return new ApplicationProperties(null);
        default:
            throw new ProtonException("Expected Map type but found encoding: " + encodingCode);
    }

    if (count > buffer.remaining()) {
        throw new IllegalArgumentException("Map element count " + count + " is specified to be greater than the " +
                                           "amount of data available ("+ buffer.remaining() + ")");
    }

    TypeConstructor<?> valueConstructor = null;

    Map<String, Object> map = new LinkedHashMap<>(count);
    for (int i = 0; i < count / 2; i++) {
        String key = decoder.readString(null);
        if (key == null) {
            throw new DecodeException("String key in ApplicationProperties cannot be null");
        }

        boolean arrayType = false;
        byte code = buffer.get(buffer.position());
        switch (code)
        {
            case EncodingCodes.ARRAY8:
            case EncodingCodes.ARRAY32:
                arrayType = true;
        }

        valueConstructor = findNextDecoder(decoder, buffer, valueConstructor);

        final Object value;

        if (arrayType) {
            value = ((ArrayType.ArrayEncoding) valueConstructor).readValueArray();
        } else {
            value = valueConstructor.readValue();
        }

        map.put(key, value);
    }

    return new ApplicationProperties(map);
}
 
Example 15
Source File: FastPathMessageAnnotationsType.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Override
public MessageAnnotations readValue() {
    DecoderImpl decoder = getDecoder();
    ReadableBuffer buffer = decoder.getBuffer();

    final int size;
    final int count;

    byte encodingCode = buffer.get();

    switch (encodingCode) {
        case EncodingCodes.MAP8:
            size = buffer.get() & 0xFF;
            count = buffer.get() & 0xFF;
            break;
        case EncodingCodes.MAP32:
            size = buffer.getInt();
            count = buffer.getInt();
            break;
        case EncodingCodes.NULL:
            return new MessageAnnotations(null);
        default:
            throw new ProtonException("Expected Map type but found encoding: " + encodingCode);
    }

    if (count > buffer.remaining()) {
        throw new IllegalArgumentException("Map element count "+count+" is specified to be greater than the amount of data available ("+
                                           decoder.getByteBufferRemaining()+")");
    }

    TypeConstructor<?> valueConstructor = null;

    Map<Symbol, Object> map = new LinkedHashMap<>(count);
    for(int i = 0; i < count / 2; i++) {
        Symbol key = decoder.readSymbol(null);
        if (key == null) {
            throw new DecodeException("String key in DeliveryAnnotations cannot be null");
        }

        boolean arrayType = false;
        byte code = buffer.get(buffer.position());
        switch (code)
        {
            case EncodingCodes.ARRAY8:
            case EncodingCodes.ARRAY32:
                arrayType = true;
        }

        valueConstructor = findNextDecoder(decoder, buffer, valueConstructor);

        final Object value;

        if (arrayType) {
            value = ((ArrayType.ArrayEncoding) valueConstructor).readValueArray();
        } else {
            value = valueConstructor.readValue();
        }

        map.put(key, value);
    }

    return new MessageAnnotations(map);
}
 
Example 16
Source File: FastPathHeaderType.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Override
public Header readValue() {
    DecoderImpl decoder = getDecoder();
    ReadableBuffer buffer = decoder.getBuffer();
    byte typeCode = decoder.getBuffer().get();

    @SuppressWarnings("unused")
    int size = 0;
    int count = 0;

    switch (typeCode) {
        case EncodingCodes.LIST0:
            break;
        case EncodingCodes.LIST8:
            size = buffer.get() & 0xff;
            count = buffer.get() & 0xff;
            break;
        case EncodingCodes.LIST32:
            size = buffer.getInt();
            count = buffer.getInt();
            break;
        default:
            throw new DecodeException("Incorrect type found in Header encoding: " + typeCode);
    }

    Header header = new Header();

    for (int index = 0; index < count; ++index) {
        switch (index) {
            case 0:
                header.setDurable(decoder.readBoolean(null));
                break;
            case 1:
                header.setPriority(decoder.readUnsignedByte(null));
                break;
            case 2:
                header.setTtl(decoder.readUnsignedInteger(null));
                break;
            case 3:
                header.setFirstAcquirer(decoder.readBoolean(null));
                break;
            case 4:
                header.setDeliveryCount(decoder.readUnsignedInteger(null));
                break;
            default:
                throw new IllegalStateException("To many entries in Header encoding");
        }
    }

    return header;
}
 
Example 17
Source File: FastPathDeliveryAnnotationsType.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Override
public DeliveryAnnotations readValue() {
    DecoderImpl decoder = getDecoder();
    ReadableBuffer buffer = decoder.getBuffer();

    final int size;
    final int count;

    byte encodingCode = buffer.get();

    switch (encodingCode) {
        case EncodingCodes.MAP8:
            size = buffer.get() & 0xFF;
            count = buffer.get() & 0xFF;
            break;
        case EncodingCodes.MAP32:
            size = buffer.getInt();
            count = buffer.getInt();
            break;
        case EncodingCodes.NULL:
            return new DeliveryAnnotations(null);
        default:
            throw new ProtonException("Expected Map type but found encoding: " + encodingCode);
    }

    if (count > buffer.remaining()) {
        throw new IllegalArgumentException("Map element count " + count + " is specified to be greater than the " +
                                           "amount of data available (" + buffer.remaining() + ")");
    }

    TypeConstructor<?> valueConstructor = null;

    Map<Symbol, Object> map = new LinkedHashMap<>(count);
    for(int i = 0; i < count / 2; i++) {
        Symbol key = decoder.readSymbol(null);
        if (key == null) {
            throw new DecodeException("String key in DeliveryAnnotations cannot be null");
        }

        boolean arrayType = false;
        byte code = buffer.get(buffer.position());
        switch (code)
        {
            case EncodingCodes.ARRAY8:
            case EncodingCodes.ARRAY32:
                arrayType = true;
        }

        valueConstructor = findNextDecoder(decoder, buffer, valueConstructor);

        final Object value;

        if (arrayType) {
            value = ((ArrayType.ArrayEncoding) valueConstructor).readValueArray();
        } else {
            value = valueConstructor.readValue();
        }

        map.put(key, value);
    }

    return new DeliveryAnnotations(map);
}
 
Example 18
Source File: FastPathPropertiesType.java    From qpid-proton-j with Apache License 2.0 4 votes vote down vote up
@Override
public Properties readValue() {
    DecoderImpl decoder = getDecoder();
    ReadableBuffer buffer = decoder.getBuffer();
    byte typeCode = decoder.getBuffer().get();

    @SuppressWarnings("unused")
    int size = 0;
    int count = 0;

    switch (typeCode) {
        case EncodingCodes.LIST0:
            break;
        case EncodingCodes.LIST8:
            size = buffer.get() & 0xff;
            count = buffer.get() & 0xff;
            break;
        case EncodingCodes.LIST32:
            size = buffer.getInt();
            count = buffer.getInt();
            break;
        default:
            throw new DecodeException("Incorrect type found in Properties encoding: " + typeCode);
    }

    Properties properties = new Properties();

    for (int index = 0; index < count; ++index) {
        switch (index) {
            case 0:
                properties.setMessageId(decoder.readObject());
                break;
            case 1:
                properties.setUserId(decoder.readBinary(null));
                break;
            case 2:
                properties.setTo(decoder.readString(null));
                break;
            case 3:
                properties.setSubject(decoder.readString(null));
                break;
            case 4:
                properties.setReplyTo(decoder.readString(null));
                break;
            case 5:
                properties.setCorrelationId(decoder.readObject());
                break;
            case 6:
                properties.setContentType(decoder.readSymbol(null));
                break;
            case 7:
                properties.setContentEncoding(decoder.readSymbol(null));
                break;
            case 8:
                properties.setAbsoluteExpiryTime(decoder.readTimestamp(null));
                break;
            case 9:
                properties.setCreationTime(decoder.readTimestamp(null));
                break;
            case 10:
                properties.setGroupId(decoder.readString(null));
                break;
            case 11:
                properties.setGroupSequence(decoder.readUnsignedInteger(null));
                break;
            case 12:
                properties.setReplyToGroupId(decoder.readString(null));
                break;
            default:
                throw new IllegalStateException("To many entries in Properties encoding");
        }
    }

    return properties;
}