Example usage for java.io ObjectOutputStream writeObject

List of usage examples for java.io ObjectOutputStream writeObject

Introduction

In this page you can find the example usage for java.io ObjectOutputStream writeObject.

Prototype

public final void writeObject(Object obj) throws IOException 

Source Link

Document

Write the specified object to the ObjectOutputStream.

Usage

From source file:com.code.savemarks.utils.Utils.java

/**
 * Serialize an object into a byte array.
 * //from w  ww .  jav  a  2s .  com
 * @param obj An object to be serialized.
 * @return A byte array containing the serialized object
 * @throws QueueFailureException If an I/O error occurs during the
 * serialization process.
 */
public static byte[] serialize(Object obj) {
    try {
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        ObjectOutputStream objectOut = new ObjectOutputStream(new BufferedOutputStream(bytesOut));
        objectOut.writeObject(obj);
        objectOut.close();
        return encodeBase64(bytesOut.toByteArray());
    } catch (IOException e) {
        throw new QueueFailureException(e);
    }
}

From source file:Main.java

/**
 * Convert Object to Byte Array/*from  ww w.  ja  va 2  s . c  o m*/
 * 
 * @param obj
 * @return
 * @throws IOException
 */
public static byte[] convertObjectToByteArray(Object obj) throws IOException {
    ObjectOutputStream os = null;
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream(5000);
    os = new ObjectOutputStream(new BufferedOutputStream(byteStream));
    os.flush();
    os.writeObject(obj);
    os.flush();
    byte[] sendBuf = byteStream.toByteArray();
    os.close();
    return sendBuf;

}

From source file:org.nebulaframework.util.io.IOSupport.java

/**
 * Serializes the given object and returns a byte[] representation of
 * the serialized data./*from   ww w. j a  v a2 s.c  o  m*/
 * 
 * @param obj Object to Serialize
 * 
 * @return byte[] of serialized data
 * 
 * @throws IOException if thrown during serialization
 */
public static <T extends Serializable> byte[] serializeToBytes(T obj) throws IOException {

    Assert.notNull(obj);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(obj);

    return bos.toByteArray();
}

From source file:brooklyn.util.javalang.Serializers.java

@SuppressWarnings("unchecked")
public static <T> T reconstitute(T object, ClassLoader classLoader, final ObjectReplacer replacer)
        throws IOException, ClassNotFoundException {
    if (object == null)
        return null;

    class ReconstitutingObjectOutputStream extends ObjectOutputStream {
        public ReconstitutingObjectOutputStream(OutputStream outputStream) throws IOException {
            super(outputStream);
            enableReplaceObject(true);/*from   ww  w. j  av  a2  s.  c  om*/
        }

        @Override
        protected Object replaceObject(Object obj) throws IOException {
            return replacer.replace(obj);
        }
    }
    ;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ReconstitutingObjectOutputStream(baos);
    oos.writeObject(object);
    oos.close();

    class ReconstitutingObjectInputStream extends ClassLoaderObjectInputStream {
        public ReconstitutingObjectInputStream(InputStream inputStream, ClassLoader classLoader)
                throws IOException {
            super(inputStream, classLoader);
            super.enableResolveObject(true);
        }

        @Override
        protected Object resolveObject(Object obj) throws IOException {
            return replacer.resolve(obj);
        }
    }
    ;
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ReconstitutingObjectInputStream ois = new ReconstitutingObjectInputStream(bais, classLoader);
    try {
        return (T) ois.readObject();
    } finally {
        Streams.closeQuietly(ois);
    }
}

From source file:com.marintek.isis.wicket.popupbox.applib.PopupWicketBoxSemanticsProvider.java

static String toString(Serializable serializable) {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {/* w ww  . j  a v  a2  s .com*/
        oos = new ObjectOutputStream(baos);
        oos.writeObject(serializable);
        return new String(Base64.encodeBase64(baos.toByteArray()));
    } catch (IOException e) {
        throw new Base64Serializer.Exception(e);
    } finally {
        try {
            if (oos != null) {
                oos.close();
            }
        } catch (IOException e) {
            throw new Base64Serializer.Exception(e);
        }
    }
}

