Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.util.zip.Deflater;

public class Main {
    public static byte[] compress(byte[] uncompressedBuffer) {
        Deflater deflater = new Deflater();
        deflater.setInput(uncompressedBuffer);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(uncompressedBuffer.length);

        try {
            deflater.finish();
            byte[] buffer = new byte[1024];
            while (!deflater.finished()) {
                int count = deflater.deflate(buffer);
                outputStream.write(buffer, 0, count);
            }
            byte[] output = outputStream.toByteArray();
            return output;

        } finally {
            try {
                deflater.end();
                outputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}