Exemplo n.º 1
0
  @Test
  public void testRestorePositionAfterSizeException() throws Exception {
    byte[] array1 = createTestByteArray(100, (byte) 10);
    byte[] array2 = createTestByteArray(100, (byte) 20);
    byte[] tooBigArray = createTestByteArray(150, (byte) 30);

    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    try (DataOutputStreamEx dataOutputStream = DataOutputStreamEx.create(byteOutputStream)) {
      dataOutputStream.serialize(bytesSerializer(), array1, MAX_SIZE_127);
      try {
        dataOutputStream.serialize(bytesSerializer(), tooBigArray, MAX_SIZE_127);
      } catch (SerializeException ignored) {
      }
      dataOutputStream.serialize(bytesSerializer(), array2, MAX_SIZE_127);
    }

    ByteArrayInputStream byteArrayInputStream =
        new ByteArrayInputStream(byteOutputStream.toByteArray());
    try (DataInputStreamEx dataInputStream = DataInputStreamEx.create(byteArrayInputStream)) {
      assertArrayEquals(array1, dataInputStream.deserialize(bytesSerializer()));
      assertArrayEquals(array2, dataInputStream.deserialize(bytesSerializer()));
      assertTrue(dataInputStream.isEndOfStream());
    }

    assertEquals(getPoolItemsString(), getCreatedItems(), getPoolItems());
  }
Exemplo n.º 2
0
  @Test
  public void testStandardWrite() throws IOException {
    final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    try (final DataOutputStreamEx dataOutputStream =
        DataOutputStreamEx.create(byteOutputStream, 1)) {
      dataOutputStream.writeInt(42);
      dataOutputStream.writeBoolean(false);
      dataOutputStream.writeByte((byte) 43);
      dataOutputStream.writeChar((char) 44);
      dataOutputStream.writeDouble(45.46);
      dataOutputStream.writeFloat(47.48f);
      dataOutputStream.writeIso88591("49");
      dataOutputStream.writeIso88591(""); // check specific situation
      dataOutputStream.writeLong(50L);
      dataOutputStream.writeShort((short) 51);
      dataOutputStream.writeUTF8("test 52");
      dataOutputStream.writeUTF8(""); // check specific situation
      dataOutputStream.writeUTF16("тест 53");
      dataOutputStream.writeUTF16(""); // check specific situation
      dataOutputStream.writeVarInt(-54);
      dataOutputStream.writeVarLong(-55);
      dataOutputStream.write(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
      dataOutputStream.write(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 5, 2);
    }

    final ByteArrayInputStream byteArrayInputStream =
        new ByteArrayInputStream(byteOutputStream.toByteArray());
    try (final DataInputStreamEx dataInputStream = DataInputStreamEx.create(byteArrayInputStream)) {
      assertEquals(42, dataInputStream.readInt());
      assertEquals(false, dataInputStream.readBoolean());
      assertEquals((byte) 43, dataInputStream.readByte());
      assertEquals((char) 44, dataInputStream.readChar());
      assertEquals(45.46, dataInputStream.readDouble(), 1e-6);
      assertEquals(47.48f, dataInputStream.readFloat(), 1e-6);
      assertEquals("49", dataInputStream.readIso88591());
      assertEquals("", dataInputStream.readIso88591()); // check specific situation
      assertEquals(50L, dataInputStream.readLong());
      assertEquals((short) 51, dataInputStream.readShort());
      assertEquals("test 52", dataInputStream.readUTF8());
      assertEquals("", dataInputStream.readUTF8()); // check specific situation
      assertEquals("тест 53", dataInputStream.readUTF16());
      assertEquals("", dataInputStream.readUTF16()); // check specific situation
      assertEquals(-54, dataInputStream.readVarInt());
      assertEquals(-55, dataInputStream.readVarLong());
      assertArrayEquals(
          new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, read(dataInputStream, new byte[10]));
      assertArrayEquals(
          new byte[] {0, 0, 0, 0, 0, 6, 7, 0, 0, 0}, read(dataInputStream, new byte[10], 5, 2));
      assertTrue(dataInputStream.isEndOfStream());
    }

    assertEquals(getPoolItemsString(), getCreatedItems(), getPoolItems());
  }
Exemplo n.º 3
0
  @Test
  public void test() throws IOException, SerializeException, DeserializeException {
    String[] strings = new String[] {"test1-string", "test2-int", "test3-t", "test4-str"};
    BufferSerializer<String> bufferSerializer =
        SerializerBuilder.create(DefiningClassLoader.create()).build(String.class);

    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    try (DataOutputStreamEx dataOutputStream = DataOutputStreamEx.create(byteOutputStream, 30)) {
      for (String string : strings) {
        dataOutputStream.serialize(bufferSerializer, string, MAX_SIZE_127);
      }
    }

    ByteArrayInputStream byteInputStream = new ByteArrayInputStream(byteOutputStream.toByteArray());
    try (DataInputStreamEx dataInputStream = DataInputStreamEx.create(byteInputStream, 50)) {
      final List<String> newStrings = new ArrayList<>(4);
      while (!dataInputStream.isEndOfStream()) {
        newStrings.add(dataInputStream.deserialize(bufferSerializer));
      }

      assertArrayEquals(strings, newStrings.toArray(new String[4]));
      assertTrue(dataInputStream.isEndOfStream());
    }

    assertEquals(getPoolItemsString(), getCreatedItems(), getPoolItems());
  }