Reads a serialized object from a file that has been compressed using gzip. : Serialization « File Input Output « Java






Reads a serialized object from a file that has been compressed using gzip.

      
/* Copyright (C) 2003 Univ. of Massachusetts Amherst, Computer Science Dept.
   This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit).
   http://www.cs.umass.edu/~mccallum/mallet
   This software is provided under the terms of the Common Public License,
   version 1.0, as published by http://www.opensource.org.  For further
   information, see the file `LICENSE' included with this distribution. */

//package cc.mallet.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.zip.GZIPOutputStream;

/**
 * 
 * 
 * @author <a href="mailto:casutton@cs.umass.edu">Charles Sutton</a>
 * @version $Id: ArrayUtils.java,v 1.1 2007/10/22 21:37:40 mccallum Exp $
 */
public class Util {


    /**
     * Reads a serialized object from a file that has been compressed using gzip.
     *  You probably want to use {@link #readObject} instead, because it will automatically guess
     *  from the extension whether the file is compressed, and call this method if necessary.
     * @param f Compressed file to read object from
     * @see #readObject
     */
    public static Object readGzippedObject (File f)
    {
      try {
        ObjectInputStream ois = new ObjectInputStream (new BufferedInputStream (new GZIPInputStream (new FileInputStream(f))));
        Object obj = ois.readObject();
        ois.close ();
        return obj;
      }
      catch (IOException e) {
        throw new RuntimeException (e);
      } catch (ClassNotFoundException e) {
        throw new RuntimeException (e);
      }
    }

}

   
    
    
    
    
    
  








Related examples in the same category

1.Serialization with ObjectInputStream and ObjectOutputStream
2.Object SerializationObject Serialization
3.Serial Demo
4.Create a serialized output file
5.Reconstructing an externalizable objectReconstructing an externalizable object
6.Simple use of Externalizable and a pitfallSimple use of Externalizable and a pitfall
7.Serializable
8.Serializer class
9.This program shows how to use getSerialVersionUID
10.Working with Serialization
11.Assists with the serialization process and performs additional functionality based on serialization.
12.Computes all the class serialVersionUIDs under the jboss home directory.
13.Serializable Enumeration
14.Serialization Utilities
15.This program demonstrates the transfer of serialized objects between virtual machines
16.A class whose clone method uses serialization
17.Writes a serialized version of obj to a given file, compressing it using gzip.
18.Serialized File Util
19.Serializes an object to a file, masking out annoying exceptions.