Java Object Serialize serialize(Object value)

Here you can find the source of serialize(Object value)

Description

Serializes the given object value to a newly allocated byte array.

License

Apache License

Parameter

Parameter Description
value object value to serialize

Declaration

public static byte[] serialize(Object value) throws IOException 

Method Source Code

//package com.java2s;
/*// w w  w. jav a 2s.  c o m
 * Copyright (c) 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class Main {
    /**
     * Serializes the given object value to a newly allocated byte array.
     *
     * @param value object value to serialize
     * @since 1.16
     */
    public static byte[] serialize(Object value) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        serialize(value, out);
        return out.toByteArray();
    }

    /**
     * Serializes the given object value to an output stream, and close the output stream.
     *
     * @param value object value to serialize
     * @param outputStream output stream to serialize into
     * @since 1.16
     */
    public static void serialize(Object value, OutputStream outputStream) throws IOException {
        try {
            new ObjectOutputStream(outputStream).writeObject(value);
        } finally {
            outputStream.close();
        }
    }
}

Related

  1. serialize(Object object, File file)
  2. serialize(Object object, String fileName)
  3. serialize(Object object, String path)
  4. Serialize(Object serializableObject, String filePath)
  5. serialize(Object state)
  6. serialize(Object value)
  7. serialize(S value)
  8. serialize(Serializable jobSpec)
  9. serialize(Serializable o)