Search in sources :

Example 1 with DeflaterInputStream

use of java.util.zip.DeflaterInputStream in project robovm by robovm.

the class DeflaterInputStreamTest method testReadByteByByte.

public void testReadByteByByte() throws IOException {
    byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    InputStream in = new DeflaterInputStream(new ByteArrayInputStream(data));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    assertEquals(1, in.available());
    int b;
    while ((b = in.read()) != -1) {
        out.write(b);
    }
    assertEquals(0, in.available());
    assertEquals(Arrays.toString(data), Arrays.toString(inflate(out.toByteArray())));
    in.close();
    try {
        in.available();
        fail();
    } catch (IOException expected) {
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DeflaterInputStream(java.util.zip.DeflaterInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) DeflaterInputStream(java.util.zip.DeflaterInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 2 with DeflaterInputStream

use of java.util.zip.DeflaterInputStream in project robovm by robovm.

the class DeflaterInputStreamTest method testReadExceptions.

public void testReadExceptions() throws IOException {
    byte[] data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    byte[] buffer = new byte[8];
    InputStream in = new DeflaterInputStream(new ByteArrayInputStream(data));
    try {
        in.read(buffer, 0, 10);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        in.read(null, 0, 5);
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        in.read(buffer, -1, 5);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    in.close();
    try {
        in.read(buffer, 0, 5);
        fail();
    } catch (IOException expected) {
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DeflaterInputStream(java.util.zip.DeflaterInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) InputStream(java.io.InputStream) DeflaterInputStream(java.util.zip.DeflaterInputStream) IOException(java.io.IOException)

Example 3 with DeflaterInputStream

use of java.util.zip.DeflaterInputStream in project bazel by bazelbuild.

the class ZipCombiner method writeEntryFromBuffer.

/** Writes an entry with the given name, date and external file attributes from the buffer. */
private void writeEntryFromBuffer(ZipFileEntry entry, byte[] uncompressed) throws IOException {
    CRC32 crc = new CRC32();
    crc.update(uncompressed);
    entry.setCrc(crc.getValue());
    entry.setSize(uncompressed.length);
    if (mode == OutputMode.FORCE_STORED) {
        entry.setMethod(Compression.STORED);
        entry.setCompressedSize(uncompressed.length);
        writeEntry(entry, new ByteArrayInputStream(uncompressed));
    } else {
        ByteArrayOutputStream compressed = new ByteArrayOutputStream();
        copyStream(new DeflaterInputStream(new ByteArrayInputStream(uncompressed), getDeflater()), compressed);
        entry.setMethod(Compression.DEFLATED);
        entry.setCompressedSize(compressed.size());
        writeEntry(entry, new ByteArrayInputStream(compressed.toByteArray()));
    }
}
Also used : CRC32(java.util.zip.CRC32) ByteArrayInputStream(java.io.ByteArrayInputStream) DeflaterInputStream(java.util.zip.DeflaterInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with DeflaterInputStream

use of java.util.zip.DeflaterInputStream in project bazel by bazelbuild.

the class ZipCombiner method writeEntry.

/**
   * Writes an entry from the specified source {@link ZipReader} and {@link ZipFileEntry} using the
   * specified {@link EntryAction}.
   * 
   *  <p>Writes the output entry from the input entry performing inflation or deflation as needed
   *  and applies any values from the {@link EntryAction} as needed.
   */
private void writeEntry(ZipReader zip, ZipFileEntry entry, EntryAction action) throws IOException {
    checkArgument(action.getType() != ActionType.SKIP, "Cannot write a zip entry whose action is of type SKIP.");
    ZipFileEntry outEntry = new ZipFileEntry(entry);
    if (action.getType() == ActionType.RENAME) {
        checkNotNull(action.getNewName(), "ZipEntryFilter actions of type RENAME must not have a null filename.");
        outEntry.setName(action.getNewName());
    }
    if (action.getDate() != null) {
        outEntry.setTime(action.getDate().getTime());
    }
    InputStream data;
    if (mode == OutputMode.FORCE_DEFLATE && entry.getMethod() != Compression.DEFLATED) {
        // The output mode is deflate, but the entry compression is not. Create a deflater stream
        // from the raw file data and deflate to a temporary byte array to determine the deflated
        // size. Then use this byte array as the input stream for writing the entry.
        ByteArrayOutputStream tmp = new ByteArrayOutputStream();
        copyStream(new DeflaterInputStream(zip.getRawInputStream(entry), getDeflater()), tmp);
        data = new ByteArrayInputStream(tmp.toByteArray());
        outEntry.setMethod(Compression.DEFLATED);
        outEntry.setCompressedSize(tmp.size());
    } else if (mode == OutputMode.FORCE_STORED && entry.getMethod() != Compression.STORED) {
        // The output mode is stored, but the entry compression is not; create an inflater stream
        // from the raw file data. 
        data = new InflaterInputStream(zip.getRawInputStream(entry), getInflater());
        outEntry.setMethod(Compression.STORED);
        outEntry.setCompressedSize(entry.getSize());
    } else {
        // Entry compression agrees with output mode; use the raw file data as is.
        data = zip.getRawInputStream(entry);
    }
    writeEntry(outEntry, data);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DeflaterInputStream(java.util.zip.DeflaterInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) DeflaterInputStream(java.util.zip.DeflaterInputStream) ZipFileEntry(com.google.devtools.build.zip.ZipFileEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with DeflaterInputStream

use of java.util.zip.DeflaterInputStream in project intellij-community by JetBrains.

the class BinaryEncoder method encode.

public static void encode(@NotNull InputStream input, @NotNull Writer writer) throws IOException, BinaryPatchException {
    int maxLineSize = 52;
    byte[] deflated = new byte[maxLineSize];
    try (DeflaterInputStream deflaterStream = new DeflaterInputStream(input)) {
        int lineSize;
        do {
            lineSize = deflaterStream.read(deflated, 0, maxLineSize);
            if (lineSize <= 0)
                break;
            writer.append(getCharForLineSize(lineSize));
            //fill encoded block to be divisible by 4
            int newSize = ((lineSize + 3) / 4) * 4;
            Arrays.fill(deflated, lineSize, newSize, (byte) 0);
            writer.append(new String(Base85x.encode(deflated, newSize)));
            writer.append('\n');
        } while (lineSize > 0);
    } catch (Base85x.Base85FormatException e) {
        throw new BinaryPatchException(e);
    }
}
Also used : DeflaterInputStream(java.util.zip.DeflaterInputStream) Base85x(com.intellij.openapi.diff.impl.patch.lib.base85xjava.Base85x)

Aggregations

DeflaterInputStream (java.util.zip.DeflaterInputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 InputStream (java.io.InputStream)7 InflaterInputStream (java.util.zip.InflaterInputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 IOException (java.io.IOException)6 ZipFileEntry (com.google.devtools.build.zip.ZipFileEntry)1 Base85x (com.intellij.openapi.diff.impl.patch.lib.base85xjava.Base85x)1 CRC32 (java.util.zip.CRC32)1