Java Serialize serializeGZip(T obj)

Here you can find the source of serializeGZip(T obj)

Description

serialize G Zip

License

Open Source License

Declaration

public static <T extends Externalizable> byte[] serializeGZip(T obj) 

Method Source Code


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

import java.io.*;

import java.util.zip.GZIPOutputStream;

public class Main {
    public static <T extends Externalizable> byte[] serializeGZip(T obj) {
        try {/*from  w ww  . j a  va2 s .c om*/
            return serializeGZipSafe(obj);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static <T extends Externalizable> byte[] serializeGZipSafe(T obj)
            throws IOException, ClassNotFoundException {
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();

            GZIPOutputStream gzip = null;
            try {
                gzip = new GZIPOutputStream(baos);

                ObjectOutput out = null;
                try {
                    out = new ObjectOutputStream(gzip);
                    obj.writeExternal(out);
                    out.flush();
                } finally {
                    if (out != null) {
                        try {
                            out.close();
                        } catch (Exception e) {
                            /* NOP */ }
                    }
                }

                gzip.finish();
                gzip.flush();
            } finally {
                if (gzip != null) {
                    try {
                        gzip.close();
                    } catch (Exception e) {
                        /* NOP */ }
                }
            }

            baos.flush();
            return baos.toByteArray();
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (Exception e) {
                    /* NOP */ }
            }
        }
    }
}

Related

  1. serializedCopy(Object obj)
  2. serializedCopy(T object)
  3. serializeDistributionType(String distributionType, String HadoopDistribution)
  4. serializeExecutor(Object executor)
  5. serializeFromString(final String pString)
  6. serializeInt(int i)
  7. serializeInt2MinLE(int value)
  8. serializeInt2MinLE(int value)
  9. serializeIntLE(int value, byte[] outbuf, int offset)