Description
Given a serializable object, this will return the object's serialized byte array representation.
License
Open Source License
Parameter
Parameter | Description |
---|
object | the object to serialize |
Exception
Parameter | Description |
---|
RuntimeException | if failed to serialize the object |
Return
the serialized bytes
Declaration
public static byte[] serialize(Serializable object)
throws RuntimeException
Method Source Code
//package com.java2s;
/*//w w w . j a v a 2s. c o m
* RHQ Management Platform
* Copyright (C) 2005-2012 Red Hat, Inc.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation, and/or the GNU Lesser
* General Public License, version 2.1, also as published by the Free
* Software Foundation.
*
* 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 and the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU Lesser 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.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Main {
/**
* Given a serializable object, this will return the object's serialized byte array representation.
*
* @param object the object to serialize
*
* @return the serialized bytes
*
* @throws RuntimeException if failed to serialize the object
*/
public static byte[] serialize(Serializable object)
throws RuntimeException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(byteStream);
oos.writeObject(object);
oos.close();
} catch (IOException ioe) {
throw new RuntimeException("Failed to serialize object", ioe);
}
return byteStream.toByteArray();
}
}
Related
- serialize(Serializable obj)
- serialize(Serializable obj)
- serialize(Serializable obj)
- serialize(Serializable obj, String fileName)
- serialize(Serializable obj, String path)
- serialize(Serializable object)
- serialize(Serializable object)
- serialize(Serializable object)
- serialize(Serializable object, File dest)