Example usage for java.util.zip Deflater Deflater

List of usage examples for java.util.zip Deflater Deflater

Introduction

In this page you can find the example usage for java.util.zip Deflater Deflater.

Prototype

public Deflater(int level) 

Source Link

Document

Creates a new compressor using the specified compression level.

Usage

From source file:com.newrelic.agent.transport.DataSenderImpl.java

private OutputStream getOutputStream(OutputStream out, String encoding)
        /* 966:    */ throws IOException
/* 967:    */ {//from  ww w.j  av  a  2  s  . c  o m
    /* 968:776 */ if ("deflate".equals(encoding)) {
        /* 969:777 */ return new DeflaterOutputStream(out, new Deflater(-1));
        /* 970:    */ }
    /* 971:779 */ return out;
    /* 972:    */ }

From source file:com.ichi2.anki.SyncClient.java

public static void fullSyncFromLocal(String password, String username, String deckName, String deckPath) {
    URL url;//from  w  w w .ja  v a2s .c  om
    try {
        Log.i(AnkiDroidApp.TAG, "Fullup");
        url = new URL(AnkiDroidProxy.SYNC_URL + "fullup");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");

        conn.setRequestProperty("Connection", "close");
        conn.setRequestProperty("Charset", "UTF-8");
        // conn.setRequestProperty("Content-Length", "8494662");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + MIME_BOUNDARY);
        conn.setRequestProperty("Host", AnkiDroidProxy.SYNC_HOST);

        DataOutputStream ds = new DataOutputStream(conn.getOutputStream());
        Log.i(AnkiDroidApp.TAG, "Pass");
        ds.writeBytes(TWO_HYPHENS + MIME_BOUNDARY + END);
        ds.writeBytes("Content-Disposition: form-data; name=\"p\"" + END + END + password + END);
        Log.i(AnkiDroidApp.TAG, "User");
        ds.writeBytes(TWO_HYPHENS + MIME_BOUNDARY + END);
        ds.writeBytes("Content-Disposition: form-data; name=\"u\"" + END + END + username + END);
        Log.i(AnkiDroidApp.TAG, "DeckName");
        ds.writeBytes(TWO_HYPHENS + MIME_BOUNDARY + END);
        ds.writeBytes("Content-Disposition: form-data; name=\"d\"" + END + END + deckName + END);
        Log.i(AnkiDroidApp.TAG, "Deck");
        ds.writeBytes(TWO_HYPHENS + MIME_BOUNDARY + END);
        ds.writeBytes("Content-Disposition: form-data; name=\"deck\";filename=\"deck\"" + END);
        ds.writeBytes("Content-Type: application/octet-stream" + END);
        ds.writeBytes(END);

        FileInputStream fStream = new FileInputStream(deckPath);
        byte[] buffer = new byte[Utils.CHUNK_SIZE];
        int length = -1;

        Deflater deflater = new Deflater(Deflater.BEST_SPEED);
        DeflaterOutputStream dos = new DeflaterOutputStream(ds, deflater);

        Log.i(AnkiDroidApp.TAG, "Writing buffer...");
        while ((length = fStream.read(buffer)) != -1) {
            dos.write(buffer, 0, length);
            Log.i(AnkiDroidApp.TAG, "Length = " + length);
        }
        dos.finish();
        fStream.close();

        ds.writeBytes(END);
        ds.writeBytes(TWO_HYPHENS + MIME_BOUNDARY + TWO_HYPHENS + END);
        Log.i(AnkiDroidApp.TAG, "Closing streams...");

        ds.flush();
        ds.close();

        // Ensure we got the HTTP 200 response code
        int responseCode = conn.getResponseCode();
        if (responseCode != 200) {
            Log.i(AnkiDroidApp.TAG, "Response code = " + responseCode);
            // throw new Exception(String.format("Received the response code %d from the URL %s", responseCode,
            // url));
        } else {
            Log.i(AnkiDroidApp.TAG, "Response code = 200");
        }

        // Read the response
        InputStream is = conn.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int bytesRead;
        while ((bytesRead = is.read(bytes)) != -1) {
            baos.write(bytes, 0, bytesRead);
        }
        byte[] bytesReceived = baos.toByteArray();
        baos.close();

        is.close();
        String response = new String(bytesReceived);

        Log.i(AnkiDroidApp.TAG, "Finished!");
    } catch (MalformedURLException e) {
        Log.i(AnkiDroidApp.TAG, "MalformedURLException = " + e.getMessage());
    } catch (IOException e) {
        Log.i(AnkiDroidApp.TAG, "IOException = " + e.getMessage());
    }
}

