org.jboss.marshalling.ByteInput Java Examples

The following examples show how to use org.jboss.marshalling.ByteInput. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MarshallingDecoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;
    }

    Unmarshaller unmarshaller = provider.getUnmarshaller(ctx);
    ByteInput input = new ChannelBufferByteInput(frame);

    try {
        unmarshaller.start(input);
        Object obj = unmarshaller.readObject();
        unmarshaller.finish();
        return obj;
    } finally {
        // Call close in a finally block as the ReplayingDecoder will throw an Error if not enough bytes are
        // readable. This helps to be sure that we do not leak resource
        unmarshaller.close();
    }
}
 
Example #2
Source File: MarshallingDecoder.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;
    }

    Unmarshaller unmarshaller = provider.getUnmarshaller(ctx);
    ByteInput input = new ChannelBufferByteInput(frame);

    try {
        unmarshaller.start(input);
        Object obj = unmarshaller.readObject();
        unmarshaller.finish();
        return obj;
    } finally {
        // Call close in a finally block as the ReplayingDecoder will throw an Error if not enough bytes are
        // readable. This helps to be sure that we do not leak resource
        unmarshaller.close();
    }
}
 
Example #3
Source File: ChunkyByteInputOutputTest.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testEqualBuffer() throws Exception {
    final byte[] content = "1234567890".getBytes(StandardCharsets.UTF_8);

    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final ByteOutput byteOutput = new ChunkyByteOutput(Marshalling.createByteOutput(byteArrayOutputStream), 10);

    byteOutput.write(content);
    byteOutput.flush();

    final byte[] chunked = byteArrayOutputStream.toByteArray();

    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(chunked);
    final ByteInput byteInput = new ChunkyByteInput(Marshalling.createByteInput(byteArrayInputStream));
    byte[] result = new byte[content.length];
    byteInput.read(result);
    byteInput.close();

    Assert.assertArrayEquals(content, result);
    Assert.assertEquals(-1, byteInput.read());
}
 
Example #4
Source File: ChunkyByteInputOutputTest.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testMultiChunk() throws Exception {
    final byte[] content = "12345678901234567890123456789012345678901234567890".getBytes(StandardCharsets.UTF_8);

    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final ByteOutput byteOutput = new ChunkyByteOutput(Marshalling.createByteOutput(byteArrayOutputStream), 10);

    byteOutput.write(content);
    byteOutput.flush();

    final byte[] chunked = byteArrayOutputStream.toByteArray();

    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(chunked);
    final ByteInput byteInput = new ChunkyByteInput(Marshalling.createByteInput(byteArrayInputStream));
    byte[] result = new byte[content.length];
    byteInput.read(result);
    byteInput.close();

    Assert.assertArrayEquals(content, result);
    Assert.assertEquals(-1, byteInput.read());
}
 
Example #5
Source File: ChunkyByteInputOutputTest.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testRemainingBytes() throws Exception {
    final byte[] content = "1234567890123456789012345678901234567890123".getBytes(StandardCharsets.UTF_8);

    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final ByteOutput byteOutput = new ChunkyByteOutput(Marshalling.createByteOutput(byteArrayOutputStream), 10);

    byteOutput.write(content);
    byteOutput.flush();

    final byte[] chunked = byteArrayOutputStream.toByteArray();

    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(chunked);
    final ByteInput byteInput = new ChunkyByteInput(Marshalling.createByteInput(byteArrayInputStream));
    byte[] result = new byte[content.length];
    byteInput.read(result);
    byteInput.close();

    Assert.assertArrayEquals(content, result);
    Assert.assertEquals(-1, byteInput.read());
}
 
