Java Serialize serializeToString(Object o)

Here you can find the source of serializeToString(Object o)

Description

serialize To String

License

Open Source License

Declaration

public static String serializeToString(Object o) throws Exception 

Method Source Code

//package com.java2s;
/*/*from w w w .  j ava 2s .c o  m*/
 * Copyright 2001-2008 Aqris Software AS. All rights reserved.
 * 
 * This program is dual-licensed under both the Common Development
 * and Distribution License ("CDDL") and the GNU General Public
 * License ("GPL"). You may elect to use one or the other of these
 * licenses.
 */

import java.io.ByteArrayOutputStream;

import java.io.ObjectOutputStream;

public class Main {
    public static String serializeToString(Object o) throws Exception {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        ObjectOutputStream stream = new ObjectOutputStream(bytes);
        stream.writeObject(o);
        stream.flush();
        return bytesToString(bytes.toByteArray());
    }

    private static String bytesToString(byte[] bytes) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            result.append(bytes[i]);
            result.append(" ");
        }
        return result.toString();
    }
}

Related

  1. serializeString(File file, String string)
  2. serializeToBase64(Serializable object)
  3. serializeToFile(Object obj, String fileName)
  4. serializeToFile(Object object, File file)
  5. serializeToFile(Serializable s, String path)
  6. serializeToSurefireDirectory(final Class testClass, final Object object)
  7. serializeUnserialize(Object ob)