Java OutputStream Write serialize(Serializable obj, OutputStream outputStream)

Here you can find the source of serialize(Serializable obj, OutputStream outputStream)

Description

Serializes an Object to the specified stream.

The stream will be closed once the object is written.

License

Apache License

Parameter

Parameter Description
obj the object to serialize to bytes, may be null
outputStream the stream to write to, must not be null

Exception

Parameter Description
IllegalArgumentException if <code>outputStream</code> is <code>null</code>
SerializationException (runtime) if the serialization fails

Declaration

public static void serialize(Serializable obj, OutputStream outputStream) 

Method Source Code


//package com.java2s;
/*//from ww w .  j a  va2s . c  o m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.*;

public class Main {
    /**
     * <p>Serializes an <code>Object</code> to the specified stream.</p>
     *
     * <p>The stream will be closed once the object is written.
     * This avoids the need for a finally clause, and maybe also exception
     * handling, in the application code.</p>
     * 
     * <p>The stream passed in is not buffered internally within this method.
     * This is the responsibility of your application if desired.</p>
     *
     * @param obj  the object to serialize to bytes, may be null
     * @param outputStream  the stream to write to, must not be null
     * @throws IllegalArgumentException if <code>outputStream</code> is <code>null</code>
     * @throws SerializationException (runtime) if the serialization fails
     */
    public static void serialize(Serializable obj, OutputStream outputStream) {
        if (outputStream == null) {
            throw new IllegalArgumentException("The OutputStream must not be null");
        }
        ObjectOutputStream out = null;
        try {
            // stream closed in the finally
            out = new ObjectOutputStream(outputStream);
            out.writeObject(obj);

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException ex) {
                // ignore close exception
            }
        }
    }

    /**
     * <p>Serializes an <code>Object</code> to a byte array for
     * storage/serialization.</p>
     *
     * @param obj  the object to serialize to bytes
     * @return a byte[] with the converted Serializable
     * @throws SerializationException (runtime) if the serialization fails
     */
    public static byte[] serialize(Serializable obj) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
        serialize(obj, baos);
        return baos.toByteArray();
    }
}

Related

  1. serialize(Object value, OutputStream targetOutputStream)
  2. serialize(OutputStream os, String... params)
  3. serialize(OutputStream out, Object obj)
  4. serialize(Serializable obj, ByteArrayOutputStream bout)
  5. serialize(Serializable obj, OutputStream outputStream)
  6. serialize(Serializable s, OutputStream os)
  7. serializeInt(int i, DataOutputStream dout)
  8. serializeInt(OutputStream out, Integer data)
  9. serializeNull(DataOutputStream dout)