Пример #1
0
 public byte[] getPayload() throws IOException {
   if (payloadSize < 0) throw new IllegalStateException("Payload already read");
   if (routingKeySize > 0) {
     input.skip(routingKeySize); // routing key was never read
     routingKeySize = -1;
   }
   byte[] buf = new byte[payloadSize];
   input.read(buf, 0, payloadSize);
   payloadSize = -1;
   return buf;
 }
Пример #2
0
 public String getRoutingKey() throws IOException {
   if (routingKeySize < 0) throw new IllegalStateException("Routing key already read");
   input.read(routingKeyBuf, 0, routingKeySize);
   String ans = new String(routingKeyBuf, 0, routingKeySize, UTF8);
   routingKeySize = -1;
   return ans;
 }
Пример #3
0
    /**
     * Advance to the next message or return false if there are no more messages. The cursor
     * initially starts "before" the next message.
     */
    public boolean next() throws IOException {
      if (routingKeySize > 0) {
        input.skip(routingKeySize); // routing key was never read
        routingKeySize = -1;
      }
      if (payloadSize > 0) {
        input.skip(payloadSize); // payload was never read
        payloadSize = -1;
      }

      int len = length();
      if (input.position() >= len) return false;

      id = firstMessageId + input.position() - FILE_HEADER_SIZE;

      byte type = input.readByte();
      if (type != TYPE_MESSAGE) {
        throw new IOException(
            "Unexpected message type 0x"
                + Integer.toHexString(type & 0xFF)
                + " at "
                + (input.position() - 1)
                + " in "
                + MessageFile.this);
      }

      timestamp = input.readLong();

      routingKeySize = input.readShort();
      if (routingKeySize < 0 || routingKeySize >= routingKeyBuf.length) {
        throw new IOException(
            "Invalid routing key size "
                + routingKeySize
                + " at "
                + (input.position() - 2)
                + " in "
                + MessageFile.this);
      }

      payloadSize = input.readInt();
      if (payloadSize < 0) {
        throw new IOException(
            "Negative payload size "
                + payloadSize
                + " at "
                + (input.position() - 4)
                + " in "
                + MessageFile.this);
      }

      nextPosition = input.position() + routingKeySize + payloadSize;
      if (nextPosition > len) {
        throw new IOException(
            "Payload size "
                + payloadSize
                + " at "
                + (input.position() - 4)
                + " extends beyond EOF "
                + len
                + " in "
                + MessageFile.this);
      }

      return true;
    }
Пример #4
0
 private void unget() {
   routingKeySize = payloadSize = -1;
   input.position(messageIdToPosition(id));
 }