Search in sources :

Example 1 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project jetty.project by eclipse.

the class GZIPContentDecoderTest method testSmallBlockWithGZIPChunkedAtEnd.

@Test
public void testSmallBlockWithGZIPChunkedAtEnd() throws Exception {
    String data = "0";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream output = new GZIPOutputStream(baos);
    output.write(data.getBytes(StandardCharsets.UTF_8));
    output.close();
    byte[] bytes = baos.toByteArray();
    // The trailer is 8 bytes, chunk the last 9 bytes
    byte[] bytes1 = new byte[bytes.length - 9];
    System.arraycopy(bytes, 0, bytes1, 0, bytes1.length);
    byte[] bytes2 = new byte[bytes.length - bytes1.length];
    System.arraycopy(bytes, bytes1.length, bytes2, 0, bytes2.length);
    GZIPContentDecoder decoder = new GZIPContentDecoder(pool, 2048);
    ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1));
    assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString());
    assertFalse(decoder.isFinished());
    decoder.release(decoded);
    decoded = decoder.decode(ByteBuffer.wrap(bytes2));
    assertEquals(0, decoded.remaining());
    assertTrue(decoder.isFinished());
    decoder.release(decoded);
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project jetty.project by eclipse.

the class GZIPContentDecoderTest method testBigBlock.

@Test
public void testBigBlock() throws Exception {
    String data = "0123456789ABCDEF";
    for (int i = 0; i < 10; ++i) data += data;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream output = new GZIPOutputStream(baos);
    output.write(data.getBytes(StandardCharsets.UTF_8));
    output.close();
    byte[] bytes = baos.toByteArray();
    String result = "";
    GZIPContentDecoder decoder = new GZIPContentDecoder(pool, 2048);
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    while (buffer.hasRemaining()) {
        ByteBuffer decoded = decoder.decode(buffer);
        result += StandardCharsets.UTF_8.decode(decoded).toString();
        decoder.release(decoded);
    }
    assertEquals(data, result);
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project jetty.project by eclipse.

the class GZIPContentDecoderTest method testNoBlocks.

@Test
public void testNoBlocks() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream output = new GZIPOutputStream(baos);
    output.close();
    byte[] bytes = baos.toByteArray();
    GZIPContentDecoder decoder = new GZIPContentDecoder(pool, 2048);
    ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes));
    assertEquals(0, decoded.remaining());
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project jetty.project by eclipse.

the class GZIPContentDecoderTest method testSmallBlock.

@Test
public void testSmallBlock() throws Exception {
    String data = "0";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream output = new GZIPOutputStream(baos);
    output.write(data.getBytes(StandardCharsets.UTF_8));
    output.close();
    byte[] bytes = baos.toByteArray();
    GZIPContentDecoder decoder = new GZIPContentDecoder(pool, 2048);
    ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes));
    assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString());
    decoder.release(decoded);
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 5 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project jetty.project by eclipse.

the class GZIPContentDecoderTest method testSmallBlockWithGZIPTrailerChunked.

@Test
public void testSmallBlockWithGZIPTrailerChunked() throws Exception {
    String data = "0";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream output = new GZIPOutputStream(baos);
    output.write(data.getBytes(StandardCharsets.UTF_8));
    output.close();
    byte[] bytes = baos.toByteArray();
    // The trailer is 4+4 bytes, chunk the last 3 bytes
    byte[] bytes1 = new byte[bytes.length - 3];
    System.arraycopy(bytes, 0, bytes1, 0, bytes1.length);
    byte[] bytes2 = new byte[bytes.length - bytes1.length];
    System.arraycopy(bytes, bytes1.length, bytes2, 0, bytes2.length);
    GZIPContentDecoder decoder = new GZIPContentDecoder(pool, 2048);
    ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1));
    assertEquals(0, decoded.capacity());
    decoder.release(decoded);
    decoded = decoder.decode(ByteBuffer.wrap(bytes2));
    assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString());
    decoder.release(decoded);
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)17577 ByteArrayInputStream (java.io.ByteArrayInputStream)4644 Test (org.junit.Test)4581 IOException (java.io.IOException)4284 InputStream (java.io.InputStream)1946 ObjectOutputStream (java.io.ObjectOutputStream)1662 PrintStream (java.io.PrintStream)1531 DataOutputStream (java.io.DataOutputStream)1303 ArrayList (java.util.ArrayList)866 ObjectInputStream (java.io.ObjectInputStream)815 OutputStream (java.io.OutputStream)800 File (java.io.File)765 HashMap (java.util.HashMap)555 Test (org.junit.jupiter.api.Test)526 FileInputStream (java.io.FileInputStream)456 Document (org.w3c.dom.Document)380 DataInputStream (java.io.DataInputStream)378 OutputStreamWriter (java.io.OutputStreamWriter)364 URL (java.net.URL)355 StreamResult (javax.xml.transform.stream.StreamResult)337