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:glluch.com.ontotaxoseeker.TestsGen.java

/**
 * Deserialize an object//from  w  w w. j  a  v  a2s  . c o  m
 * @param filename The file name where the object was serialized.
 * @return An object. Most likely a posterior cast will be needed.
 * @throws FileNotFoundException if the ser file was not found
 * @throws IOException ?
 * @throws ClassNotFoundException Very unlikely to appear. The object class is always there.
 */
public static Object load(String filename) throws FileNotFoundException, IOException, ClassNotFoundException {

    FileInputStream fileIn = new FileInputStream(filename);
    ObjectInputStream in = new ObjectInputStream(fileIn);
    Object o = in.readObject();

    return o;
}

From source file:br.com.i9torpedos.model.service.serverSocket.DSSocketSIM1.java

@Override
public void run() {

    try {//from w  ww .  ja v  a  2s .co m
        while (true) {
            Socket socket = server.accept();
            log.info("Cliente conectado  " + socket.getInetAddress());

            try {

                PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
                printWriter.println(conectou);
                log.info("Sinal de autenticao enviado para o Cliente  " + new Date().toString());

                //Faz verificao no fluxo de entrada do Objeto
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());

                try {

                    //faz a leitura do Objeto de entrada
                    Object readObject = objectInputStream.readObject();

                    if (readObject instanceof SendSMSMessage) {

                        try {

                            SendSMSMessage smsMessage = (SendSMSMessage) readObject;

                            // Thread.sleep(random.nextInt(10000));

                            ponte.set(smsMessage);
                            Thread t = new Thread(new ConsumerSendSMSMessage(ponte),
                                    "PONTE_ASYNC_DSSOCKETSIM1");
                            t.start();

                            listaSMS.add(smsMessage);

                            objectInputStream.close();
                            socket.close();

                            if (listaSMS.size() > 0 && !listaSMS.isEmpty()) {

                                DServiceModem1 md1 = new DServiceModem1(listaSMS, 1000);
                                new Thread(md1, "MODEM 1").start();
                                listaSMS.clear();

                            }

                        } catch (InterruptedException ex) {
                            Logger.getLogger(DSSocketSIM1.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }

                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(DSSocketSIM1.class.getName()).log(Level.SEVERE, null, ex);
                }

            } catch (IOException ex) {
                Logger.getLogger(DSSocketSIM1.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    socket.close();
                } catch (IOException ex) {
                    Logger.getLogger(DSSocketSIM1.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(DSSocketSIM1.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            server.close();
        } catch (IOException ex) {
            Logger.getLogger(DSSocketSIM1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

From source file:natalia.dymnikova.cluster.scheduler.impl.Codec.java

public Serializable unpack(final InputStream inputStream) {
    try {//  w ww.j a  v  a2  s  .c  om
        try (final ObjectInputStream in = new ObjectInputStream(inputStream)) {
            return (Serializable) in.readObject();
        }
    } catch (final IOException e) {
        throw propagateUnchecked(e);
    } catch (final ClassNotFoundException e) {
        throw unchecked(e);
    }
}

From source file:br.com.i9torpedos.model.service.serverSocket.DSSocketSIM2.java

@Override
public void run() {

    try {//  www .  j  a  v a  2s  .  co m
        while (true) {
            Socket socket = server.accept();
            log.info("Cliente conectado  " + socket.getInetAddress());

            try {

                PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
                printWriter.println(new Date().toString() + "Teste Servidor");
                log.info("Sinal de autenticao enviado para o Cliente  " + new Date().toString());

                //Faz verificao no fluxo de entrada do Objeto
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());

                try {

                    //faz a leitura do Objeto de entrada
                    Object readObject = objectInputStream.readObject();

                    if (readObject instanceof SendSMSMessage) {

                        try {
                            SendSMSMessage smsMessage = (SendSMSMessage) readObject;

                            // Thread.sleep(random.nextInt(10000));

                            ponte.set(smsMessage);
                            new Thread(new ConsumerSendSMSMessage(ponte), "PONTE_ASYNC_DSSOCKETSIM2").start();
                            listaSMS.add(smsMessage);

                            objectInputStream.close();
                            socket.close();

                            if (listaSMS.size() > 0 && !listaSMS.isEmpty()) {

                                DServiceModem2 md2 = new DServiceModem2(listaSMS, 10000);
                                new Thread(md2, "MODEM 2").start();
                                listaSMS.clear();

                            }

                        } catch (InterruptedException ex) {
                            Logger.getLogger(DSSocketSIM2.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }

                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(DSSocketSIM2.class.getName()).log(Level.SEVERE, null, ex);
                }

            } catch (IOException ex) {
                Logger.getLogger(DSSocketSIM2.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    socket.close();
                } catch (IOException ex) {
                    Logger.getLogger(DSSocketSIM2.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(DSSocketSIM2.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            server.close();
        } catch (IOException ex) {
            Logger.getLogger(DSSocketSIM2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

From source file:com.icantrap.collections.dawg.Dawg.java

/**
 * Factory method.  Creates a new Dawg entry by reading in data from the given InputStream.  Once the data is read,
 * the stream remains open./*from w w  w  .j a  va 2  s  . c  om*/
 *
 * @param is the stream with the data to create the Dawg instance.
 * @return a new Dawg instance with the data loaded
 * @throws DataFormatException if the InputStream doesn't contain the proper data format for loading a Dawg instance
 * @throws IOException if reading from the stream casues an IOException.
 */
public static Dawg load(InputStream is) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(is, 8 * 1024);
    ObjectInputStream ois = new ObjectInputStream(bis);

    int[] ints;

    try {
        ints = (int[]) ois.readObject();
    } catch (ClassNotFoundException cnfe) {
        throw new DataFormatException("Bad file.  Not valid for loading com.icantrap.collections.dawg.Dawg",
                cnfe);
    }

    return new Dawg(ints);
}

From source file:com.dii.ids.application.utils.io.SimpleDiskCache.java

private Map<String, Serializable> readMetadata(DiskLruCache.Snapshot snapshot) throws IOException {
    ObjectInputStream ois = null;
    try {//from w  ww  .j a va 2s .  com
        ois = new ObjectInputStream(new BufferedInputStream(snapshot.getInputStream(METADATA_IDX)));
        @SuppressWarnings("unchecked")
        Map<String, Serializable> annotations = (Map<String, Serializable>) ois.readObject();
        return annotations;
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(ois);
    }
}

From source file:org.eclipse.thym.core.internal.util.BundleHttpCacheStorage.java

@Override
public HttpCacheEntry getEntry(String key) throws IOException {
    File f = getCacheFile(key);/*from  ww w  .  ja  va2s . c o  m*/
    if (f.exists()) {
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        HttpCacheEntry entry = null;

        try {
            byte[] bytes = FileUtils.readFileToByteArray(f);
            bais = new ByteArrayInputStream(bytes);
            ois = new ObjectInputStream(bais);
            entry = (HttpCacheEntry) ois.readObject();
        } catch (ClassNotFoundException e) {
            HybridCore.log(IStatus.ERROR, "Missing bundle", e);
        } finally {
            if (ois != null)
                ois.close();
            if (bais != null)
                bais.close();
        }
        return entry;
    }
    return null;
}

From source file:de.fu_berlin.inf.dpp.videosharing.net.ConnectionFactory.java

/**
 * // ww  w.  j a  v a2  s  .c o m
 * @param session
 * @param mode
 * @throws IOException
 */
public ConnectionFactory(StreamSession session, Mode mode) throws IOException {
    this.mode = mode;
    this.session = session;

    try {
        switch (mode) {
        case CLIENT:
            clientVideoIn = session.getInputStream(0);

            clientStatisticsOut = new ObjectOutputStream(session.getOutputStream(0));
            clientStatisticsOut.flush();
            clientActivitiesOut = new ObjectOutputStream(session.getOutputStream(1));
            clientActivitiesOut.flush();
            clientErrorIn = new ObjectInputStream(session.getInputStream(1));

            break;
        case HOST:
            hostVideoOut = session.getOutputStream(0);

            hostStatisticsIn = new ObjectInputStream(session.getInputStream(0));
            hostActivitiesIn = new ObjectInputStream(session.getInputStream(1));
            hostErrorOut = new ObjectOutputStream(session.getOutputStream(1));
            hostErrorOut.flush();
            break;
        default:
            break;
        }
    } catch (IOException e) {
        session.dispose();
        throw e;
    }

}

From source file:com.scoredev.scores.HighScore.java

public void setHighScore(final int score) throws IOException {
    //check permission first
    SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        sm.checkPermission(new HighScorePermission(gameName));
    }/*from w w w  . ja v a2s  .co m*/

    // need a doPrivileged block to manipulate the file
    try {
        AccessController.doPrivileged(new PrivilegedExceptionAction() {
            public Object run() throws IOException {
                Hashtable scores = null;
                // try to open the existing file. Should have a locking
                // protocol (could use File.createNewFile).
                try {
                    FileInputStream fis = new FileInputStream(highScoreFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    scores = (Hashtable) ois.readObject();
                } catch (Exception e) {
                    // ignore, try and create new file
                }

                // if scores is null, create a new hashtable
                if (scores == null)
                    scores = new Hashtable(13);

                // update the score and save out the new high score
                scores.put(gameName, new Integer(score));
                FileOutputStream fos = new FileOutputStream(highScoreFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(scores);
                oos.close();
                return null;
            }
        });
    } catch (PrivilegedActionException pae) {
        throw (IOException) pae.getException();
    }
}

From source file:RedisCache.java

/** Read the object from Base64 string. */
private static Object fromString(String s) throws IOException, ClassNotFoundException {
    byte[] data = Base64.decodeBase64(s);
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
    Object o = ois.readObject();/*from  w  w w .ja va2 s  . c  o m*/
    ois.close();
    return o;
}