Example #6
Source File: ChunkyByteInputOutputTest.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testIncompleteRead() throws Exception {
    final byte[] content = "1234567890123456789012345678901234567890123".getBytes(StandardCharsets.UTF_8);

    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final ByteOutput byteOutput = new ChunkyByteOutput(Marshalling.createByteOutput(byteArrayOutputStream), 10);

    byteOutput.write(content);
    byteOutput.flush();

    final byte[] chunked = byteArrayOutputStream.toByteArray();

    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(chunked);
    final ByteInput byteInput = new ChunkyByteInput(Marshalling.createByteInput(byteArrayInputStream));
    int readLength = content.length - 15;
    byte[] result = new byte[readLength];
    byteInput.read(result);
    byteInput.close();

    byte[] expected = new byte[readLength];
    System.arraycopy(content, 0, expected, 0, readLength);

    Assert.assertArrayEquals(expected, result);
    Assert.assertEquals(-1, byteInput.read());
}
 
Example #7
Source File: ChunkyByteInputOutputTest.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testOffsetRead() throws Exception {
    final byte[] content = "1234567890123456789012345678901234567890123".getBytes(StandardCharsets.UTF_8);

    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final ByteOutput byteOutput = new ChunkyByteOutput(Marshalling.createByteOutput(byteArrayOutputStream), 10);

    byteOutput.write(content);
    byteOutput.flush();

    final byte[] chunked = byteArrayOutputStream.toByteArray();

    final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(chunked);
    final ByteInput byteInput = new ChunkyByteInput(Marshalling.createByteInput(byteArrayInputStream));

    int readLength = 5;
    byte[] result = new byte[content.length];
    byteInput.read(result, content.length - 6, readLength);
    byteInput.close();

    byte[] expected = new byte[content.length];
    System.arraycopy(content, 0, expected, content.length - 6, readLength);

    Assert.assertArrayEquals(expected, result);
    Assert.assertEquals(-1, byteInput.read());
}
 
Example #8
Source File: LimitingByteInput.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
LimitingByteInput(ByteInput input, long limit) {
    if (limit <= 0) {
        throw new IllegalArgumentException("The limit MUST be > 0");
    }
    this.input = input;
    this.limit = limit;
}
 
Example #9
Source File: CompatibleMarshallingDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    if (discardingTooLongFrame) {
        buffer.skipBytes(actualReadableBytes());
        checkpoint();
        return;
    }

    Unmarshaller unmarshaller = provider.getUnmarshaller(ctx);
    ByteInput input = new ChannelBufferByteInput(buffer);
    if (maxObjectSize != Integer.MAX_VALUE) {
        input = new LimitingByteInput(input, maxObjectSize);
    }
    try {
        unmarshaller.start(input);
        Object obj = unmarshaller.readObject();
        unmarshaller.finish();
        out.add(obj);
    } catch (LimitingByteInput.TooBigObjectException ignored) {
        discardingTooLongFrame = true;
        throw new TooLongFrameException();
    } finally {
        // Call close in a finally block as the ReplayingDecoder will throw an Error if not enough bytes are
        // readable. This helps to be sure that we do not leak resource
        unmarshaller.close();
    }
}
 
Example #10
Source File: LimitingByteInput.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
LimitingByteInput(ByteInput input, long limit) {
    if (limit <= 0) {
        throw new IllegalArgumentException("The limit MUST be > 0");
    }
    this.input = input;
    this.limit = limit;
}
 
Example #11
Source File: CompatibleMarshallingDecoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    if (discardingTooLongFrame) {
        buffer.skipBytes(actualReadableBytes());
        checkpoint();
        return;
    }

    Unmarshaller unmarshaller = provider.getUnmarshaller(ctx);
    ByteInput input = new ChannelBufferByteInput(buffer);
    if (maxObjectSize != Integer.MAX_VALUE) {
        input = new LimitingByteInput(input, maxObjectSize);
    }
    try {
        unmarshaller.start(input);
        Object obj = unmarshaller.readObject();
        unmarshaller.finish();
        out.add(obj);
    } catch (LimitingByteInput.TooBigObjectException ignored) {
        discardingTooLongFrame = true;
        throw new TooLongFrameException();
    } finally {
        // Call close in a finally block as the ReplayingDecoder will throw an Error if not enough bytes are
        // readable. This helps to be sure that we do not leak resource
        unmarshaller.close();
    }
}
 
Example #12
Source File: ChunkyByteInput.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public ChunkyByteInput(final ByteInput byteInput) {
    input = byteInput;
}