Java ByteBuffer Compress compress(ByteBuffer buffer)

Here you can find the source of compress(ByteBuffer buffer)

Description

compress

License

Open Source License

Declaration

static public byte[] compress(ByteBuffer buffer) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

import java.util.zip.GZIPOutputStream;

public class Main {
    static public byte[] compress(ByteBuffer buffer) throws Exception {
        return compress(buffer.array());
    }/* w  w w  . jav a 2s.  com*/

    static public byte[] compress(byte[] bytes) throws IOException {
        return compress(bytes, 0, bytes.length);
    }

    static public byte[] compress(byte[] bytes, int fromPos, int length) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length / 4);
        GZIPOutputStream gzipOut = new GZIPOutputStream(bos);
        gzipOut.write(bytes, fromPos, length);
        gzipOut.close();
        return bos.toByteArray();
    }
}

Related

  1. compress(ByteBuffer byteBuf)
  2. decompress(ByteBuffer buffer)