/** * The total length of the frame including the header. * * @return the total length of the frame including the header. */ public int frameLength() { return buffer.getInt(offset, LITTLE_ENDIAN); }
public void wrap(final DirectBuffer buffer) { addressOffset = buffer.addressOffset(); capacity = buffer.capacity(); final byte[] byteArray = buffer.byteArray(); if (byteArray != this.byteArray) { this.byteArray = byteArray; } final ByteBuffer byteBuffer = buffer.byteBuffer(); if (byteBuffer != this.byteBuffer) { this.byteBuffer = byteBuffer; } }
public void wrapMaxValue(final DirectBuffer wrapBuffer) { final int headerLength = 2; final int limit = parentMessage.limit(); final int dataLength = (int)(buffer.getShort(limit, java.nio.ByteOrder.LITTLE_ENDIAN) & 0xFFFF); parentMessage.limit(limit + headerLength + dataLength); wrapBuffer.wrap(buffer, limit + headerLength, dataLength); }
private void testAlignedReadMethods(final DirectBuffer buffer, final int offset) { buffer.getLong(offset + SIZE_OF_LONG); buffer.getLong(offset + SIZE_OF_LONG, BIG_ENDIAN); buffer.getDouble(offset + SIZE_OF_DOUBLE); buffer.getDouble(offset + SIZE_OF_DOUBLE, BIG_ENDIAN); buffer.getInt(offset + SIZE_OF_INT); buffer.getInt(offset + SIZE_OF_INT, BIG_ENDIAN); buffer.getFloat(offset + SIZE_OF_FLOAT); buffer.getFloat(offset + SIZE_OF_FLOAT, BIG_ENDIAN); buffer.getShort(offset + SIZE_OF_SHORT); buffer.getShort(offset + SIZE_OF_SHORT, BIG_ENDIAN); buffer.getChar(offset + SIZE_OF_CHAR); buffer.getChar(offset + SIZE_OF_CHAR, BIG_ENDIAN); buffer.getByte(offset + SIZE_OF_BYTE); buffer.getByte(offset + SIZE_OF_BYTE); buffer.getStringUtf8(offset + SIZE_OF_INT); buffer.getStringUtf8(offset + SIZE_OF_INT, BIG_ENDIAN); buffer.getStringAscii(offset + SIZE_OF_INT); buffer.getStringAscii(offset + SIZE_OF_INT, BIG_ENDIAN); // string size is not read for these method => no need for 4-bytes // alignment buffer.getStringUtf8(offset + SIZE_OF_BYTE, 7); buffer.getStringWithoutLengthUtf8(offset + SIZE_OF_BYTE, 7); buffer.getStringAscii(offset + SIZE_OF_BYTE, 7); buffer.getStringWithoutLengthAscii(offset + SIZE_OF_BYTE, 7); }
public int getPackageName(final byte[] dst, final int dstOffset, final int length) { final int headerLength = 2; final int limit = parentMessage.limit(); final int dataLength = (int)(buffer.getShort(limit, java.nio.ByteOrder.LITTLE_ENDIAN) & 0xFFFF); final int bytesCopied = Math.min(length, dataLength); parentMessage.limit(limit + headerLength + dataLength); buffer.getBytes(limit + headerLength, dst, dstOffset, bytesCopied); return bytesCopied; }
/** * Get the position at which the snapshot was taken. {@link Aeron#NULL_VALUE} if no snapshot for recovery. * * @param counters to search within. * @param counterId for the active recovery counter. * @return the log position if found otherwise {@link Aeron#NULL_VALUE}. */ public static long getLogPosition(final CountersReader counters, final int counterId) { final DirectBuffer buffer = counters.metaDataBuffer(); if (counters.getCounterState(counterId) == RECORD_ALLOCATED) { final int recordOffset = CountersReader.metaDataOffset(counterId); if (buffer.getInt(recordOffset + TYPE_ID_OFFSET) == RECOVERY_STATE_TYPE_ID) { return buffer.getLong(recordOffset + KEY_OFFSET + LOG_POSITION_OFFSET); } } return NULL_VALUE; }
public static int readIntoBuffer( final DirectBuffer buffer, int offset, final DirectBuffer valueBuffer) { final int length = buffer.getInt(offset, ByteOrder.LITTLE_ENDIAN); offset += Integer.BYTES; final byte[] bytes = new byte[length]; valueBuffer.wrap(bytes); buffer.getBytes(offset, bytes, 0, length); offset += length; return offset; }
public static char[] bytesToHex(final DirectBuffer buffer, final int offset, final int length) { final char[] chars = new char[length * 2]; for (int i = 0; i < length; i++) { final int b = buffer.getByte(offset + i) & 0xFF; chars[i * 2] = HEX_ARRAY[b >>> 4]; chars[i * 2 + 1] = HEX_ARRAY[b & 0x0F]; } return chars; } }
public int compareTo(final DirectBuffer that) { final int thisCapacity = this.capacity; final int thatCapacity = that.capacity(); final byte[] thisByteArray = this.byteArray; final byte[] thatByteArray = that.byteArray(); final long thisOffset = this.addressOffset; final long thatOffset = that.addressOffset(); for (int i = 0, length = Math.min(thisCapacity, thatCapacity); i < length; i++) { final int cmp = Byte.compare( UNSAFE.getByte(thisByteArray, thisOffset + i), UNSAFE.getByte(thatByteArray, thatOffset + i)); if (0 != cmp) { return cmp; } } if (thisCapacity != thatCapacity) { return thisCapacity - thatCapacity; } return 0; }
private boolean filter(final int typeId, final DirectBuffer keyBuffer) { if (!match(typeFilter, () -> Integer.toString(typeId))) { return false; } if (SYSTEM_COUNTER_TYPE_ID == typeId && !match(identityFilter, () -> Integer.toString(keyBuffer.getInt(0)))) { return false; } else if ((typeId >= PUBLISHER_LIMIT_TYPE_ID && typeId <= RECEIVER_POS_TYPE_ID) || typeId == SENDER_LIMIT_TYPE_ID || typeId == PER_IMAGE_TYPE_ID || typeId == PUBLISHER_POS_TYPE_ID) { return match(identityFilter, () -> Long.toString(keyBuffer.getLong(REGISTRATION_ID_OFFSET))) && match(sessionFilter, () -> Integer.toString(keyBuffer.getInt(SESSION_ID_OFFSET))) && match(streamFilter, () -> Integer.toString(keyBuffer.getInt(STREAM_ID_OFFSET))) && match(channelFilter, () -> keyBuffer.getStringAscii(CHANNEL_OFFSET)); } else if (typeId >= SEND_CHANNEL_STATUS_TYPE_ID && typeId <= RECEIVE_CHANNEL_STATUS_TYPE_ID) { return match(channelFilter, () -> keyBuffer.getStringAscii(ChannelEndpointStatus.CHANNEL_OFFSET)); } return true; }
public boolean addToBatch(OutgoingMessage message) { final DirectBuffer buffer = message.getBuffer(); final int requiredLength = buffer.capacity(); if (writeOffset + requiredLength <= batchBuffer.capacity()) { buffer.getBytes(0, batchBuffer, writeOffset, requiredLength); writeOffset += requiredLength; return true; } else { return false; } }
@SuppressWarnings("unused") private void validateFragment(final DirectBuffer buffer, final int offset, final int length, final Header header) { assertThat(length, is(messageLengths[messageCount] - HEADER_LENGTH)); assertThat(buffer.getInt(offset), is(messageCount)); assertThat(buffer.getByte(offset + 4), is((byte)'z')); remaining -= BitUtil.align(messageLengths[messageCount], FrameDescriptor.FRAME_ALIGNMENT); messageCount++; }
/** * Ensure the vector is valid for the buffer. * * @throws NullPointerException if the buffer is null. * @throws IllegalArgumentException if the offset is out of range for the buffer. * @throws IllegalArgumentException if the length is out of range for the buffer. * @return this for a fluent API. */ public DirectBufferVector validate() { final int capacity = buffer.capacity(); if (offset < 0 || offset >= capacity) { throw new IllegalArgumentException("offset=" + offset + " capacity=" + capacity); } if (length < 0 || length > (capacity - offset)) { throw new IllegalArgumentException("offset=" + offset + " capacity=" + capacity + " length=" + length); } return this; }
/** * Get the value stored in the reserve space at the end of a data frame header. * <p> * Note: The value is in {@link ByteOrder#LITTLE_ENDIAN} format. * * @return the value stored in the reserve space at the end of a data frame header. * @see DataHeaderFlyweight */ public long reservedValue() { return buffer.getLong(offset + RESERVED_VALUE_OFFSET, LITTLE_ENDIAN); } }
final ByteBuffer bb = buffer.byteBuffer(); if (null != bb) final int capacity = buffer.capacity(); byteBuffer = ByteBuffer.wrap(buffer.byteArray());
@Test public void shouldStoreRawData() { final int typeIdOne = 333; final long keyOne = 777L; final MutableDirectBuffer keyOneBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(8)); keyOneBuffer.putLong(0, keyOne); final DirectBuffer labelOneBuffer = new UnsafeBuffer("Test Label One".getBytes(US_ASCII)); final int typeIdTwo = 222; final long keyTwo = 444; final MutableDirectBuffer keyTwoBuffer = new UnsafeBuffer(ByteBuffer.allocateDirect(8)); keyTwoBuffer.putLong(0, keyTwo); final DirectBuffer labelTwoBuffer = new UnsafeBuffer("Test Label Two".getBytes(US_ASCII)); final int counterIdOne = manager.allocate( typeIdOne, keyOneBuffer, 0, keyOneBuffer.capacity(), labelOneBuffer, 0, labelOneBuffer.capacity()); final int counterIdTwo = manager.allocate( typeIdTwo, keyTwoBuffer, 0, keyTwoBuffer.capacity(), labelTwoBuffer, 0, labelTwoBuffer.capacity()); manager.forEach(metaData); final ArgumentCaptor<DirectBuffer> argCaptorOne = ArgumentCaptor.forClass(DirectBuffer.class); final ArgumentCaptor<DirectBuffer> argCaptorTwo = ArgumentCaptor.forClass(DirectBuffer.class); final InOrder inOrder = Mockito.inOrder(metaData); inOrder.verify(metaData).accept(eq(counterIdOne), eq(typeIdOne), argCaptorOne.capture(), eq("Test Label One")); inOrder.verify(metaData).accept(eq(counterIdTwo), eq(typeIdTwo), argCaptorTwo.capture(), eq("Test Label Two")); inOrder.verifyNoMoreInteractions(); final DirectBuffer keyOneBufferCapture = argCaptorOne.getValue(); assertThat(keyOneBufferCapture.getLong(0), is(keyOne)); final DirectBuffer keyTwoBufferCapture = argCaptorTwo.getValue(); assertThat(keyTwoBufferCapture.getLong(0), is(keyTwo)); }
private <KeyType extends DbKey, ValueType extends DbValue> boolean visit( KeyType keyInstance, ValueType valueInstance, KeyValuePairVisitor<KeyType, ValueType> iteratorConsumer, RocksDbIterator iterator) { keyViewBuffer.wrap(iterator.key()); valueViewBuffer.wrap(iterator.value()); keyInstance.wrap(keyViewBuffer, 0, keyViewBuffer.capacity()); valueInstance.wrap(valueViewBuffer, 0, valueViewBuffer.capacity()); return iteratorConsumer.visit(keyInstance, valueInstance); }
final boolean isPaddingFrame = termBuffer.getShort(typeOffset(termOffset)) == PADDING_FRAME_TYPE; final int dataLength = isPaddingFrame ? DataHeaderFlyweight.HEADER_LENGTH : length; final ByteBuffer byteBuffer = termBuffer.byteBuffer(); byteBuffer.limit(termOffset + dataLength).position(termOffset);