Java Serializable Serialize serializeObject(Serializable ser)

Here you can find the source of serializeObject(Serializable ser)

Description

Serialize an object into a byte array

License

Open Source License

Parameter

Parameter Description
ser a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static byte[] serializeObject(Serializable ser) throws Exception 

Method Source Code

//package com.java2s;
/** ***************************************************************
Util.java/*from   w w  w .j  av  a2  s.c o m*/
Copyright (C) 2001  Brendon Upson 
http://www.wnc.net.au info@wnc.net.au
    
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
    
This program 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 General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
    
 *************************************************************** */

import java.io.ByteArrayOutputStream;

import java.io.ObjectOutputStream;

import java.io.Serializable;

public class Main {
    /**
     * Serialize an object into a byte array
     * @param ser
     * @return
     * @throws Exception
     */
    public static byte[] serializeObject(Serializable ser) throws Exception {
        if (ser == null)
            return null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(ser);
        byte[] retval = baos.toByteArray();
        return retval;
    }
}

Related

  1. serializeObject(File inFile, Serializable inObject)
  2. serializeObject(Serializable cds)
  3. serializeObject(Serializable o)
  4. serializeObject(Serializable obj, String filename)
  5. serializeObject(Serializable serializable)
  6. serializeObjectToFile(File targetFile, Serializable serializable)