Here you can find the source of serialize(Object obj)
Parameter | Description |
---|---|
obj | the object to serialize |
Parameter | Description |
---|---|
IOException | if an I/O error occurs. |
public static byte[] serialize(Object obj) throws IOException
//package com.java2s; /*//w ww. ja v a 2 s .co m * Copyright (c) 2000 - 2005 Nokia Corporation and/or its subsidiary(-ies). * All rights reserved. * This component and the accompanying materials are made available * under the terms of "Eclipse Public License v1.0" * which accompanies this distribution, and is available * at the URL "http://www.eclipse.org/legal/epl-v10.html". * * Initial Contributors: * Nokia Corporation - initial contribution. * * Contributors: * * Description: * */ import java.io.IOException; import java.io.OutputStream; import java.io.ObjectOutputStream; import java.io.ByteArrayOutputStream; public class Main { /** * Serializes a serializable object into a byte array. * * @param obj the object to serialize * @throws IOException if an I/O error occurs. */ public static byte[] serialize(Object obj) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); serialize(out, obj); return out.toByteArray(); } /** * Serializes a serializable object * * @param out the stream to write seralized data to * @param obj the object to serialize * @throws IOException if an I/O error occurs. */ public static void serialize(OutputStream out, Object obj) throws IOException { ObjectOutputStream serializer = new ObjectOutputStream(out); serializer.writeObject(obj); } }