Search in sources :

Example 41 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();
    ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes1));
    assertEquals(0, decoded.capacity());
    decoded = decoder.decode(ByteBuffer.wrap(bytes2));
    assertEquals(data, StandardCharsets.UTF_8.decode(decoded).toString());
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 42 with ByteArrayOutputStream

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

the class GZIPContentDecoderTest method testBigBlockOneByteAtATime.

@Test
public void testBigBlockOneByteAtATime() 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(64);
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    while (buffer.hasRemaining()) {
        ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(new byte[] { buffer.get() }));
        if (decoded.hasRemaining())
            result += StandardCharsets.UTF_8.decode(decoded).toString();
    }
    assertEquals(data, result);
    assertTrue(decoder.isFinished());
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 43 with ByteArrayOutputStream

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

the class GZIPContentDecoderTest method testStreamBigBlockOneByteAtATime.

@Test
public void testStreamBigBlockOneByteAtATime() 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();
    baos = new ByteArrayOutputStream();
    GZIPInputStream input = new GZIPInputStream(new ByteArrayInputStream(bytes), 1);
    int read;
    while ((read = input.read()) >= 0) baos.write(read);
    assertEquals(data, new String(baos.toByteArray(), StandardCharsets.UTF_8));
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 44 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();
    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 45 with ByteArrayOutputStream

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

the class GZIPContentDecoderTest method testTwoSmallBlocks.

@Test
public void testTwoSmallBlocks() throws Exception {
    String data1 = "0";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream output = new GZIPOutputStream(baos);
    output.write(data1.getBytes(StandardCharsets.UTF_8));
    output.close();
    byte[] bytes1 = baos.toByteArray();
    String data2 = "1";
    baos = new ByteArrayOutputStream();
    output = new GZIPOutputStream(baos);
    output.write(data2.getBytes(StandardCharsets.UTF_8));
    output.close();
    byte[] bytes2 = baos.toByteArray();
    byte[] bytes = new byte[bytes1.length + bytes2.length];
    System.arraycopy(bytes1, 0, bytes, 0, bytes1.length);
    System.arraycopy(bytes2, 0, bytes, bytes1.length, bytes2.length);
    GZIPContentDecoder decoder = new GZIPContentDecoder();
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    ByteBuffer decoded = decoder.decode(buffer);
    assertEquals(data1, StandardCharsets.UTF_8.decode(decoded).toString());
    assertTrue(decoder.isFinished());
    assertTrue(buffer.hasRemaining());
    decoded = decoder.decode(buffer);
    assertEquals(data2, StandardCharsets.UTF_8.decode(decoded).toString());
    assertTrue(decoder.isFinished());
    assertFalse(buffer.hasRemaining());
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)17705 ByteArrayInputStream (java.io.ByteArrayInputStream)4669 Test (org.junit.Test)4609 IOException (java.io.IOException)4326 InputStream (java.io.InputStream)1957 ObjectOutputStream (java.io.ObjectOutputStream)1679 PrintStream (java.io.PrintStream)1544 DataOutputStream (java.io.DataOutputStream)1303 ArrayList (java.util.ArrayList)875 ObjectInputStream (java.io.ObjectInputStream)818 OutputStream (java.io.OutputStream)807 File (java.io.File)770 HashMap (java.util.HashMap)558 Test (org.junit.jupiter.api.Test)526 FileInputStream (java.io.FileInputStream)460 Document (org.w3c.dom.Document)382 DataInputStream (java.io.DataInputStream)378 OutputStreamWriter (java.io.OutputStreamWriter)365 URL (java.net.URL)358 GZIPOutputStream (java.util.zip.GZIPOutputStream)338