From source file:com.jaspersoft.jasperserver.core.util.StreamUtil.java

public static byte[] compress(Object object) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = new GZIPOutputStream(baos);
    ObjectOutputStream oos = new ObjectOutputStream(gzos);
    oos.writeObject(object);
    oos.flush();/*from ww  w . j  a  va  2s .  c  o  m*/
    oos.close();
    return baos.toByteArray();
}

From source file:fr.ortolang.diffusion.security.authentication.TicketHelper.java

public static String makeTicket(String username, String hash, long ticketValidity) {
    Cipher cipher;/*from www . j a v a  2  s  .  c om*/
    try {
        cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        Ticket ticket = new Ticket(username, hash, ticketValidity);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(ticket);
        MessageDigest md = MessageDigest.getInstance(MESSAGE_DIGEST_ALGORITHM);
        byte[] digest = md.digest(bos.toByteArray());
        bos.write(digest);
        byte[] encryptedBytes = cipher.doFinal(bos.toByteArray());
        return Base64.encodeBase64URLSafeString(encryptedBytes);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException
            | BadPaddingException | IllegalBlockSizeException e) {
        LOGGER.log(Level.SEVERE, "Error when making a ticket", e);
    }
    return "";
}

From source file:jedi.util.serialization.Pickle.java

/**
 * Serializes a object on the main memory and returns a sequence of bytes as a String.
 * //  w  w w  .j ava2s . c o m
 * @param o Object to be serialized.
 * @return String
 */

public static String dumps(Object o) {
    String s = "";

    try {
        // Serializing.

        // Reference to a sequence of bytes on the memory.
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(o);
        oos.close();

        // Converts a array of bytes into a String.
        // The default conversion doesn't works on the other hand Base64 works fine.
        s = new String(Base64.encodeBase64(baos.toByteArray()));

        baos.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return s;
}

From source file:Main.java

public static String objectSerializableToString(Serializable object) {
    ByteArrayOutputStream baos = null;
    ObjectOutputStream os = null;
    try {// www.jav a 2s  . c o  m
        if (object != null) {
            baos = new ByteArrayOutputStream();
            os = new ObjectOutputStream(baos);
            os.writeObject(object);
            os.flush();
            baos.flush();
            return baos.toString("UTF-8");
        }
    } catch (Throwable e) {
    } finally {
        try {
            if (os != null) {
                os.close();
            }
        } catch (Throwable e2) {
        }

        try {
            if (baos != null) {
                baos.close();
            }
        } catch (Throwable e2) {
        }
    }
    return null;
}

From source file:com.projity.exchange.LocalFileImporter.java

public static Job getExportFileJob(final FileImporter importer) {
    final Job job = new Job(importer.getJobQueue(), "exportFile", //$NON-NLS-1$
            Messages.getString("LocalFileImporter.Exporting"), true); //$NON-NLS-1$
    job.addRunnable(new JobRunnable("Export", 1.0f) { //$NON-NLS-1$
        public Object run() throws Exception {
            DataUtil serializer = new DataUtil();
            System.out.println("Serialization..."); //$NON-NLS-1$
            long t1 = System.currentTimeMillis();
            DocumentData projectData = serializer.serializeDocument(importer.getProject());
            projectData.setMaster(true);
            projectData.setLocal(true);/*from   ww  w . j  a v  a2 s  . c  o m*/
            long t2 = System.currentTimeMillis();
            System.out.println("Serialization...Done in " + (t2 - t1) + " ms"); //$NON-NLS-1$ //$NON-NLS-2$
            File f = new File(importer.getFileName());
            System.out.println("Saving " + f + "..."); //$NON-NLS-1$ //$NON-NLS-2$
            t1 = System.currentTimeMillis();
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
            out.writeObject(VERSION);
            out.writeObject(projectData);
            out.close();
            t2 = System.currentTimeMillis();
            System.out.println("Saving...Done in " + (t2 - t1) + " ms"); //$NON-NLS-1$ //$NON-NLS-2$
            setProgress(1.0f);
            return null;
        }
    });
    return job;
}