VariableLengthByteArrayCodec implements Codec<byte[]> { public byte[] decode(BitBuffer buffer, Resolver resolver, Builder builder) throws DecodingException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); boolean cont = true; while (cont) { byte b = buffer.readAsByte(8); bout.write(b); cont = (b & (1 << 7)) > 0; } return bout.toByteArray(); }  byte[] decode(BitBuffer buffer, Resolver resolver, Builder builder); void encode(byte[] value, BitChannel channel, Resolver resolver); Expression<Integer, Resolver> getSize(); CodecDescriptor getCodecDescriptor(); Class<?>[] getTypes(); Class<?> getType();  }
@Test public void shouldDecodeOneByte() throws DecodingException { VariableLengthByteArrayCodec codec = new VariableLengthByteArrayCodec(); when(buffer.readAsByte(8)).thenReturn((byte) 0x0f); byte[] decoded = codec.decode(buffer, resolver, builder); assertThat(decoded, is(not(nullValue()))); assertThat(decoded.length, is(1)); verify(buffer).readAsByte(8); verifyNoMoreInteractions(buffer, resolver, builder); }
@Test public void shouldDecodeMultipleBytes() throws DecodingException { VariableLengthByteArrayCodec codec = new VariableLengthByteArrayCodec(); when(buffer.readAsByte(8)).thenReturn((byte) 0xff).thenReturn((byte) 0x0f); byte[] decoded = codec.decode(buffer, resolver, builder); assertThat(decoded, is(not(nullValue()))); assertThat(decoded.length, is(2)); verify(buffer, times(2)).readAsByte(8); verifyNoMoreInteractions(buffer, resolver, builder); }
@Test public void shouldDecodeSomeHolder() throws DecodingException { Codec<SomeHolder> codec = Codecs.create(SomeHolder.class, new VariableLengthByteArrayCodecFactory()); SomeHolder holder = Codecs.decode(codec, (byte) 0xff, (byte) 0x0f); assertThat(holder.getValue(), is(not(nullValue()))); assertThat(holder.getValue().length, is(2)); assertThat(holder.getValue()[0], is((byte) 0xff)); assertThat(holder.getValue()[1], is((byte) 0x0f)); Codecs.document(codec, Codecs.DocumentType.Html, System.err); }