### Question:
DoradoLengthDecoder implements LengthDecoder { @Override public int decodeLength(ByteBuffer in) { if (in.remaining() < LEAST_BYTE_SIZE) { return -1; } byte[] first4Bytes = new byte[4]; in.get(first4Bytes); if (first4Bytes[0] == MAGIC[0] && first4Bytes[1] == MAGIC[1]) { if (in.remaining() < LEAST_BYTE_SIZE) { return -1; } else { return in.getInt() + 8; } } else { int originalThriftSize = BytesUtil.bytes2int(first4Bytes, 0); if (originalThriftSize > 0) { return originalThriftSize + 4; } else { throw new ProtocolException("Receiving data not octo protocol or original thrift protocol!"); } } }  @Override int decodeLength(ByteBuffer in);  }### Answer:
@Test public void testOctoDecodeLength() { byte[] byteArray = {(byte) 0xab, (byte) 0xba, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x12}; ByteBuffer in = ByteBuffer.wrap(byteArray); DoradoLengthDecoder decoder = new DoradoLengthDecoder(); int length = decoder.decodeLength(in); Assert.assertTrue(length == 26); byteArray = new byte[]{(byte) 0xab, (byte) 0xba, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0}; in = ByteBuffer.wrap(byteArray); length = decoder.decodeLength(in); Assert.assertTrue(length == -1); }
@Test public void testEmptyDecodeLength() { byte[] byteArray = new byte[0]; ByteBuffer in = ByteBuffer.wrap(byteArray); DoradoLengthDecoder decoder = new DoradoLengthDecoder(); int length = decoder.decodeLength(in); Assert.assertTrue(length == -1); }
@Test public void testThriftDecodeLength() { DoradoLengthDecoder decoder = new DoradoLengthDecoder(); byte[] byteArray = {(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x12}; ByteBuffer in = ByteBuffer.wrap(byteArray); int length = decoder.decodeLength(in); Assert.assertTrue(length == 22); byteArray = new byte[]{(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x00}; in = ByteBuffer.wrap(byteArray); try { decoder.decodeLength(in); } catch (Exception e) { Assert.assertTrue(e instanceof ProtocolException); } }