Java Serialize serializeObject(final Object object)

Here you can find the source of serializeObject(final Object object)

Description

serialize Object

License

Open Source License

Declaration

public static byte[] serializeObject(final Object object) 

Method Source Code

//package com.java2s;
/*/*from w ww.ja v  a  2  s.  c  om*/
 * SwingTech Software - http://cooksarm.sourceforge.net/
 *
 * Copyright (C) 2011 Joe Rice All rights reserved.
 *
 * SwingTech Cooks Arm 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 3 of the License, or (at your option) any
 * later version.
 *
 * SwingTech Cooks Arm 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
 * SwingTech Cooks Arm; If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.ByteArrayOutputStream;

import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

public class Main {
    public static byte[] serializeObject(final Object object) {
        ObjectOutput out = null;
        byte[] buf = null;

        if (object == null) {
            return null;
        }

        try {
            // Serialize to a byte array
            final ByteArrayOutputStream bos = new ByteArrayOutputStream();
            out = new ObjectOutputStream(bos);
            out.writeObject(object);
            out.close();

            // Get the bytes of the serialized object
            buf = bos.toByteArray();
        } catch (final Exception e) {
            throw new RuntimeException("Error trying to serializeObject the following object:  " + object, e);
        }

        return buf;
    }
}

Related

  1. serializeInt2MinLE(int value)
  2. serializeIntLE(int value, byte[] outbuf, int offset)
  3. serializeJdk(Object obj)
  4. serializeMock(Object mock)
  5. serializeObject(B b)
  6. serializeObject(String absFilePath, Object obj)
  7. serializeObject(String path, Object e)
  8. serializeObject(T obj)
  9. serializeObjects(File oof, Object[] objectsOut)