Java Object Save writeObject(Object object)

Here you can find the source of writeObject(Object object)

Description

Serialize an Object to a ByteArray

License

Open Source License

Return

a byte[] array

Declaration

public static byte[] writeObject(Object object) throws IOException 

Method Source Code

//package com.java2s;
/*// w  w w .  ja  v  a2  s. c om
 * Copyright (c) 2008-2016, GigaSpaces Technologies, Inc. All Rights Reserved.
 *
 * 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 {
    /**
     * Serialize an Object to a ByteArray
     *
     * @return a byte[] array
     */
    public static byte[] writeObject(Object object) throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(buffer);
        out.writeObject(object);
        out.close();
        return buffer.toByteArray();
    }

    /**
     * Write an Object to an OutputStream
     */
    public static void writeObject(OutputStream out, Object obj) throws IOException {
        ObjectOutputStream objOut = new ObjectOutputStream(out);
        objOut.writeObject(obj);
        objOut.flush();
    }
}

Related

  1. writeObject(Object o, File f)
  2. writeObject(Object o, File path)
  3. writeObject(Object o, ObjectOutput dos)
  4. writeObject(Object obj, File path)
  5. writeObject(Object obj, ObjectOutput out)
  6. writeObject(Object object, File destinationFile)
  7. writeObject(Object object, String path)
  8. writeObject(Object p_object, File p_outFile)
  9. writeObjectToFile(File file, Object o)