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

The following examples show how to use org.apache.qpid.proton.codec.ReadableBuffer#remaining() . 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: ReceiverImpl.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@Override
public ReadableBuffer recv()
{
    if (_current == null) {
        throw new IllegalStateException("no current delivery");
    }

    ReadableBuffer consumed = _current.recv();
    if (consumed.remaining() > 0) {
        getSession().incrementIncomingBytes(-consumed.remaining());
        if (getSession().getTransportSession().getIncomingWindowSize().equals(UnsignedInteger.ZERO)) {
            modified();
        }
    }
    return consumed;
}
 
Example 2
Source File: DeliveryImpl.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
int sendNoCopy(ReadableBuffer buffer)
{
    int length = buffer.remaining();

    if (_dataView == null || !_dataView.hasRemaining())
    {
        _dataView = buffer;
    }
    else
    {
        consolidateSendBuffers(buffer);
    }

    addToTransportWorkList();
    return length;
}
 
Example 3
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 4
Source File: FrameWriter.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
private int writePerformative(Object frameBody, ReadableBuffer payload, Runnable onPayloadTooLarge) {
    frameBuffer.position(frameStart + FRAME_HEADER_SIZE);

    if (frameBody != null) {
        encoder.writeObject(frameBody);
    }

    int performativeSize = frameBuffer.position() - frameStart;

    if (onPayloadTooLarge != null && maxFrameSize > 0 && payload != null && (payload.remaining() + performativeSize) > maxFrameSize) {
        // Next iteration will re-encode the frame body again with updates from the <payload-to-large>
        // handler and then we can move onto the body portion.
        onPayloadTooLarge.run();
        performativeSize = writePerformative(frameBody, payload, null);
    }

    return performativeSize;
}
 
Example 5
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 6
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 7
Source File: NettyReadable.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public boolean equals(Object other) {
   if (this == other) {
      return true;
   }

   if (!(other instanceof ReadableBuffer)) {
      return false;
   }

   ReadableBuffer readable = (ReadableBuffer) other;
   if (this.remaining() != readable.remaining()) {
      return false;
   }

   return buffer.nioBuffer().equals(readable.byteBuffer());
}
 
Example 8
Source File: DeliveryImpl.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
int send(final ReadableBuffer buffer)
{
    int length = buffer.remaining();
    getOrCreateDataBuffer().append(copyContents(buffer));
    addToTransportWorkList();
    return length;
}
 
Example 9
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 10
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 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: 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 13
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 14
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 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: 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);
}