Search in sources :

Example 21 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project jetty.project by eclipse.

the class GZIPContentDecoderTest method testStreamNoBlocks.

@Test
public void testStreamNoBlocks() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream output = new GZIPOutputStream(baos);
    output.close();
    byte[] bytes = baos.toByteArray();
    GZIPInputStream input = new GZIPInputStream(new ByteArrayInputStream(bytes), 1);
    int read = input.read();
    assertEquals(-1, read);
}
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 22 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream in project jetty.project by eclipse.

the class GZIPContentDecoderTest method testBigBlockWithExtraBytes.

@Test
public void testBigBlockWithExtraBytes() throws Exception {
    String data1 = "0123456789ABCDEF";
    for (int i = 0; i < 10; ++i) data1 += data1;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream output = new GZIPOutputStream(baos);
    output.write(data1.getBytes(StandardCharsets.UTF_8));
    output.close();
    byte[] bytes1 = baos.toByteArray();
    String data2 = "HELLO";
    byte[] bytes2 = data2.getBytes(StandardCharsets.UTF_8);
    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);
    String result = "";
    GZIPContentDecoder decoder = new GZIPContentDecoder(64);
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    while (buffer.hasRemaining()) {
        ByteBuffer decoded = decoder.decode(buffer);
        if (decoded.hasRemaining())
            result += StandardCharsets.UTF_8.decode(decoded).toString();
        if (decoder.isFinished())
            break;
    }
    assertEquals(data1, result);
    assertTrue(buffer.hasRemaining());
    assertEquals(data2, StandardCharsets.UTF_8.decode(buffer).toString());
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 23 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream 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();
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    while (buffer.hasRemaining()) {
        ByteBuffer decoded = decoder.decode(buffer);
        result += StandardCharsets.UTF_8.decode(decoded).toString();
    }
    assertEquals(data, result);
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 24 with GZIPOutputStream

use of java.util.zip.GZIPOutputStream 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();
    ByteBuffer decoded = decoder.decode(ByteBuffer.wrap(bytes));
    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 25 with GZIPOutputStream

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

Aggregations

GZIPOutputStream (java.util.zip.GZIPOutputStream)322 ByteArrayOutputStream (java.io.ByteArrayOutputStream)135 FileOutputStream (java.io.FileOutputStream)96 IOException (java.io.IOException)93 OutputStream (java.io.OutputStream)66 File (java.io.File)62 Test (org.junit.Test)48 BufferedOutputStream (java.io.BufferedOutputStream)30 FileInputStream (java.io.FileInputStream)28 OutputStreamWriter (java.io.OutputStreamWriter)27 GZIPInputStream (java.util.zip.GZIPInputStream)25 InputStream (java.io.InputStream)24 ByteBuffer (java.nio.ByteBuffer)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 BufferedWriter (java.io.BufferedWriter)18 DropBoxManager (android.os.DropBoxManager)15 BufferedReader (java.io.BufferedReader)11 DataOutputStream (java.io.DataOutputStream)11 FileNotFoundException (java.io.FileNotFoundException)11 InputStreamReader (java.io.InputStreamReader)11