From source file:org.apache.flex.swf.io.SWFWriter.java

/**
 * This method does not close the {@code output} stream.
 *//*from  w ww  .ja v a 2 s. com*/
@Override
public void writeTo(OutputStream output) {
    assert output != null;

    writtenTags = new HashSet<ITag>();

    // The SWF data after the first 8 bytes can be compressed. At this
    // moment, we only encode the "compressible" part.
    writeCompressibleHeader();

    // FileAttributes must be the first tag.
    writeTag(SWF.getFileAttributes(swf));

    // Raw Metadata
    String metadata = swf.getMetadata();

    if (metadata != null)
        writeTag(new MetadataTag(metadata));

    // SetBackgroundColor tag
    final RGB backgroundColor = swf.getBackgroundColor();
    if (backgroundColor != null)
        writeTag(new SetBackgroundColorTag(backgroundColor));

    // EnableDebugger2 tag        
    if (enableDebug)
        writeTag(new EnableDebugger2Tag("NO-PASSWORD"));

    // ProductInfo tag for Flex compatibility
    ProductInfoTag productInfo = swf.getProductInfo();
    if (productInfo != null)
        writeTag(productInfo);

    // ScriptLimits tag
    final ScriptLimitsTag scriptLimitsTag = swf.getScriptLimits();
    if (scriptLimitsTag != null)
        writeTag(scriptLimitsTag);

    // Frames and enclosed tags.
    writeFrames();

    // End of SWF
    writeTag(new EndTag());

    writtenTags = null;

    // Compute the size of the SWF file.
    long length = outputBuffer.size() + 8;
    try {
        // write the first 8 bytes
        switch (useCompression) {
        case LZMA:
            output.write('Z');
            break;
        case ZLIB:
            output.write('C');
            break;
        case NONE:
            output.write('F');
            break;
        default:
            assert false;
        }

        output.write('W');
        output.write('S');
        output.write(swf.getVersion());

        writeInt(output, (int) length);

        // write the "compressible" part
        switch (useCompression) {
        case LZMA: {
            LZMACompressor compressor = new LZMACompressor();
            compressor.compress(outputBuffer);
            // now write the compressed length
            final long compressedLength = compressor.getLengthOfCompressedPayload();
            assert compressedLength <= 0xffffffffl;

            writeInt(output, (int) compressedLength);

            // now write the LZMA props
            compressor.writeLZMAProperties(output);

            // Normally LZMA (7zip) would write an 8 byte length here, but we don't, because the
            // SWF header already has this info

            // now write the n bytes of LZMA data, followed by the 6 byte EOF
            compressor.writeDataAndEnd(output);
            output.flush();
        }
            break;
        case ZLIB: {
            int compressionLevel = enableDebug ? Deflater.BEST_SPEED : Deflater.BEST_COMPRESSION;
            Deflater deflater = new Deflater(compressionLevel);
            DeflaterOutputStream deflaterStream = new DeflaterOutputStream(output, deflater);
            deflaterStream.write(outputBuffer.getBytes(), 0, outputBuffer.size());
            deflaterStream.finish();
            deflater.end();
            deflaterStream.flush();
            break;
        }
        case NONE: {
            output.write(outputBuffer.getBytes(), 0, outputBuffer.size());
            output.flush();
            break;
        }
        default:
            assert false;
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}