示例#1
0
  @Test
  public void shouldMarshalAllNumbers() throws IOException {
    ShortNumbers sn = new ShortNumbers();
    sn.setI8((byte) 5);
    sn.setU8((short) 200);
    sn.setI16((short) 1000);
    sn.setU16(50000);

    LongNumbers ln = new LongNumbers();
    ln.setI32(-100000);
    ln.setU32(3_000_000_000L);
    ln.setI24(5_000_000);
    ln.setU24(10_000_000);

    AllNumbers an = new AllNumbers();
    an.setShortNumbers(sn);
    an.setLongNumbers(ln);

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    Marshaller marshaller = dadlContext.createMarshaller();
    marshaller.marshal(an, os);
    byte[] bytes = os.toByteArray();
    assertThat(bytes.length, is(20));

    ByteArrayBitStreamReader reader = new ByteArrayBitStreamReader(bytes);
    assertThat(reader.readByte(), is((byte) 5));
    assertThat(reader.readUnsignedByte(), is(200));
    assertThat(reader.readShort(), is((short) 1000));
    assertThat(reader.readUnsignedShort(), is(50000));
    assertThat(reader.readBits(24), is(5_000_000L));
    assertThat(reader.readBits(24), is(10_000_000L));
    assertThat(reader.readInt(), is(-100000));
    assertThat(reader.readUnsignedInt(), is(3_000_000_000L));
    reader.close();
  }
示例#2
0
  @Test
  public void shouldParseAllNumbers() throws IOException {
    ByteArrayBitStreamWriter writer = new ByteArrayBitStreamWriter();
    writer.writeBits(5, 8);
    writer.writeBits(200, 8);
    writer.writeBits(1000, 16);
    writer.writeBits(50000, 16);
    writer.writeBits(5_000_000L, 24);
    writer.writeBits(10_000_000L, 24);
    writer.writeInt(-100000);
    writer.writeUnsignedInt(3_000_000_000L);
    writer.close();
    byte[] bytes = writer.toByteArray();
    assertThat(bytes.length, is(20));

    Unmarshaller parser = dadlContext.createUnmarshaller();
    AllNumbers an = parser.unmarshal(bytes, AllNumbers.class);
    assertThat(an, is(notNullValue()));
    ShortNumbers sn = an.getShortNumbers();
    assertThat(sn, is(notNullValue()));
    assertThat(sn.getI8(), is(5));
    assertThat(sn.getU8(), is(200));
    assertThat(sn.getI16(), is(1000));
    assertThat(sn.getU16(), is(50000));

    LongNumbers ln = an.getLongNumbers();
    assertThat(ln, is(notNullValue()));
    assertThat(ln.getI24(), is(5_000_000));
    assertThat(ln.getU24(), is(10_000_000));
    assertThat(ln.getI32(), is(-100_000));
    assertThat(ln.getU32(), is(3_000_000_000L));
  }