Example usage for java.io ObjectOutputStream ObjectOutputStream

List of usage examples for java.io ObjectOutputStream ObjectOutputStream

Introduction

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

Prototype

public ObjectOutputStream(OutputStream out) throws IOException 

Source Link

Document

Creates an ObjectOutputStream that writes to the specified OutputStream.

Usage

From source file:Main.java

static byte[] toBlob(Serializable object) throws IOException {
    ObjectOutputStream oos = null;
    ByteArrayOutputStream bos;//  w w w.  j av  a  2s .  co m
    byte[] result = null;
    try {
        bos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(bos);
        oos.writeObject(object);
        result = bos.toByteArray();
    } finally {
        if (oos != null) {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:com.yfiton.oauth.OAuthUtils.java

public static void writeAuthorizationInfo(Path file, AuthorizationData data) throws IOException {
    ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(file));
    oos.writeObject(data);/*w  w w.j  a  v a2s .c  o m*/
    oos.close();
}

From source file:Main.java

public static void writeGZipCompressedObject(Object o, OutputStream out) throws IOException {
    GZIPOutputStream gos = new GZIPOutputStream(out);
    ObjectOutputStream oos = new ObjectOutputStream(gos);
    oos.writeObject(o);/*from ww  w . java  2  s  .c o  m*/
    oos.flush();
    gos.finish();
}

From source file:de.kbs.acavis.service.SerializationHelper.java

public static String serializePublicationIdentifier(PublicationIdentifier identifier) throws IOException {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    ObjectOutputStream so = new ObjectOutputStream(bo);

    so.writeObject(identifier);/*from  w w w.  j  ava  2  s  .  c om*/
    so.flush();

    String serialization = bo.toString();

    so.close();
    bo.close();

    return serialization;
}

From source file:Main.java

public static byte[] toBinary(Serializable obj) {
    if (obj == null) {
        return null;
    }/* www .  ja  v a  2s . co  m*/
    if (obj instanceof byte[]) {
        return (byte[]) obj;
    }
    try {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        try {
            return baos.toByteArray();
        } finally {
            oos.close();
        }
    } catch (Exception e) {
        String msg = "Failed to convert the object to binary: obj=" + obj;
        throw new IllegalStateException(msg, e);
    }
}

From source file:Main.java

public static boolean saveObjectToFile(String filePath, Object object) {
    if (!TextUtils.isEmpty(filePath)) {
        File cacheFile = null;/*from   www . ja va  2 s .c  o  m*/

        try {
            cacheFile = new File(filePath);
            if (cacheFile.exists()) {
                cacheFile.delete();
            }

            if (!cacheFile.getParentFile().exists()) {
                cacheFile.getParentFile().mkdirs();
            }

            cacheFile.createNewFile();
        } catch (Throwable var6) {
            var6.printStackTrace();
            cacheFile = null;
        }

        if (cacheFile != null) {
            try {
                FileOutputStream t = new FileOutputStream(cacheFile);
                GZIPOutputStream gzos = new GZIPOutputStream(t);
                ObjectOutputStream oos = new ObjectOutputStream(gzos);
                oos.writeObject(object);
                oos.flush();
                oos.close();
                return true;
            } catch (Throwable var7) {
                var7.printStackTrace();
            }
        }
    }

    return false;
}

From source file:Util.java

/**
 * Writes a serialized version of obj to a given file, compressing it using gzip.
 * @param f File to write to// ww  w.j  a  va 2  s.c  o m
 * @param obj Object to serialize
 */
public static void writeGzippedObject(File f, Serializable obj) {
    try {
        ObjectOutputStream oos = new ObjectOutputStream(
                new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(f))));
        oos.writeObject(obj);
        oos.close();
    } catch (IOException e) {
        System.err.println("Exception writing file " + f + ": " + e);
    }
}

From source file:Main.java

/**
 * Serialize any object into String//from   w  w  w.j ava  2 s .  c o  m
 * @param object Object
 * @return String
 * @throws IOException If unable to access Stream
 */
public static String serialize(Object object) throws java.io.IOException {
    if (object == null)
        return null;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(
            new Base64OutputStream(byteArrayOutputStream, 0));
    objectOutputStream.writeObject(object);
    objectOutputStream.flush();
    objectOutputStream.close();
    return byteArrayOutputStream.toString();
}

From source file:com.pcms.core.util.ObjectUtil.java

public static byte[] serialize(Object object) {
    ObjectOutputStream oos = null;
    ByteArrayOutputStream baos = null;
    try {/*from  w w  w. java2  s .  c  o m*/
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(object);
        byte[] bytes = baos.toByteArray();
        return bytes;
    } catch (Exception e) {
        LOG.error(e.getMessage());
    }
    return null;
}

From source file:Main.java

private static void writePageNameHashesToFile(Map<UUID, String> map, File folder) {
    if (uuidsToNames != null) {
        String mapString = "Map: ";
        for (Map.Entry<UUID, String> entry : uuidsToNames.entrySet()) {
            mapString += entry.toString();
            mapString += "\n";
        }// w  w w. j a v  a  2 s  . c  o  m
    }

    if (map == null) { // TODO: Do I need this?
        map = new HashMap<UUID, String>();
    }

    try {
        FileOutputStream fos = new FileOutputStream(folder.getAbsolutePath() + fileName);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(map);
        oos.close();
    } catch (Exception e) {
        Log.e("Write to file", e.toString());
    }
}