Example usage for java.io ObjectInput readObject

List of usage examples for java.io ObjectInput readObject

Introduction

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

Prototype

public Object readObject() throws ClassNotFoundException, IOException;

Source Link

Document

Read and return an object.

Usage

From source file:com.artistech.tuio.mouse.ZeroMqMouse.java

/**
 * Main entry point for ZeroMQ integration.
 *
 * @param args//from  w  ww  . j av a2  s. com
 * @throws AWTException
 * @throws java.io.IOException
 */
public static void main(String[] args) throws AWTException, java.io.IOException {
    //read off the TUIO port from the command line
    String zeromq_port;

    Options options = new Options();
    options.addOption("z", "zeromq-port", true, "ZeroMQ Server:Port to subscribe to. (-z localhost:5565)");
    options.addOption("h", "help", false, "Show this message.");
    HelpFormatter formatter = new HelpFormatter();

    try {
        CommandLineParser parser = new org.apache.commons.cli.BasicParser();
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("help")) {
            formatter.printHelp("tuio-mouse-driver", options);
            return;
        } else {
            if (cmd.hasOption("z") || cmd.hasOption("zeromq-port")) {
                zeromq_port = cmd.getOptionValue("z");
            } else {
                System.err.println("The zeromq-port value must be specified.");
                formatter.printHelp("tuio-mouse-driver", options);
                return;
            }
        }
    } catch (ParseException ex) {
        System.err.println("Error Processing Command Options:");
        formatter.printHelp("tuio-mouse-driver", options);
        return;
    }

    //load conversion services
    ServiceLoader<ProtoConverter> services = ServiceLoader.load(ProtoConverter.class);

    //create zeromq context
    ZMQ.Context context = ZMQ.context(1);

    // Connect our subscriber socket
    ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
    subscriber.setIdentity(ZeroMqMouse.class.getName().getBytes());

    //this could change I guess so we can get different data subscrptions.
    subscriber.subscribe("TuioCursor".getBytes());
    //        subscriber.subscribe("TuioTime".getBytes());
    subscriber.connect("tcp://" + zeromq_port);

    System.out.println("Subscribed to " + zeromq_port + " for ZeroMQ messages.");

    // Get updates, expect random Ctrl-C death
    String msg = "";
    MouseDriver md = new MouseDriver();
    while (!msg.equalsIgnoreCase("END")) {
        boolean success = false;
        byte[] recv = subscriber.recv();

        com.google.protobuf.GeneratedMessage message = null;
        TuioPoint pt = null;
        String type = recv.length > 0 ? new String(recv) : "";
        recv = subscriber.recv();
        switch (type) {
        case "TuioCursor.PROTOBUF":
            try {
                //it is a cursor?
                message = com.artistech.protobuf.TuioProtos.Cursor.parseFrom(recv);
                success = true;
            } catch (Exception ex) {
            }
            break;
        case "TuioTime.PROTOBUF":
            //                    try {
            //                        //it is a cursor?
            //                        message = com.artistech.protobuf.TuioProtos.Time.parseFrom(recv);
            //                        success = true;
            //                    } catch (Exception ex) {
            //                    }
            break;
        case "TuioObject.PROTOBUF":
            try {
                //it is a cursor?
                message = com.artistech.protobuf.TuioProtos.Object.parseFrom(recv);
                success = true;
            } catch (Exception ex) {
            }
            break;
        case "TuioBlob.PROTOBUF":
            try {
                //it is a cursor?
                message = com.artistech.protobuf.TuioProtos.Blob.parseFrom(recv);
                success = true;
            } catch (Exception ex) {
            }
            break;
        case "TuioCursor.JSON":
            try {
                //it is a cursor?
                pt = mapper.readValue(recv, TUIO.TuioCursor.class);
                success = true;
            } catch (Exception ex) {
            }
            break;
        case "TuioTime.JSON":
            //                    try {
            //                        //it is a cursor?
            //                        pt = mapper.readValue(recv, TUIO.TuioTime.class);
            //                        success = true;
            //                    } catch (Exception ex) {
            //                    }
            break;
        case "TuioObject.JSON":
            try {
                //it is a cursor?
                pt = mapper.readValue(recv, TUIO.TuioObject.class);
                success = true;
            } catch (Exception ex) {
            }
            break;
        case "TuioBlob.JSON":
            try {
                //it is a cursor?
                pt = mapper.readValue(recv, TUIO.TuioBlob.class);
                success = true;
            } catch (Exception ex) {
            }
            break;
        case "TuioTime.OBJECT":
            break;
        case "TuioCursor.OBJECT":
        case "TuioObject.OBJECT":
        case "TuioBlob.OBJECT":
            try {
                //Try reading the data as a serialized Java object:
                try (ByteArrayInputStream bis = new ByteArrayInputStream(recv)) {
                    //Try reading the data as a serialized Java object:
                    ObjectInput in = new ObjectInputStream(bis);
                    Object o = in.readObject();
                    //if it is of type Point (Cursor, Object, Blob), process:
                    if (TuioPoint.class.isAssignableFrom(o.getClass())) {
                        pt = (TuioPoint) o;
                        process(pt, md);
                    }

                    success = true;
                }
            } catch (java.io.IOException | ClassNotFoundException ex) {
            } finally {
            }
            break;
        default:
            success = false;
            break;
        }

        if (message != null && success) {
            //ok, so we have a message that is not null, so it was protobuf:
            Object o = null;

            //look for a converter that will suppor this objec type and convert:
            for (ProtoConverter converter : services) {
                if (converter.supportsConversion(message)) {
                    o = converter.convertFromProtobuf(message);
                    break;
                }
            }

            //if the type is of type Point (Cursor, Blob, Object), process:
            if (o != null && TuioPoint.class.isAssignableFrom(o.getClass())) {
                pt = (TuioPoint) o;
            }
        }

        if (pt != null) {
            process(pt, md);
        }
    }
}

