Example usage for java.io ObjectInputStream close

List of usage examples for java.io ObjectInputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the input stream.

Usage

From source file:net.sf.nmedit.jpatch.ModuleDescriptions.java

public void readCache(File cache, ClassLoader loader)
        throws FileNotFoundException, IOException, ClassNotFoundException {
    ObjectInputStream in = new PluginObjectInputStream(loader,
            new BufferedInputStream(new FileInputStream(cache)));
    try {/*www .  jav a 2  s. c o  m*/
        readCache(in);
    } finally {
        in.close();
    }
}

From source file:com.abelsky.idea.geekandpoke.entries.impl.OfflineCacheImpl.java

/**
 * Tries to read persisted entry from the {@code file}.
 *
 * @return De-serialized object or {@code null} on any error.
 *///from w  ww  . j  a va  2  s . c  o  m
private @Nullable Entry readFromFile(@NotNull File file) {
    if (log.isDebugEnabled()) {
        log.debug("Reading entry from \"" + file + "\"");
    }

    try {
        @NotNull
        final ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
        try {
            return (Entry) in.readObject();
        } finally {
            in.close();
        }
    } catch (ClassNotFoundException e) {
        log.error(e);
    } catch (IOException e) {
        if (log.isDebugEnabled()) {
            log.debug(e);
        }
    }
    return null;
}

From source file:de.javakaffee.web.msm.serializer.jackson.JacksonTranscoderTest.java

private StandardSession javaRoundtrip(final StandardSession session,
        final MemcachedBackupSessionManager manager) throws IOException, ClassNotFoundException {

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(bos);
    session.writeObjectData(oos);/*from w w w .  j  av a 2s  . c  o m*/
    oos.close();
    bos.close();

    final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    final ObjectInputStream ois = new ObjectInputStream(bis);
    final StandardSession readSession = manager.createEmptySession();
    readSession.readObjectData(ois);
    ois.close();
    bis.close();

    return readSession;
}

From source file:io.seldon.api.state.ZkAlgorithmUpdater.java

@Override
public void run() {
    logger.info("Starting");
    try {// w ww  .  j  av  a 2 s. c om
        boolean error = false;
        while (keepRunning) {
            try {

                CuratorFramework client = null;
                boolean ok = false;
                for (int attempts = 0; attempts < 4 && !ok; attempts++) {
                    client = curatorHandler.getCurator().usingNamespace(clientName);
                    logger.info("Waiting until zookeeper connected on attempt " + attempts);
                    ok = client.getZookeeperClient().blockUntilConnectedOrTimedOut();
                    if (ok)
                        logger.info("zookeeper connected");
                    else {
                        logger.error("Timed out waiting for zookeeper connect : attempt " + attempts);
                    }
                }
                if (!ok) {
                    logger.error("Failed to connect to zookeeper after multiple attempts - STOPPING");
                    return;
                }
                queue = new LinkedBlockingQueue<>();
                final Watcher watcher = new Watcher() {
                    boolean expired = false;

                    @Override
                    public void process(WatchedEvent event) {
                        try {
                            if (event.getPath() != null)
                                queue.put(event.getPath());
                            else {
                                logger.warn("Unexpected event " + event.getType().name() + " -> "
                                        + event.toString());
                                switch (event.getState()) {
                                case SyncConnected: {
                                }
                                    break;
                                case Expired: {
                                    queue.put("");
                                }
                                    break;
                                case Disconnected: {
                                    logger.warn("Disconnected from server");
                                    //queue.put("");                                      
                                }
                                    break;
                                }

                            }
                        } catch (InterruptedException e) {
                            throw new Error(e);
                        }
                    }
                };

                logger.info("Checking path " + ALG_PATH + " exists");
                if (client.checkExists().forPath(ALG_PATH) == null) {
                    logger.warn(
                            "Path " + ALG_PATH + " does not exist for client " + clientName + " creating...");
                    client.create().forPath(ALG_PATH);
                } else
                    logger.info("Path " + ALG_PATH + " exists");

                //client.getConnectionStateListenable().addListener(stateListener);
                boolean restart = false;
                while (keepRunning && !restart) {

                    client.getData().usingWatcher(watcher).forPath(ALG_PATH);

                    String path = queue.take();
                    if (!StringUtils.isEmpty(path)) {
                        logger.info("Alg Path changed " + path);

                        byte[] bytes = client.getData().forPath(ALG_PATH);

                        try {
                            ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
                            CFAlgorithm alg = (CFAlgorithm) in.readObject();
                            in.close();
                            //Update Algorithm for client
                            logger.info(
                                    "Updating algorithm options for " + clientName + " to " + alg.toString());
                            Util.getAlgorithmService().setAlgorithmOptions(clientName, alg);
                            numUpdates++;
                        } catch (ClassNotFoundException e) {
                            logger.error("Can't find class ", e);
                        } catch (IOException e) {
                            logger.error("Failed to deserialize algorithm for client " + clientName, e);
                        }
                    } else {
                        //logger.warn("Empty path - maybe zookeeper connection state change watcher will be reset");
                        logger.warn("Will try to restart");
                        restart = true;
                    }
                }

            } catch (IOException e) {
                logger.error("Exception trying to create sk client ", e);
                error = true;
            } catch (Exception e) {
                logger.error("Exception from zookeeper client ", e);
                error = true;
            } finally {

            }

            if (keepRunning && error) {
                logger.info("Sleeping " + sleepTime);
                Thread.sleep(sleepTime);
                if (sleepTime * 2 < maxSleepTime)
                    sleepTime = sleepTime * 2;
                error = false;
            }

        }
    } catch (InterruptedException e) {
        logger.warn("Sleep interuppted ", e);
    }
    logger.info("Stopping");
}

