Java XML Base64 Encode Decode decompressFromBase64(String message)

Here you can find the source of decompressFromBase64(String message)

Description

decompress From Base

License

Open Source License

Declaration

public static byte[] decompressFromBase64(String message) throws IOException 

Method Source Code


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

import javax.xml.bind.DatatypeConverter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

public class Main {
    public static byte[] decompressFromBase64(String message) throws IOException {
        byte[] compressed = DatatypeConverter.parseBase64Binary(message);

        if (!isGzipped(compressed)) {
            return null;
        }//from  ww w  .j a  v a2  s.c o m

        return decompress(compressed);
    }

    public static boolean isGzipped(byte[] bytes) {
        int head = ((int) bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
        return (GZIPInputStream.GZIP_MAGIC == head);
    }

    public static byte[] decompress(byte[] message) throws IOException {
        GZIPInputStream gzipIn = new GZIPInputStream(new ByteArrayInputStream(message));
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int count;
        while ((count = gzipIn.read(buffer)) != -1) {
            outputStream.write(buffer, 0, count);
        }

        gzipIn.close();

        return outputStream.toByteArray();
    }
}

Related

  1. decodeBase64(File file)
  2. decodeBase64(String b64data)
  3. decodeBase64(String base64)
  4. decodeBase64(String s)
  5. decodeBase64(String value)
  6. encodeBase64(byte[] b)
  7. encodeBase64(byte[] bytes)
  8. encodeBASE64(byte[] data)
  9. encodeBase64(String input)