@Override public Byte decodeFromWire(int pos, Buffer buffer) { return buffer.getByte(pos); }
@Override public ReplyException decodeFromWire(int pos, Buffer buffer) { int i = (int) buffer.getByte(pos); ReplyFailure rf = ReplyFailure.fromInt(i); pos++; int failureCode = buffer.getInt(pos); pos += 4; boolean isNull = buffer.getByte(pos) == (byte)0; String message; if (!isNull) { pos++; int strLength = buffer.getInt(pos); pos += 4; byte[] bytes = buffer.getBytes(pos, pos + strLength); message = new String(bytes, CharsetUtil.UTF_8); } else { message = null; } return new ReplyException(rf, failureCode, message); }
public void readFromWire(Buffer buffer, CodecManager codecManager) { int pos = 0; byte protocolVersion = buffer.getByte(pos); if (protocolVersion > WIRE_PROTOCOL_VERSION) { throw new IllegalStateException("Invalid wire protocol version " + protocolVersion + byte systemCodecCode = buffer.getByte(pos); pos++; if (systemCodecCode == -1) { messageCodec = codecManager.systemCodecs()[systemCodecCode]; byte bsend = buffer.getByte(pos); send = bsend == 0; pos++;
@Override public Boolean decodeFromWire(int pos, Buffer buffer) { return buffer.getByte(pos) == 0; }
@Override public MyReplyException decodeFromWire(int pos, Buffer buffer) { int failureCode = buffer.getInt(pos); pos += 4; boolean isNull = buffer.getByte(pos) == (byte)0; String message; if (!isNull) { pos++; int strLength = buffer.getInt(pos); pos += 4; byte[] bytes = buffer.getBytes(pos, pos + strLength); message = new String(bytes, CharsetUtil.UTF_8); } else { message = null; } return new MyReplyException(failureCode, message); }
assertIndexOutOfBoundsException(() -> b.getByte(bytesLen)); assertIndexOutOfBoundsException(() -> b.getByte(bytesLen + 1)); assertIndexOutOfBoundsException(() -> b.getByte(bytesLen + 100)); assertIndexOutOfBoundsException(() -> b.getByte(-1)); assertIndexOutOfBoundsException(() -> b.getByte(-100)); assertIndexOutOfBoundsException(() -> b.getInt(bytesLen)); assertIndexOutOfBoundsException(() -> b.getInt(bytesLen + 1));
private int parseDelimited() { int len = buff.length(); for (; pos < len; pos++) { if (buff.getByte(pos) == delim[delimPos]) { delimPos++; if (delimPos == delim.length) { pos++; delimPos = 0; return pos - delim.length; } } else { if (delimPos > 0) { pos -= delimPos; delimPos = 0; } } } return -1; }
private String toHex(Buffer buffer) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < buffer.length(); i++) { sb.append(String.format("%02X ", buffer.getByte(i))); } return sb.toString(); }
private String toHex(Buffer buffer) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < buffer.length(); i++) { sb.append(String.format("%02X ", buffer.getByte(i))); } return sb.toString(); }
private void testSetByte(Buffer buff) throws Exception { for (int i = 0; i < numSets; i++) { buff.setByte(i, (byte) i); } for (int i = 0; i < numSets; i++) { assertEquals(i, buff.getByte(i)); } }
@Test public void testGetByte() throws Exception { int bytesLen = 100; byte[] bytes = TestUtils.randomByteArray(bytesLen); Buffer b = Buffer.buffer(bytes); for (int i = 0; i < bytesLen; i++) { assertEquals(bytes[i], b.getByte(i)); } }
private void delimited(Buffer delim) { int lines = 50; Buffer[] expected = new Buffer[lines]; //We create lines of length zero to <lines> and shuffle them List<Buffer> lineList = generateLines(lines, true, delim.getByte(0)); expected = lineList.toArray(expected); int totLength = lines * (lines - 1) / 2; // The sum of 0...(lines - 1) Buffer inp = Buffer.buffer(totLength + lines * delim.length()); for (int i = 0; i < lines; i++) { inp.appendBuffer(expected[i]); inp.appendBuffer(delim); } //We then try every combination of chunk size up to twice the input string length for (int i = 1; i < inp.length() * 2; i++) { doTestDelimited(inp, delim, new Integer[]{i}, expected); } //Then we try a sequence of random chunk sizes List<Integer> chunkSizes = generateChunkSizes(lines); //Repeat a few times for (int i = 0; i < 10; i++) { Collections.shuffle(chunkSizes); doTestDelimited(inp, delim, chunkSizes.toArray(new Integer[]{}), expected); } }
recordParser.fixedSizeMode(4); final char type = (char) buffer.getByte(0);
@Override public Byte decodeFromWire(int pos, Buffer buffer) { return buffer.getByte(pos); }
@Test public void testSlice2() throws Exception { Buffer buff = TestUtils.randomBuffer(100); Buffer sliced = buff.slice(10, 20); for (int i = 0; i < 10; i++) { assertEquals(buff.getByte(10 + i), sliced.getByte(i)); } long rand = TestUtils.randomLong(); sliced.setLong(0, rand); assertEquals(rand, buff.getLong(10)); buff.appendString(TestUtils.randomUnicodeString(100)); assertEquals(10, sliced.length()); }
private String toHex(Buffer buffer) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < buffer.length(); i++) { sb.append(String.format("%02X ", buffer.getByte(i))); } return sb.toString(); }
@Test public void testGetByte() throws Exception { int bytesLen = 100; byte[] bytes = TestUtils.randomByteArray(bytesLen); Buffer b = Buffer.buffer(bytes); for (int i = 0; i < bytesLen; i++) { assertEquals(bytes[i], b.getByte(i)); } }