Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

import java.util.zip.GZIPInputStream;

public class Main {
    /**
     * Degzips the compressed array and places the results into the decompressed array.
     *
     * @param compressed The compressed array.
     * @param decompressed The decompressed array.
     * @throws IOException If an I/O error occurs.
     */
    public static void degzip(byte[] compressed, byte[] decompressed) throws IOException {
        try (DataInputStream is = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(compressed)))) {
            is.readFully(decompressed);
        }
    }

    /**
     * Degzips <strong>all</strong> of the datain the specified {@link ByteBuffer}.
     *
     * @param compressed The compressed buffer.
     * @return The decompressed array.
     * @throws IOException If there is an error decompressing the buffer.
     */
    public static byte[] degzip(ByteBuffer compressed) throws IOException {
        try (InputStream is = new GZIPInputStream(new ByteArrayInputStream(compressed.array()));
                ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            byte[] buffer = new byte[1024];

            while (true) {
                int read = is.read(buffer, 0, buffer.length);
                if (read == -1) {
                    break;
                }

                out.write(buffer, 0, read);
            }

            return out.toByteArray();
        }
    }
}