Example usage for java.io ObjectInputStream ObjectInputStream

List of usage examples for java.io ObjectInputStream ObjectInputStream

Introduction

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

Prototype

public ObjectInputStream(InputStream in) throws IOException 

Source Link

Document

Creates an ObjectInputStream that reads from the specified InputStream.

Usage

From source file:com.acme.CodeDrivenProcessor.java

public CodeDrivenProcessor(String resource) {
    // 1. load the resource
    // 2. deserialize the lambda
    System.out.println("resource=" + resource);
    if (resource == null)
        return;/*from w w  w . j a va2  s. c o  m*/
    try {
        // InputStream is = new FileInputStream(new File("/tmp/bytes"));//
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(resource);
        if (is == null)
            return;
        System.out.println("deserializing");
        ObjectInputStream ois = new ObjectInputStream(is);
        delegate = (Processor<Tuple, Tuple>) ois.readObject();
        ois.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:com.intuit.tank.project.Script.java

@SuppressWarnings("unchecked")
public static List<ScriptStep> deserializeBlob(SerializedScriptStep serializedScriptStep) {
    List<ScriptStep> result = null;
    ObjectInputStream s = null;/*  w  w  w.  jav  a2s  .c  o m*/
    try {
        if (serializedScriptStep != null && serializedScriptStep.getSerialzedBlob() != null) {
            s = new ObjectInputStream(serializedScriptStep.getSerialzedBlob().getBinaryStream());
            result = (List<ScriptStep>) s.readObject();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(s);
    }
    return result;
}

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

public static AuthorIdentifier deserializeAuthorIdentifier(String serialization)
        throws IOException, ClassNotFoundException {
    byte b[] = serialization.getBytes();

    ByteArrayInputStream bi = new ByteArrayInputStream(b);
    ObjectInputStream si = new ObjectInputStream(bi);

    AuthorIdentifier identifier = (AuthorIdentifier) si.readObject();

    si.close();// w w  w .  j a va2s .  c  om
    bi.close();

    return identifier;
}

From source file:de.tudarmstadt.ukp.csniper.resbuild.BinaryCasReader.java

@Override
public void getNext(CAS aCAS) throws IOException, CollectionException {
    w.resume();/*from w w w .  j a  v a2 s  .c  o m*/

    Resource res = nextFile();
    InputStream is = null;
    try {
        is = res.getInputStream();
        if (res.getResource().getFilename().endsWith(".gz")) {
            is = new GZIPInputStream(is);
        }
        CASCompleteSerializer serializer = (CASCompleteSerializer) new ObjectInputStream(
                new BufferedInputStream(is)).readObject();
        ((CASImpl) aCAS).reinit(serializer);
    } catch (ClassNotFoundException e) {
        throw new IOException(e);
    } finally {
        closeQuietly(is);
    }

    w.suspend();
}

From source file:de.xwic.appkit.core.cluster.impl.ClientHandler.java

@Override
public void run() {

    try {/*from  w  w w. j a  v a  2 s.com*/

        //socket.setSoTimeout(10000); // set the timeout to 10 seconds.

        ObjectInputStream oIn = new ObjectInputStream(socket.getInputStream());
        ObjectOutputStream oOut = new ObjectOutputStream(socket.getOutputStream());

        while (true) {
            Message msgIn;
            try {
                msgIn = (Message) oIn.readObject();
            } catch (IOException e) {
                // this occurs when the connection is closed
                log.info("Client " + socket.getInetAddress() + " disconnected.");
                break;
            } catch (ClassNotFoundException e) {
                log.error("Can not handle incoming message - stream might be corrupted..", e);
                break;
            }

            Response response = null;

            //System.out.println("Received: " + msgIn.getCommand() + " ('" + msgIn.getArgument() + "')");

            // as long as the client is not identified, no protocol is selected. 
            if (protocol == null) {
                if (Message.CMD_IDENTIFY_CLIENT.equals(msgIn.getCommand())) {
                    // argument must contain client type, which causes the selection of the right protocol
                    // hard-coded here right now, but might get externalized into some configuration file
                    // later on...
                    if ("ClusterNode".equals(msgIn.getArgument())) {
                        protocol = new ClusterNodeClientProtocol(cluster);
                    } else if ("Console".equals(msgIn.getArgument())) {
                        protocol = new ConsoleClientProtocol(cluster);
                    } else {
                        log.debug("Invalid protocol selected.");
                        response = new Response(false, "Invalid protocol selected.");
                    }

                    if (protocol != null) {
                        log.info("Selected protocol " + protocol.getClass().getName());
                    }

                } else {
                    log.debug("Invalid command received - awaiting client identification..");
                }
            } else {
                response = protocol.handleMessage(socket, msgIn);
            }

            if (response == null) {
                response = new Response(true); // send success response
            }

            oOut.writeObject(response);

        }

    } catch (IOException e) {
        log.error("Communication error with client", e);
        // will cause an exit.. 
    }

    if (protocol != null) {
        protocol.onConnectionLost();
    }
    // close the socket.
    try {
        socket.close();
    } catch (IOException e) {
        log.warn("Error closing socket.", e);
    }

    // handle exiting properly (i.e. notify Cluster instance)

}

From source file:net.sf.profiler4j.console.client.Client.java

public synchronized void connect(String host, int port) throws ClientException {
    try {//from   w  ww.ja v a2  s  .c o  m
        if (isConnected()) {
            throw new ClientException("Client already connected");
        }
        s = new Socket(host, port);
        out = new ObjectOutputStream(new BufferedOutputStream(s.getOutputStream()));
        in = new ObjectInputStream(new BufferedInputStream(s.getInputStream()));
        String serverVersion = in.readUTF();
        if (!serverVersion.equals(AgentConstants.VERSION)) {
            s.close();
            s = null;
            out = null;
            in = null;
            throw new ClientException("Version of remote agent is incompatible: console is '"
                    + AgentConstants.VERSION + "' but agent is '" + serverVersion + "'");
        }
        log.info("Client connected to " + host + ":" + port);
    } catch (Exception e) {
        handleException(e);
    }
    // fireClientEvent(new ClientEvent(this, ClientEvent.CONNECTED, null));
}

From source file:com.mtgi.io.RelocatableFileTest.java

@Test
public void testEmptyFile() throws IOException, ClassNotFoundException {
    output.writeObject(inst);/*from  w  w  w  .j  a v a  2s  .  c o  m*/
    output.close();

    assertTrue("original file still here", inst.getLocalFile().isFile());
    assertEquals("source file is empty", 0, inst.getLocalFile().length());

    RelocatableFile transferred = (RelocatableFile) new ObjectInputStream(
            new ByteArrayInputStream(buffer.toByteArray())).readObject();
    assertNotNull("file loaded", transferred);
    assertNotNull("local file defined", transferred.getLocalFile());
    assertTrue("local file exists", transferred.getLocalFile().isFile());
    assertFalse("reloaded file points to a new path",
            inst.getLocalFile().getCanonicalPath().equals(transferred.getLocalFile().getCanonicalPath()));

    assertEquals("transferred file is empty", 0, transferred.getLocalFile().length());
    assertTrue("transferred file is closed", transferred.getLocalFile().delete());
}

From source file:com.ikon.util.Serializer.java

/**
 * @param obj//from w  w  w  . ja  v  a  2 s. com
 */
public static Object read(String filename) {
    FileInputStream fis = null;
    ObjectInputStream ois = null;
    Object obj = null;

    try {
        fis = new FileInputStream(Config.HOME_DIR + File.separator + filename + ".ser");
        ois = new ObjectInputStream(fis);
        obj = ois.readObject();
    } catch (FileNotFoundException e) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    } catch (ClassNotFoundException e) {
        log.error(e.getMessage());
    } finally {
        IOUtils.closeQuietly(ois);
        IOUtils.closeQuietly(fis);
    }

    return obj;
}

From source file:Models.UserModel.java

public User LogIn(String email, String password) {
    try {/*from  ww w. java2 s  . co m*/
        String auth = HMAC_SHA256(email, password);
        FileInputStream file = new FileInputStream("src/Data/users.dat");
        User user;
        try (ObjectInputStream inStream = new ObjectInputStream(file)) {
            users = (ArrayList<User>) inStream.readObject();
            for (int i = 0; i < users.size(); ++i) {
                if (users.get(i).getEmail().equals(email) && users.get(i).getAuth().equals(auth)) {
                    inStream.close();
                    return users.get(i);
                }
            }

            inStream.close();
            return null;
        }
    } catch (IOException | ClassNotFoundException e) {
        System.out.println(e.getMessage());
        JOptionPane.showMessageDialog(null, "Error read file", "ERROR", 2);
        return null;
    }
}

From source file:edu.cmu.cs.lti.ark.util.SerializedObjects.java

private static ObjectInputStream getObjectInputStream(String inputFile) throws IOException {
    return new ObjectInputStream(new BufferedInputStream(getInputStream(inputFile)));
}