Java Object Serialize serialize(Serializable serializable)

Here you can find the source of serialize(Serializable serializable)

Description

serialize

License

Apache License

Parameter

Parameter Description
serializable is the Serializable Object to serialize.

Return

the given serializable as byte-array.

Declaration

public static byte[] serialize(Serializable serializable) 

Method Source Code

//package com.java2s;
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0 */

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

import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Main {
    /**/*from  ww  w  .  j  a  v a 2  s . c o  m*/
     * @param serializable is the {@link Serializable} {@link Object} to serialize.
     * @return the given <code>serializable</code> as byte-array.
     */
    public static byte[] serialize(Serializable serializable) {

        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(serializable);
            oos.close();
            return baos.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException("Failed to serialize!", e);
        }
    }
}

Related

  1. serialize(Serializable object)
  2. serialize(Serializable object)
  3. serialize(Serializable object, File dest)
  4. serialize(Serializable object, String fileName)
  5. serialize(Serializable s, File dir, String fileName)
  6. serialize(Serializable... objects)
  7. serialize(String filename, T obj)
  8. serialize(String path, String name, Object obj)
  9. serialize(T obj)