Java Object to Byte Array serializeToBytes(Serializable object)

Here you can find the source of serializeToBytes(Serializable object)

Description

Takes serializable object and serializes it to the byte array

License

Open Source License

Parameter

Parameter Description
object object to serialize

Exception

Parameter Description
InvalidClassException Something is wrong with a class used by serialization.
NotSerializableException Some object to be serialized does not implement the java.io.Serializable interface.
IOException Any exception thrown by the underlying OutputStream.

Return

the byte array representing serializable object

Declaration

public static byte[] serializeToBytes(Serializable object) throws IOException 

Method Source Code

//package com.java2s;
/**/*w  w w .  j  ava2 s . co  m*/
 * JBoss, Home of Professional Open Source
 * Copyright 2011, Red Hat, Inc. and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This 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 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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 this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.io.Serializable;

public class Main {
    /**
     * Takes serializable object and serializes it to the byte array
     *
     * @param object object to serialize
     * @return the byte array representing serializable object
     *
     * @throws InvalidClassException Something is wrong with a class used by serialization.
     * @throws NotSerializableException Some object to be serialized does not implement the java.io.Serializable interface.
     * @throws IOException Any exception thrown by the underlying OutputStream.
     */
    public static byte[] serializeToBytes(Serializable object) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(object);
        return baos.toByteArray();
    }
}

Related

  1. serializeToByteArray(Object object)
  2. serializeToByteArray(Serializable object)
  3. serializeToByteArray(Serializable value)
  4. serializeToByteArrayNoHeader(Serializable o)
  5. serializeToBytes(Serializable object)
  6. serializeToBytes(T input)