Here you can find the source of serialize(Object value)
Parameter | Description |
---|---|
value | object value to serialize |
public static byte[] serialize(Object value) throws IOException
//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(); } } }