/** * Write a byte to buffer. * * @param b to be written. */ public void write(final int b) { buffer.putByte(offset + position, (byte)b); ++position; }
@Test public void shouldGenerateCompositeEncoder() throws Exception { final int bufferOffset = 64; final int capacityFieldOffset = bufferOffset; final int numCylindersOffset = bufferOffset + 2; final int expectedEngineCapacity = 2000; final int manufacturerCodeOffset = bufferOffset + 3; final byte[] manufacturerCode = { 'A', 'B', 'C' }; final String className = "EngineEncoder"; final String fqClassName = ir.applicableNamespace() + "." + className; when(mockBuffer.getShort(capacityFieldOffset, BYTE_ORDER)) .thenReturn((short)expectedEngineCapacity); generateTypeStubs(); final Class<?> clazz = compile(fqClassName); assertNotNull(clazz); final Object flyweight = clazz.getConstructor().newInstance(); wrap(bufferOffset, flyweight, mockBuffer, BUFFER_CLASS); final short numCylinders = (short)4; clazz.getDeclaredMethod("numCylinders", short.class).invoke(flyweight, numCylinders); clazz.getDeclaredMethod("putManufacturerCode", byte[].class, int.class) .invoke(flyweight, manufacturerCode, 0); verify(mockBuffer).putByte(numCylindersOffset, (byte)numCylinders); verify(mockBuffer).putBytes(manufacturerCodeOffset, manufacturerCode, 0, manufacturerCode.length); }
/** * Write a byte to buffer. * * @param b to be written. * @throws IllegalStateException if insufficient capacity remains in the buffer. */ public void write(final int b) { if (position == length) { throw new IllegalStateException("position has reached the end of underlying buffer"); } buffer.putByte(offset + position, (byte)b); ++position; }
public TokenCodecEncoder primitiveType(final PrimitiveTypeCodec value) { buffer.putByte(offset + 21, (byte)value.value()); return this; }
public TokenCodecEncoder byteOrder(final ByteOrderCodec value) { buffer.putByte(offset + 22, (byte)value.value()); return this; }
public TokenCodecEncoder presence(final PresenceCodec value) { buffer.putByte(offset + 23, (byte)value.value()); return this; }
public TokenCodecEncoder signal(final SignalCodec value) { buffer.putByte(offset + 20, (byte)value.value()); return this; }
@Test public void shouldSetBitInByte() { final int bufferIndex = 8; short total = 0; for (int i = 0; i < 8; i++) { byte bits = buffer.getByte(bufferIndex); bits = (byte)(bits | (1 << i)); buffer.putByte(bufferIndex, bits); total += (1 << i); assertThat(buffer.getByte(bufferIndex), is((byte)total)); } }
private void testAlignedWriteMethods(final MutableDirectBuffer buffer, final int offset) { buffer.putLong(offset + SIZE_OF_LONG, Long.MAX_VALUE); buffer.putLong(offset + SIZE_OF_LONG, Long.MAX_VALUE, BIG_ENDIAN); buffer.putDouble(offset + SIZE_OF_DOUBLE, Double.MAX_VALUE); buffer.putDouble(offset + SIZE_OF_DOUBLE, Double.MAX_VALUE, BIG_ENDIAN); buffer.putInt(offset + SIZE_OF_INT, Integer.MAX_VALUE); buffer.putInt(offset + SIZE_OF_INT, Integer.MAX_VALUE, BIG_ENDIAN); buffer.putFloat(offset + SIZE_OF_FLOAT, Float.MAX_VALUE); buffer.putFloat(offset + SIZE_OF_FLOAT, Float.MAX_VALUE, BIG_ENDIAN); buffer.putShort(offset + SIZE_OF_SHORT, Short.MAX_VALUE); buffer.putShort(offset + SIZE_OF_SHORT, Short.MAX_VALUE, BIG_ENDIAN); buffer.putChar(offset + SIZE_OF_CHAR, Character.MAX_VALUE); buffer.putChar(offset + SIZE_OF_CHAR, Character.MAX_VALUE, BIG_ENDIAN); buffer.putByte(offset + SIZE_OF_BYTE, Byte.MAX_VALUE); buffer.putByte(offset + SIZE_OF_BYTE, Byte.MAX_VALUE); buffer.putStringUtf8(offset + SIZE_OF_INT, TEST_STRING); buffer.putStringUtf8(offset + SIZE_OF_INT, TEST_STRING, BIG_ENDIAN); buffer.putStringUtf8(offset + SIZE_OF_INT, TEST_STRING, Integer.MAX_VALUE); buffer.putStringUtf8(offset + SIZE_OF_INT, TEST_STRING, BIG_ENDIAN, Integer.MAX_VALUE); buffer.putStringAscii(offset + SIZE_OF_INT, TEST_STRING); buffer.putStringAscii(offset + SIZE_OF_INT, TEST_STRING, BIG_ENDIAN); // string size is not read for these method => no need for 4-bytes // alignment buffer.putStringWithoutLengthUtf8(offset + SIZE_OF_BYTE, TEST_STRING); buffer.putStringWithoutLengthAscii(offset + SIZE_OF_BYTE, TEST_STRING); }
public ExecuteCommandResponseEncoder intent(final short value) { buffer.putByte(offset + 12, (byte)value); return this; }
public MsgPackWriter writeNil() { buffer.putByte(offset, NIL); ++offset; return this; }
public GroupSizeEncodingEncoder numInGroup(final short value) { buffer.putByte(offset + 2, (byte)value); return this; }
public ExecuteCommandResponseEncoder rejectionType(final RejectionType value) { buffer.putByte(offset + 13, (byte)value.value()); return this; }
public ErrorResponseEncoder code(final ErrorResponseCode value) { buffer.putByte(offset + 0, (byte)value.value()); return this; }
public OpenMessageSubscriptionEncoder closeOnCorrelate(final BooleanType value) { buffer.putByte(offset + 18, (byte)value.value()); return this; }
public ErrorResponseEncoder errorCode(final ErrorCode value) { buffer.putByte(offset + 0, (byte)value.value()); return this; }
public AppendResponseEncoder succeeded(final BooleanType value) { buffer.putByte(offset + 4, (byte)value.value()); return this; }
@Override public void write(MutableDirectBuffer buffer, int offset) { buffer.putByte(PURPOSE_OFFSET, (byte) purpose.ordinal()); record.write(buffer, RECORD_OFFSET); }
public static int writePartition( PartitionInfo partition, RaftState state, MutableDirectBuffer writeBuffer, int offset) { writeBuffer.putInt(offset, partition.getPartitionId(), ByteOrder.LITTLE_ENDIAN); offset += SIZE_OF_INT; writeBuffer.putInt(offset, partition.getReplicationFactor(), ByteOrder.LITTLE_ENDIAN); offset += SIZE_OF_INT; writeBuffer.putByte(offset, (byte) (state == RaftState.LEADER ? 1 : 0)); offset += SIZE_OF_BYTE; return offset; }