Example usage for java.util.zip DeflaterOutputStream DeflaterOutputStream

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

Introduction

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

Prototype

public DeflaterOutputStream(OutputStream out) 

Source Link

Document

Creates a new output stream with a default compressor and buffer size.

Usage

From source file:org.fejoa.library.messages.ZipEnvelope.java

static public byte[] zip(byte[] data, boolean isRawData) throws JSONException, IOException {
    JSONObject object = new JSONObject();
    object.put(Envelope.PACK_TYPE_KEY, ZIP_TYPE);
    if (isRawData)
        object.put(Envelope.CONTAINS_DATA_KEY, 1);
    object.put(ZIP_FORMAT_KEY, ZIP_FORMAT);
    String header = object.toString() + "\n";

    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    outStream.write(header.getBytes());//from  w w  w  .  j a v a  2  s . c o m
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(outStream);
    deflaterOutputStream.write(data);
    deflaterOutputStream.finish();
    outStream.close();
    return outStream.toByteArray();
}

From source file:org.openmrs.module.shr.contenthandler.api.ContentTest.java

private static byte[] compressZLib(String content) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DeflaterOutputStream zlibOut = new DeflaterOutputStream(out);
    zlibOut.write(content.getBytes());/*  w  w w.j ava 2 s .c o  m*/
    IOUtils.closeQuietly(zlibOut);
    return out.toByteArray();
}

From source file:org.eclipse.gyrex.jobs.internal.worker.JobInfo.java

public static byte[] asMessage(final JobInfo info) throws IOException {
    final Properties properties = new Properties();

    // collect properties
    final Map<String, String> jobProperties = info.getJobProperties();
    for (final Entry<String, String> entry : jobProperties.entrySet()) {
        properties.put(entry.getKey(), entry.getValue());
    }//from  w w  w.ja  v a  2 s . c o  m

    // put properties
    try {
        properties.put(VERSION, VERSION_VALUE);
        properties.put(ID, info.getJobId());
        properties.put(TYPE_ID, info.getJobTypeId());
        properties.put(CONTEXT_PATH, info.getContextPath().toString());
        properties.put(QUEUE_TRIGGER, info.getQueueTrigger());
        properties.put(QUEUE_TIMESTAMP, String.valueOf(info.getQueueTimestamp()));
        properties.put(LAST_SUCCESSFUL_START, String.valueOf(info.getLastSuccessfulStart()));
        if (StringUtils.isNotBlank(info.getScheduleInfo())) {
            properties.put(SCHEDULE_INFO, info.getScheduleInfo());
        }
    } catch (final NullPointerException e) {
        // we tried to put null into the properties map
        throw new IllegalArgumentException(String.format("Invalid job info: %s", info));
    }

    // create bytes
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try (final DeflaterOutputStream df = new DeflaterOutputStream(out)) {
        properties.store(df, null);
    }
    return out.toByteArray();
}

From source file:org.jasig.cas.authentication.principal.GoogleAccountsServiceTests.java

protected static String encodeMessage(final String xmlString) throws IOException {
    byte[] xmlBytes = xmlString.getBytes("UTF-8");
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteOutputStream);
    deflaterOutputStream.write(xmlBytes, 0, xmlBytes.length);
    deflaterOutputStream.close();/*  w  w w.  java 2 s  .c  om*/

    // next, base64 encode it
    Base64 base64Encoder = new Base64();
    byte[] base64EncodedByteArray = base64Encoder.encode(byteOutputStream.toByteArray());
    return new String(base64EncodedByteArray);
}

From source file:org.nickelproject.util.IoUtil.java

/**
 * Compress the given byte array using the deflate algorithm and return the
 * compressed byte array./*  www.  java 2  s  .  c  om*/
 */
public static byte[] deflate(final byte[] pInput) throws IOException {
    final ByteArrayOutputStream vByteArrayStream = new ByteArrayOutputStream(pInput.length);
    final OutputStream vDeflaterStream = new DeflaterOutputStream(vByteArrayStream);
    vDeflaterStream.write(pInput);
    vDeflaterStream.close();
    final byte[] vCompressedBytes = vByteArrayStream.toByteArray();
    return vCompressedBytes;
}

From source file:com.moesol.keys.EncodeUidsTest.java

private OutputStream makeCompressStream(OutputStream out) throws IOException {
    return new DeflaterOutputStream(out);
    //      return new GZIPOutputStream(out);
}

From source file:com.faceye.feature.util.http.DeflateUtils.java

/**
 * Returns a deflated copy of the input array.
 *///from w  w  w  . ja  v a  2 s .  c om
public static final byte[] deflate(byte[] in) {
    // compress using DeflaterOutputStream 
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream(in.length / EXPECTED_COMPRESSION_RATIO);

    DeflaterOutputStream outStream = new DeflaterOutputStream(byteOut);

    try {
        outStream.write(in);
    } catch (Exception e) {
        LOG.error("Error compressing: ", e);
    }

    try {
        outStream.close();
    } catch (IOException e) {
        LOG.error("Error closing: ", e);
    }

    return byteOut.toByteArray();
}

From source file:org.bimserver.plugins.classloaders.JarClassLoader.java

private void addDataToMap(JarInputStream jarInputStream, JarEntry entry) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream);
    IOUtils.copy(jarInputStream, deflaterOutputStream);
    deflaterOutputStream.finish();//from   w w w  .ja  v a2 s.c  o m
    map.put(entry.getName(), byteArrayOutputStream.toByteArray());
}

From source file:com.iflytek.spider.util.DeflateUtils.java

/**
 * Returns a deflated copy of the input array.
 *///w  w w. j  av a2 s. co m
public static final byte[] deflate(byte[] in) {
    // compress using DeflaterOutputStream 
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream(in.length / EXPECTED_COMPRESSION_RATIO);

    DeflaterOutputStream outStream = new DeflaterOutputStream(byteOut);

    try {
        outStream.write(in);
    } catch (Exception e) {
        e.printStackTrace(LogUtil.getWarnStream(LOG));
    }

    try {
        outStream.close();
    } catch (IOException e) {
        e.printStackTrace(LogUtil.getWarnStream(LOG));
    }

    return byteOut.toByteArray();
}

From source file:org.taverna.server.master.common.Workflow.java

@Override
public void writeExternal(ObjectOutput out) throws IOException {
    try {/*w ww .j  a  va2s  . c  o  m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStreamWriter w = new OutputStreamWriter(new DeflaterOutputStream(baos), ENCODING);
        marshaller.marshal(this, w);
        w.close();
        byte[] bytes = baos.toByteArray();
        out.writeInt(bytes.length);
        out.write(bytes);
    } catch (JAXBException e) {
        throw new IOException("failed to marshal", e);
    }
}