Java Object Serialize serialize(Object obj)

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

Description

serialize

License

Open Source License

Declaration

public static byte[] serialize(Object obj) 

Method Source Code


//package com.java2s;
/*//w  w w.  j av a 2s  .c  om
 * Copyright 1999-2101 Alibaba.com. All rights reserved.
 * This software is the confidential and proprietary information of Alibaba.com ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it only in accordance with the terms of
 * the license agreement you entered into with Alibaba.com.
 */

import java.io.*;

public class Main {
    public static byte[] serialize(Object obj) {
        ByteArrayOutputStream baos = null;
        ObjectOutputStream oos = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);
            return baos.toByteArray();
        } catch (RuntimeException re) {
            throw re;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            closeQuietly(baos);
            closeQuietly(oos);
        }
    }

    private static void closeQuietly(Closeable c) {
        if (c == null)
            return;
        try {
            c.close();
        } catch (IOException e) {
        }
    }
}

Related

  1. serialize(Object c, ScriptableObject scope)
  2. serialize(Object o)
  3. serialize(Object o)
  4. serialize(Object obj)
  5. serialize(Object obj)
  6. serialize(Object obj)
  7. serialize(Object obj)
  8. serialize(Object obj)
  9. serialize(Object obj)