Java Object Serialize serialize(Object object, boolean zipped)

Here you can find the source of serialize(Object object, boolean zipped)

Description

Serialize one object.

License

Open Source License

Parameter

Parameter Description
object to serialize
zipped or not

Exception

Parameter Description
IOException the iO exception

Return

the serialized object

Declaration

public static byte[] serialize(Object object, boolean zipped) throws IOException 

Method Source Code

//package com.java2s;
/*//from ww  w .  j a  v  a2  s. com
 * Copyright (c) 2005-2016 Vincent Vandenschrick. All rights reserved.
 *
 *  This file is part of the Jspresso framework.
 *
 *  Jspresso is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Jspresso is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Jspresso.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.io.ObjectOutputStream;

import java.util.zip.GZIPOutputStream;

public class Main {
    /**
     * Serialize one object.
     *
     * @param object to serialize
     * @param zipped or not
     * @return the serialized object
     * @throws IOException the iO exception
     */
    public static byte[] serialize(Object object, boolean zipped) throws IOException {
        ByteArrayOutputStream bo = new ByteArrayOutputStream();

        ObjectOutputStream oo;
        if (zipped) {
            oo = new ObjectOutputStream(new GZIPOutputStream(bo));
        } else {
            oo = new ObjectOutputStream(bo);
        }

        oo.writeObject(object);
        oo.close();
        return bo.toByteArray();
    }
}

Related

  1. serialize(Object object)
  2. serialize(Object object)
  3. serialize(Object object)
  4. serialize(Object object)
  5. serialize(Object object)
  6. serialize(Object object, File file)
  7. serialize(Object object, File file)
  8. serialize(Object object, String fileName)
  9. serialize(Object object, String path)