@SuppressWarnings("unchecked") @Override public T read(ByteBuffer entry) throws SerializerException, ClassNotFoundException { ByteBufferInputStream bin = new ByteBufferInputStream(entry); try { try (OIS ois = new OIS(bin, classLoader)) { return (T) ois.readObject(); } } catch (IOException e) { throw new SerializerException(e); } finally { try { bin.close(); } catch (IOException e) { throw new AssertionError(e); } } }
private ByteBufferInputStream createStream(boolean prePad, boolean postPad) { int prePadSize = prePad ? RANDOM.nextInt(32) : 0; int postPadSize = postPad ? RANDOM.nextInt(32) : 0; ByteBuffer buffer = ByteBuffer.allocate(64 + prePadSize + postPadSize); for (int i = 0; i < 64; i++) { buffer.put(i + prePadSize, (byte) i); } ByteBufferInputStream bin = new ByteBufferInputStream(buffer); for (int i = 0; i < prePadSize; i++) { assertThat(bin.read(), is(0)); } return bin; } }
/** * {@inheritDoc} */ @Override public T read(ByteBuffer binary) throws ClassNotFoundException, SerializerException { try { try (ObjectInputStream oin = getObjectInputStream(new ByteBufferInputStream(binary))) { @SuppressWarnings("unchecked") T value = (T) oin.readObject(); return value; } } catch (IOException e) { throw new SerializerException(e); } }
@Test public void testZeroOffsetAndZeroLength() { ByteBufferInputStream stream = createStream(); byte[] read = new byte[32]; stream.read(read, 0, 0); for (int i = 0; i < read.length; i++) { Assert.assertThat(read[i], is((byte) 0)); } }
@Test public void testMaximalOffsetAndZeroLength() { ByteBufferInputStream stream = createStream(); byte[] read = new byte[32]; stream.read(read, 32, 0); for (int i = 0; i < read.length; i++) { Assert.assertThat(read[i], is((byte) 0)); } }
@Override public SoftLock<T> read(ByteBuffer entry) throws SerializerException, ClassNotFoundException { ByteBufferInputStream bin = new ByteBufferInputStream(entry); try { try (OIS ois = new OIS(bin, classLoader)) { return uncheckedCast(ois.readObject()); } } catch (IOException e) { throw new SerializerException(e); } finally { try { bin.close(); } catch (IOException e) { throw new AssertionError(e); } } }
@Test public void testZeroOffsetAndMaximalLength() { ByteBufferInputStream stream = createStream(); byte[] read = new byte[32]; stream.read(read, 0, 32); for (int i = 0; i < read.length; i++) { Assert.assertThat(read[i], is((byte) i)); } }
@SuppressWarnings("unchecked") @Override public T read(ByteBuffer entry) throws SerializerException, ClassNotFoundException { ByteBufferInputStream bin = new ByteBufferInputStream(entry); try { try (OIS ois = new OIS(bin, classLoader)) { return (T) ois.readObject(); } } catch (IOException e) { throw new SerializerException(e); } finally { try { bin.close(); } catch (IOException e) { throw new AssertionError(e); } } }
@Test public void testMinimalOffsetAndMaximalLength() { ByteBufferInputStream stream = createStream(); byte[] read = new byte[32]; stream.read(read, 1, 31); Assert.assertThat(read[0], is((byte) 0)); for (int i = 1; i < read.length; i++) { Assert.assertThat(read[i], is((byte) (i - 1))); } }
@SuppressWarnings("unchecked") @Override public T read(ByteBuffer entry) throws SerializerException, ClassNotFoundException { ByteBufferInputStream bin = new ByteBufferInputStream(entry); try { try (OIS ois = new OIS(bin, classLoader)) { return (T) ois.readObject(); } } catch (IOException e) { throw new SerializerException(e); } finally { try { bin.close(); } catch (IOException e) { throw new AssertionError(e); } } }
@Test public void testNullReadArray() { ByteBufferInputStream stream = createStream(); try { stream.read(null, 0, 0); fail("Expected NullPointerException"); } catch (NullPointerException e) { //expected } }
@Test public void testZeroOffsetAndExcessiveLength() { ByteBufferInputStream stream = createStream(); try { stream.read(new byte[32], 0, 33); fail("Expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) { //expected } }
@Test public void testExcessiveOffsetAndLegalLength() { ByteBufferInputStream stream = createStream(); try { stream.read(new byte[32], 33, 4); fail("Expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) { //expected } }
@Test public void testNegativeOffsetAndLength() { ByteBufferInputStream stream = createStream(); try { stream.read(new byte[32], -1, -1); fail("Expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) { //expected } }
@Test public void testMaxOffsetAndMaxLength() { ByteBufferInputStream stream = createStream(); try { stream.read(new byte[32], Integer.MAX_VALUE, Integer.MAX_VALUE); fail("Expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) { //expected } }
@Test public void testMinOffsetAndUnitLength() { ByteBufferInputStream stream = createStream(); try { stream.read(new byte[32], Integer.MIN_VALUE, 1); fail("Expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException e) { //expected } }