/** * 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; }
private void unget() { routingKeySize = payloadSize = -1; input.position(messageIdToPosition(id)); }