Java Serialize serializeSafe(T obj)

Here you can find the source of serializeSafe(T obj)

Description

serialize Safe

License

Open Source License

Declaration

public static <T extends Externalizable> byte[] serializeSafe(T obj)
            throws IOException, ClassNotFoundException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;

public class Main {
    public static <T extends Externalizable> byte[] serializeSafe(T obj)
            throws IOException, ClassNotFoundException {
        ByteArrayOutputStream baos = null;
        try {//from   w  ww.  j a  v a 2s  .c o  m
            baos = new ByteArrayOutputStream();

            ObjectOutput out = null;
            try {
                out = new ObjectOutputStream(baos);
                obj.writeExternal(out);
                out.flush();
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (Exception e) {
                        /* NOP */ }
                }
            }

            baos.flush();
            return baos.toByteArray();
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (Exception e) {
                    /* NOP */ }
            }
        }
    }
}

Related

  1. serializeObject(T obj)
  2. serializeObjects(File oof, Object[] objectsOut)
  3. serializeObjectToFile(Object obj, File f)
  4. serializeObjectToFile(Object object, String filename)
  5. serializer(Object object)
  6. serializeShort(short value, byte[] outbuf, int offset)
  7. serializeString(File file, String string)
  8. serializeToBase64(Serializable object)
  9. serializeToFile(Object obj, String fileName)