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:mainGUI.TrainingJFrame.java

private NeuralNet loadNetwork(String filename) {
    NeuralNet net = null;/*w  w w. j  a va 2 s  .c  om*/
    try {
        FileInputStream door = new FileInputStream(filename);
        ObjectInputStream reader = new ObjectInputStream(door);
        net = (NeuralNet) reader.readObject();
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return net;
}

From source file:Base64.java

/**
 * Attempts to decode Base64 data and deserialize a Java
 * Object within. Returns <tt>null</tt> if there was an error.
 *
 * @param encodedObject The Base64 data to decode
 * @return The decoded and deserialized object
 * @since 1.5//from   w ww.java2s .c o m
 */
public static Object decodeToObject(String encodedObject) {
    // Decode and gunzip if necessary
    byte[] objBytes = decode(encodedObject);

    java.io.ByteArrayInputStream bais = null;
    java.io.ObjectInputStream ois = null;
    Object obj = null;

    try {
        bais = new java.io.ByteArrayInputStream(objBytes);
        ois = new java.io.ObjectInputStream(bais);

        obj = ois.readObject();
    } // end try
    catch (java.io.IOException e) {
        e.printStackTrace();
        obj = null;
    } // end catch
    catch (java.lang.ClassNotFoundException e) {
        e.printStackTrace();
        obj = null;
    } // end catch
    finally {
        try {
            bais.close();
        } catch (Exception e) {
        }
        try {
            ois.close();
        } catch (Exception e) {
        }
    } // end finally

    return obj;
}

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

@Override
public void run() {

    try {//from  w  w w  . j  a  va2  s .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.zacwolf.commons.crypto._CRYPTOfactory.java

public final Object decryptObjFromInputStream(final InputStream is)
        throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException,
        NoSuchPaddingException, IOException, ClassNotFoundException {
    ready();//from w w w.  j a  v  a  2s .  c  o  m
    activecrypts++;
    try {
        final int saltsize = is.read();
        final byte[] salt;
        if (saltsize > 0) {
            salt = new byte[saltsize];
            is.read(salt);
        } else
            salt = null;
        ObjectInputStream ois = null;
        try {
            ois = is instanceof ObjectInputStream ? (ObjectInputStream) is
                    : new ObjectInputStream(new CipherInputStream(is, crypter.getDcipher(salt)));
            return ois.readObject();
        } finally {
            if (ois != null) {
                ois.close();
                ois = null;
            }
        }
    } finally {
        activecrypts--;
    }
}

From source file:com.remobile.file.AssetFilesystem.java

private void lazyInitCaches() {
    synchronized (listCacheLock) {
        if (listCache == null) {
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(assetManager.open("cdvasset.manifest"));
                listCache = (Map<String, String[]>) ois.readObject();
                lengthCache = (Map<String, Long>) ois.readObject();
                listCacheFromFile = true;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();//from   w  w  w.  j a  v  a2s.c  o m
            } catch (IOException e) {
                // Asset manifest won't exist if the gradle hook isn't set up correctly.
            } finally {
                if (ois != null) {
                    try {
                        ois.close();
                    } catch (IOException e) {
                    }
                }
            }
            if (listCache == null) {
                Log.w("AssetFilesystem",
                        "Asset manifest not found. Recursive copies and directory listing will be slow.");
                listCache = new HashMap<String, String[]>();
            }
        }
    }
}

From source file:edu.usf.cutr.siri.util.SiriUtils.java

/**
 * Read the given object from Android internal storage for this app
 * /*from  ww w. j  a  va  2  s  .  c o  m*/
 * @param objectType
 *            object type, defined by class constant Strings, to retrieve
 *            from cache (ObjectReader, ObjectMapper, or XmlReader)
 * 
 * @return deserialized Object, or null if object couldn't be deserialized
 */
public static synchronized Serializable readFromCache(String objectType) {

    FileInputStream fileStream = null;
    ObjectInputStream objectStream = null;

    // Holds object to be read from cache
    Serializable object = null;

    try {
        String fileName = objectType + CACHE_FILE_EXTENSION;

        File file = new File(fileName);

        cacheReadStartTime = System.nanoTime();
        fileStream = new FileInputStream(file);
        objectStream = new ObjectInputStream(fileStream);
        object = (Serializable) objectStream.readObject();
        cacheReadEndTime = System.nanoTime();

        // Get size of serialized object
        long fileSize = file.length();

        System.out.println("Read " + fileName + " from cache (" + fileSize + " bytes) in "
                + df.format(getLastCacheReadTime() / 1000000.0) + " ms.");
    } catch (FileNotFoundException e) {
        System.out
                .println("Cache miss - Jackson object '" + objectType + "' does not exist in app cache: " + e);
        return null;
    } catch (Exception e) {
        // Reset timestamps to show there was an error
        cacheReadStartTime = 0;
        cacheReadEndTime = 0;
        System.out.println("Couldn't read Jackson object '" + objectType + "' from cache: " + e);
    } finally {
        try {
            if (objectStream != null) {
                objectStream.close();
            }
            if (fileStream != null) {
                fileStream.close();
            }
        } catch (Exception e) {
            System.out.println("Error closing cache file connections: " + e);
        }
    }

    return object;
}

From source file:architecture.common.util.TextUtils.java

/**
 * Decode Object from a String by decoding with base64 then deserializing.
 * //from  ww w  . j  a  v  a2 s. c  o  m
 * @see #encodeObject(java.lang.Object)
 */
public final static Object decodeObject(String str) throws IOException, ClassNotFoundException {
    ByteArrayInputStream bytes = new ByteArrayInputStream(Base64.decodeBase64(str));
    ObjectInputStream stream = new ObjectInputStream(bytes);
    Object result = stream.readObject();
    stream.close();

    return result;
}

From source file:fr.dyade.aaa.util.MySqlDBRepository.java

/**
 * Loads the object./*from  ww  w  .j  a  v  a  2 s  .co  m*/
 *
 * @return The loaded object or null if it does not exist.
 * @throws ClassNotFoundException 
 */
public Object loadobj(String dirName, String name) throws IOException, ClassNotFoundException {
    if (logger.isLoggable(BasicLevel.DEBUG))
        logger.log(BasicLevel.DEBUG, "loadobj, b4 load call");

    byte[] content = load(dirName, name);

    if (logger.isLoggable(BasicLevel.DEBUG))
        logger.log(BasicLevel.DEBUG, "loadobj, after load call");

    ByteArrayInputStream bis = new ByteArrayInputStream(content);
    ObjectInputStream ois = new ObjectInputStream(bis);
    try {
        Object obj = ois.readObject();
        return obj;
    } catch (Exception e) {
        String exceptionString = e.toString();
        if (exceptionString.indexOf("KNOWN PROBLEM") == -1) {
            e.printStackTrace();
        }
        throw new IOException(e.getMessage());
    } finally {
        ois.close();
        bis.close();
    }
}

From source file:agilejson.Base64.java

/**
 * Attempts to decode Base64 data and deserialize a Java Object within.
 * Returns <tt>null</tt> if there was an error.
 * //w w  w .ja  va 2  s .  c om
 * @param encodedObject The Base64 data to decode
 * @return The decoded and deserialized object
 * @since 1.5
 */
public static Object decodeToObject(String encodedObject) {
    // Decode and gunzip if necessary
    byte[] objBytes = decode(encodedObject);

    Object obj = null;
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(new ByteArrayInputStream(objBytes));
        obj = ois.readObject();

    } catch (IOException e) {
        LOG.error("error decoding object", e);

    } catch (ClassNotFoundException e) {
        LOG.error("error decoding object", e);

    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (Exception e) {
                LOG.error("error closing ObjectInputStream", e);
            }
        }
    } // end finally

    return obj;
}

From source file:info.donsun.cache.filecache.FileCache.java

@Override
public Object get(Object key) throws CacheException {
    synchronized (LOCK) {
        // //  w w  w  .j a  v a  2s . c  om
        if (exists(key)) {
            // ??
            for (String fileName : listFileNames()) {
                if (isCacheFile(key, fileName)) {
                    try {
                        FileInputStream fileStream = new FileInputStream(
                                FileUtils.getFile(config.getDir(), fileName));
                        ObjectInputStream in = new ObjectInputStream(fileStream);
                        boolean isExpire = false;
                        try {
                            CachedObject cachedObject = (CachedObject) in.readObject();
                            // 
                            if (cachedObject.getExpire() > 0
                                    && cachedObject.getExpire() < System.currentTimeMillis()) {
                                isExpire = true;
                                return null;
                            }
                            return cachedObject.getData();
                        } finally {
                            in.close();
                            fileStream.close();
                            if (isExpire) {
                                remove(key);
                            }
                        }
                    } catch (Exception e) {
                        throw new CacheException("Get cache file fail.", e);
                    }
                }
            }
        }

        return null;
    }
}