From source file:Main.java

public static Object convertFromBytes(byte[] bytes) throws IOException, ClassNotFoundException {
    try {/*  w  ww .j  a v a2s.c  o m*/
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInput in = new ObjectInputStream(bis);
        return in.readObject();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.apache.hadoop.mapred.lib.instanceapi.SerializableUtil.java

@SuppressWarnings("unchecked")
public static <T> T deserializeObject(Configuration conf, String objectPropertyName) {
    try {//from w  w w. j a  v  a  2  s.  com
        String base64 = conf.get(objectPropertyName);
        byte[] raw = Base64.decodeBase64(base64.getBytes());
        ByteArrayInputStream inBytes = new ByteArrayInputStream(raw);
        ObjectInput in = new ObjectInputStream(inBytes);
        T obj = (T) in.readObject();
        if (obj instanceof Configurable) {
            ((Configurable) obj).setConf(conf);
        }
        if ((obj instanceof JobConfigurable) && (conf instanceof JobConf)) {
            ((JobConfigurable) obj).configure((JobConf) conf);
        }
        return obj;
    } catch (IOException e) {
        throw new IllegalStateException(e);
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

public static Object fromByteArray(byte[] inputBytes) {
    Object obj = null;//from   w w w  . j  a v a2s.c  om
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(inputBytes);
    ObjectInput objIn = null;
    try {
        objIn = new ObjectInputStream(byteArrayInputStream);
        obj = objIn.readObject();
    } catch (java.io.IOException ioe) {
        return null;
    } catch (java.lang.ClassNotFoundException cnfe) {
        return null;
    } finally {
        try {
            byteArrayInputStream.close();
            objIn.close();
        } catch (java.io.IOException closeIOE) {

        }
    }
    return obj;
}

From source file:Main.java

/**
 * Convert bytes array to object.//  w w  w . j a  v a  2  s  . c  o m
 *
 * @param bytes object bytes
 * @param <T> object class
 * @return object
 */
public static <T> T fromBytes(byte[] bytes) {
    Object result = null;
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    ObjectInput in = null;
    try {
        in = new ObjectInputStream(bis);
        result = in.readObject();
    } catch (IOException | ClassNotFoundException e) {
        throw new RuntimeException(e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) {
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (Exception e) {
            }
        }
    }
    return (T) result;
}

From source file:Main.java

public static Object deserialize(byte[] bytes) {
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    ObjectInput in = null;
    Object o = null;//from w  ww.  j  av a 2s . c  o  m
    try {
        in = new ObjectInputStream(bis);
        o = in.readObject();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            bis.close();
        } catch (IOException ex) {
            // ignore close exception
        }
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }
    return o;
}

From source file:org.apache.stratos.autoscaler.util.Deserializer.java

/**
 * Deserialize a byte array and retrieve the object.
 * @param bytes bytes to be deserialized
 * @return the deserialized {@link Object}
 * @throws Exception if the deserialization is failed.
 *///from www . j  av a 2  s  . c  o  m
public static Object deserializeFromByteArray(byte[] bytes) throws Exception {

    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    ObjectInput in = null;
    try {
        in = new ObjectInputStream(bis);
        Object o = in.readObject();

        return o;

    } finally {
        bis.close();
        if (in != null) {
            in.close();
        }
    }
}

From source file:net.ulno.jpunch.util.Util.java

/**
 * Deserializes Object from byte array.//from   w  w  w. ja v  a  2s  .c o  m
 * @throws IOException 
 * @throws StreamCorruptedException 
 * @throws ClassNotFoundException 
 */
public static Object deserializeObject(byte[] serializedObj)
        throws StreamCorruptedException, IOException, ClassNotFoundException {
    InputStream is = new ByteArrayInputStream(serializedObj);
    ObjectInput oi;
    oi = new ObjectInputStream(is);
    return oi.readObject();
}

From source file:com.mobicage.rogerthat.mfr.dal.Streamable.java

protected static Object fromBase64(String base64String) {
    try {//from  ww  w .ja  va2  s.co m
        ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decodeBase64(base64String));

        try {
            ZipInputStream zip = new ZipInputStream(bis);
            try {
                zip.getNextEntry();
                ObjectInput in = new ObjectInputStream(zip);
                try {
                    return in.readObject();
                } finally {
                    in.close();
                }
            } finally {
                zip.close();
            }
        } finally {
            bis.close();
        }
    } catch (ClassNotFoundException e) {
        log.log((Level.SEVERE), "ClassNotFoundException while deserializing member", e);
        return null;
    } catch (IOException e) {
        log.log((Level.SEVERE), "IOException while deserializing member", e);
        return null;
    }
}

From source file:com.abcseo.comments.dao.RepositoryTreapImpl.java

private static Object bytes2Object(byte[] bytes) {
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    Object o = null;/*  w  w  w .ja v  a2 s.c  o m*/
    try {
        ObjectInput in = new ObjectInputStream(bis);
        o = in.readObject();
    } catch (Exception e) {
        // TODO Auto-generated catch block
    }
    return o;
}