@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()); }
@Test public void testRestorePositionAfterException() throws Exception { TestClass validObj1 = new TestClass("abc"); TestClass validObj2 = new TestClass("=xyz="); TestClass invalidObj = new TestClass(null); BufferSerializer<TestClass> serializer = SerializerBuilder.create(DefiningClassLoader.create()).build(TestClass.class); ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); try (DataOutputStreamEx dataOutputStream = DataOutputStreamEx.create(byteOutputStream)) { dataOutputStream.serialize(serializer, validObj1, MAX_SIZE_127); try { dataOutputStream.serialize(serializer, invalidObj, MAX_SIZE_127); } catch (SerializeException ignored) { } dataOutputStream.serialize(serializer, validObj2, MAX_SIZE_127); } ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteOutputStream.toByteArray()); try (DataInputStreamEx dataInputStream = DataInputStreamEx.create(byteArrayInputStream)) { assertEquals(validObj1.str, dataInputStream.deserialize(serializer).str); assertEquals(validObj2.str, dataInputStream.deserialize(serializer).str); assertTrue(dataInputStream.isEndOfStream()); } assertEquals(getPoolItemsString(), getCreatedItems(), getPoolItems()); }
@Test public void testStartBufferLessThanMessage() throws IOException, SerializeException, DeserializeException { final BufferSerializer<TestClass> serializer = SerializerBuilder.create(DefiningClassLoader.create()).build(TestClass.class); final TestClass validObj1 = new TestClass("22222"); final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); try (final DataOutputStreamEx dataOutputStream = DataOutputStreamEx.create(byteOutputStream, 1)) { dataOutputStream.serialize(serializer, validObj1, MAX_SIZE_127); } final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteOutputStream.toByteArray()); try (final DataInputStreamEx dataInputStream = DataInputStreamEx.create(byteArrayInputStream)) { assertEquals(validObj1, dataInputStream.deserialize(serializer)); assertTrue(dataInputStream.isEndOfStream()); } assertEquals(getPoolItemsString(), getCreatedItems(), getPoolItems()); }