Example usage for java.io ObjectInputStream readUnshared

List of usage examples for java.io ObjectInputStream readUnshared

Introduction

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

Prototype

public Object readUnshared() throws IOException, ClassNotFoundException 

Source Link

Document

Reads an "unshared" object from the ObjectInputStream.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    String s = "Hello World from java2s.com";

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeUnshared(s);/*from   w w  w.  j  a  v a 2 s  . com*/
    oout.flush();
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read and print the unshared object
    System.out.println(ois.readUnshared());
    ois.close();
}

From source file:de.dal33t.powerfolder.util.ByteSerializer.java

/**
 * Deserializer method with flag indicating if the base array is zip
 * compressed/* w  ww .j  a  va2 s.  c o  m*/
 *
 * @param base
 * @param compressed
 * @return the dezerialized object
 * @throws IOException
 * @throws ClassNotFoundException
 */
private static Object deserialize0(byte[] base, boolean compressed) throws IOException, ClassNotFoundException {
    long start = System.currentTimeMillis();
    ObjectInputStream in = null;
    Object result = null;
    try {
        InputStream targetIn;
        // deserialize from the array.......u
        ByteArrayInputStream bin = new ByteArrayInputStream(base);
        if (compressed) {
            GZIPInputStream zipIn = new GZIPInputStream(bin);
            targetIn = zipIn;
        } else {
            targetIn = bin;
        }
        in = new ObjectInputStream(targetIn);
        result = in.readUnshared();
        return result;
    } finally {
        if (in != null) {
            in.close();
        }
        if (BENCHMARK && result != null) {
            totalObjects++;
            totalTime += System.currentTimeMillis() - start;

            int count = 0;
            if (CLASS_STATS.containsKey(result.getClass())) {
                count = CLASS_STATS.get(result.getClass());
            }
            count++;
            CLASS_STATS.put(result.getClass(), count);
        }
    }
}

From source file:majordodo.worker.TaskModeAwareExecutorFactory.java

@Override
public TaskExecutor createTaskExecutor(String taskType, Map<String, Object> parameters) {
    String mode = (String) parameters.getOrDefault("mode", Task.MODE_DEFAULT);
    if (mode.equals(Task.MODE_EXECUTE_FACTORY)) {
        return inner.createTaskExecutor(taskType, parameters);
    }// www.j a  va 2s  .c om
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    try {
        String parameter = (String) parameters.getOrDefault("parameter", "");
        if (parameter.startsWith("base64:")) {
            parameter = parameter.substring("base64:".length());
            byte[] serializedObjectData = Base64.getDecoder().decode(parameter);
            ByteArrayInputStream ii = new ByteArrayInputStream(serializedObjectData);
            ObjectInputStream is = new ObjectInputStream(ii) {
                @Override
                public Class resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
                    try {
                        return tccl.loadClass(desc.getName());
                    } catch (Exception e) {
                    }
                    return super.resolveClass(desc);
                }

            };
            TaskExecutor res = (TaskExecutor) is.readUnshared();
            return res;
        } else if (parameter.startsWith("newinstance:")) {
            parameter = parameter.substring("newinstance:".length());
            URLClassLoader cc = (URLClassLoader) tccl;
            Class clazz = Class.forName(parameter, true, tccl);
            TaskExecutor res = (TaskExecutor) clazz.newInstance();
            return res;
        } else {
            throw new RuntimeException("bad parameter: " + parameter);
        }
    } catch (Exception err) {
        return new TaskExecutor() {
            @Override
            public String executeTask(Map<String, Object> parameters) throws Exception {
                throw err;
            }

        };
    }

}