org.jboss.marshalling.ByteOutput Java Examples

The following examples show how to use org.jboss.marshalling.ByteOutput. 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: 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 #2
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 #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 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 #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 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 #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 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 #6
Source File: ChunkyByteOutput.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Writes a {@code CHUNK_START} header followed by the size of chunk being flushed followed by the data being flushed.
 * It will then flush the underlying byte output.
 *
 * @throws IOException
 */
public void flush() throws IOException {
    final ByteOutput output = this.output;
    final int pos = this.position;
    if (pos > 0) {
        output.write(CHUNK_START);
        writeInt(pos);
        final byte[] buffer = this.buffer;
        output.write(buffer, 0, pos);
    }
    this.position = 0;
}
 
Example #7
Source File: ChunkyByteOutput.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void writeInt(final int i) throws IOException {
    final ByteOutput output = this.output;
    byte[] bytes = new byte[4];
    bytes[0] = (byte) (i >> 24);
    bytes[1] = (byte) (i >> 16);
    bytes[2] = (byte) (i >> 8);
    bytes[3] = (byte) i;
    output.write(bytes);
}
 
Example #8
Source File: ChunkyByteOutput.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public ChunkyByteOutput(final ByteOutput output) {
    this(output, 8192);
}
 
Example #9
Source File: ChunkyByteOutput.java    From wildfly-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public ChunkyByteOutput(final ByteOutput output, final int bufferSize) {
    this.output = output;
    buffer = new byte[bufferSize];
}