From source file:com.all.dht.util.DhtFileUtils.java

public Object readObjectFromFile(String filePath) throws IOException, ClassNotFoundException {
    ObjectInputStream in = new ObjectInputStream(
            new BufferedInputStream(new SecureInputStream(new FileInputStream(filePath))));
    Object object = in.readObject();
    in.close();
    return object;
}

From source file:org.obiba.onyx.magma.ScriptedVariableValueSourceFactory.java

public Set<VariableValueSource> createSources() {
    if (resource.exists() == false) {
        return ImmutableSet.of();
    }//from   w  w w.j ava 2 s. c  om

    try {
        ImmutableSet.Builder<VariableValueSource> sources = new ImmutableSet.Builder<VariableValueSource>();
        ObjectInputStream ois = getXStream().createObjectInputStream(resource.getInputStream());
        try {
            while (true) {
                Variable variable = (Variable) ois.readObject();
                sources.add(new JavascriptVariableValueSource(variable));
            }
        } catch (EOFException e) {
            // Reached the end of the file.
        } finally {
            ois.close();
        }
        return sources.build();
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:de.jwic.base.SessionManager.java

/**
 * Reactivate the session./*  w ww  .  jav a  2 s. com*/
 * @param container
 */
public void deserialize(SessionContainer container) {

    log.debug("Deserializing container " + container);

    try {
        String filename = container.getClientId() + "_" + container.getId() + ".ser";
        File file = new File(serDir, filename);
        if (file.exists()) {
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);

            SessionContext sc = (SessionContext) ois.readObject();
            ois.close();
            sc.fireEvent(new SessionEvent(null), SessionContext.AFTER_DESERIALIZATION);

            container.setSessionContext(sc);
            container.setState(SessionContainer.STATE_NORMAL);

            fis.close();
            if (!file.delete()) { // remove the serialized file.
                log.warn("Can't delete serialization file " + file.getName());
            }

        } else {
            throw new JWicException("The session can not be desrialized because the data can not be found.");
        }

    } catch (Exception e) {
        log.error("Error deserializing session", e);
        throw new JWicException("Deserialization failed. Can not deserialize session.", e);
    }

}

From source file:com.taobao.metamorphosis.utils.codec.impl.JavaDeserializer.java

/**
 * @see com.taobao.notify.codec.Deserializer#decodeObject(byte[])
 *//*  w  w w .j  a  va2s .  c  o  m*/
@Override
public Object decodeObject(final byte[] objContent) throws IOException {
    Object obj = null;
    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;
    try {
        bais = new ByteArrayInputStream(objContent);
        ois = new ObjectInputStream(bais);
        obj = ois.readObject();
    } catch (final IOException ex) {
        throw ex;
    } catch (final ClassNotFoundException ex) {
        this.logger.warn("Failed to decode object.", ex);
    } finally {
        if (ois != null) {
            try {
                ois.close();
                bais.close();
            } catch (final IOException ex) {
                this.logger.error("Failed to close stream.", ex);
            }
        }
    }
    return obj;
}

From source file:edu.stanford.muse.email.JarDocCache.java

@Override
public synchronized Document getHeader(String prefix, int msgNum) throws IOException, ClassNotFoundException {
    URL u = new URL("jar:file:///" + baseDir + File.separator + prefix + ".headers!/" + msgNum + ".header");
    ObjectInputStream ois = new ObjectInputStream(u.openStream());
    Document ed = (Document) ois.readObject();
    ois.close();
    return ed;/*from   w ww  .ja v  a 2  s  .  c o  m*/
}