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:com.spoledge.audao.db.dao.RootDaoImpl.java

protected final <T> T deserialize(byte[] bytes, Class<T> clazz) {
    if (bytes == null)
        return null;

    try {/*  w w  w .j av  a2  s  .c  om*/
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bis);

        T ret = clazz.cast(ois.readObject());
        ois.close();

        return ret;
    } catch (Exception e) {
        throw new DBException(e);
    }
}

From source file:net.sf.jasperreports.engine.util.JRLoader.java

/**
 *
 *//*www. j a  v a 2  s . c  om*/
public static Object loadObject(JasperReportsContext jasperReportsContext, URL url) throws JRException {
    Object obj = null;

    InputStream is = null;
    ObjectInputStream ois = null;

    try {
        is = url.openStream();
        ois = new ContextClassLoaderObjectInputStream(jasperReportsContext, is);
        obj = ois.readObject();
    } catch (IOException e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_OBJECT_FROM_URL_LOADING_ERROR, new Object[] { url }, e);
    } catch (ClassNotFoundException e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_CLASS_NOT_FOUND_FROM_URL, new Object[] { url }, e);
    } finally {
        if (ois != null) {
            try {
                ois.close();
            } catch (IOException e) {
            }
        }

        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }

    return obj;
}

From source file:ru.org.linux.auth.UserDetailsServiceImpl.java

private Profile readProfile(String username) {
    Storage storage = new FileStorage(configuration.getPathPrefix() + "linux-storage/");
    InputStream df = null;/*ww  w  .j a  va2s  .c o m*/
    Map<String, Object> userProfile = null;
    try {
        df = storage.getReadStream("profile", username);
        ObjectInputStream dof = null;
        try {
            dof = new ObjectInputStream(df);
            userProfile = (Map<String, Object>) dof.readObject();
            dof.close();
            df.close();
        } catch (IOException e) {
            logger.info("Bad profile for user " + username);
        } finally {
            if (dof != null) {
                try {
                    dof.close();
                } catch (IOException e) {
                    logger.info("Bad profile for user " + username);
                }
            }
        }
    } catch (StorageException e) {
        logger.info("Bad profile for user " + username);
    } catch (ClassNotFoundException e) {
        logger.info("Bad profile for user " + username);
    } finally {
        if (df != null) {
            try {
                df.close();
            } catch (IOException e) {
                logger.info("Bad profile for user " + username);
            }
        }
    }
    ProfileProperties properties;
    if (userProfile != null) {
        properties = new ProfileProperties(
                new ProfileHashtable(DefaultProfile.getDefaultProfile(), userProfile));
    } else {
        properties = new ProfileProperties(
                new ProfileHashtable(DefaultProfile.getDefaultProfile(), new HashMap<String, Object>()));
    }
    return new Profile(properties, false);
}

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.4/*from  w  w  w  .  ja v a2s  .co m*/
 */
public static Object decodeToObject(String encodedObject) {

    byte[] objBytes = decode(encodedObject);

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

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

        return ois.readObject();
    } // end try
    catch (java.io.IOException e) {

        e.printStackTrace();
        return null;
    } // end catch
    catch (ClassNotFoundException e) {

        e.printStackTrace();
        return null;
    } // end catch
    finally {
        try {
            bais.close();
            ois.close();
        } catch (Exception e) {
        }
    } // end finally
}

From source file:de.ingrid.interfaces.csw.admin.IndexScheduler.java

private void loadPatternFile() {
    LOG.debug("try to load pattern from file");
    try {//from www.j ava  2  s.c  o  m
        final ObjectInputStream reader = new ObjectInputStream(new FileInputStream(_patternFile));
        _pattern = (String) reader.readObject();
        reader.close();
    } catch (final Exception e) {
        LOG.error(e);
    }
}

From source file:com.nextcloud.android.sso.InputStreamBinder.java

private <T extends Serializable> T deserializeObjectAndCloseStream(InputStream is)
        throws IOException, ClassNotFoundException {
    ObjectInputStream ois = new ObjectInputStream(is);
    T result = (T) ois.readObject();//from w  ww. ja v  a2 s  .com
    is.close();
    ois.close();
    return result;
}

From source file:org.blanco.techmun.android.cproviders.MensajesFetcher.java

private List<Mensaje> tryToLoadFromCache(Context context, boolean forceCacheLoad) {
    Long lastCache = PreferenceManager.getDefaultSharedPreferences(context).getLong("mensajes_last_cache", 1);
    long diff = System.currentTimeMillis() - lastCache;
    if (diff < 180000 && !forceCacheLoad) {
        //if the cache of the mensajes is greater than 3 minutes launch a new refresh
        return null;
    }/* w ww  .j  ava 2 s  .  c  om*/
    try {
        FileInputStream mesasFIS = context.openFileInput("mensajes.df");
        ObjectInputStream mesasOIS = new ObjectInputStream(mesasFIS);
        List<Mensaje> result = (List<Mensaje>) mesasOIS.readObject();
        mesasOIS.close();
        return result;
    } catch (IOException e) {
        Log.e("tachmun",
                "Error opening cache file for mesas in content provider. " + "No cache will be restored", e);
        return null;
    } catch (ClassNotFoundException e) {
        Log.e("tachmun", "Error in cache file for mesas in content provider. "
                + "Something is there but is not a Mesas object. Cache no restored", e);
        return null;
    }
}

From source file:net.sf.ehcache.ElementTest.java

License:asdf

/**
 * Tests the deserialization performance of an element containing a large byte[]
 *
 * @throws IOException/* w  ww . j  a va  2 s.  co  m*/
 * @throws ClassNotFoundException
 */
public void testDeserializationPerformance() throws IOException, ClassNotFoundException {

    byte[] value = getTestByteArray();
    Element element = new Element("test", value);

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bout);
    oos.writeObject(element);
    byte[] serializedValue = bout.toByteArray();
    oos.close();
    StopWatch stopWatch = new StopWatch();
    for (int i = 0; i < 100; i++) {
        ByteArrayInputStream bin = new ByteArrayInputStream(serializedValue);
        ObjectInputStream ois = new ObjectInputStream(bin);
        ois.readObject();
        ois.close();
    }
    long elapsed = stopWatch.getElapsedTime() / 100;
    LOG.info("In-memory size in bytes: " + serializedValue.length + " time to deserialize in ms: " + elapsed);
    assertTrue(elapsed < 30);
}

From source file:org.hoteia.qalingo.app.business.job.email.AbstractEmailItemProcessor.java

public Email process(CommonProcessIndicatorItemWrapper<Long, Email> wrapper) throws Exception {
    Email email = wrapper.getItem();/*from w  ww  .  j a v  a 2 s  .com*/
    Blob emailcontent = email.getEmailContent();

    InputStream is = emailcontent.getBinaryStream();
    ObjectInputStream oip = new ObjectInputStream(is);
    Object object = oip.readObject();

    MimeMessagePreparatorImpl mimeMessagePreparator = (MimeMessagePreparatorImpl) object;

    oip.close();
    is.close();

    try {
        // SANITY CHECK
        if (email.getStatus().equals(Email.EMAIl_STATUS_PENDING)) {

            if (mimeMessagePreparator.isMirroringActivated()) {
                String filePathToSave = mimeMessagePreparator.getMirroringFilePath();
                File file = new File(filePathToSave);

                // SANITY CHECK : create folders
                String absoluteFolderPath = file.getParent();
                File absolutePathFile = new File(absoluteFolderPath);
                if (!absolutePathFile.exists()) {
                    absolutePathFile.mkdirs();
                }

                if (!file.exists()) {
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,
                            Constants.UTF8);
                    Writer out = new BufferedWriter(outputStreamWriter);
                    if (StringUtils.isNotEmpty(mimeMessagePreparator.getHtmlContent())) {
                        out.write(mimeMessagePreparator.getHtmlContent());
                    } else {
                        out.write(mimeMessagePreparator.getPlainTextContent());
                    }

                    try {
                        if (out != null) {
                            out.close();
                        }
                    } catch (IOException e) {
                        logger.debug("Cannot close the file", e);
                    }
                } else {
                    logger.debug("File already exists : " + filePathToSave);
                }
            }

            mailSender.send(mimeMessagePreparator);
            email.setStatus(Email.EMAIl_STATUS_SENDED);
        } else {
            logger.warn("Batch try to send email was already sended!");
        }

    } catch (Exception e) {
        logger.error("Fail to send email! Exception is save in database, id:" + email.getId());
        email.setStatus(Email.EMAIl_STATUS_ERROR);
        emailService.handleEmailException(email, e);
    }

    return email;
}

From source file:de.tudarmstadt.ukp.dkpro.wsd.si.dictionary.UkbDocumentDependentDictionaryInventory.java

public UkbDocumentDependentDictionaryInventory(String inputPath, String serializiblePath,
        String neededMentionsPath) throws FileNotFoundException, IOException {
    // Open the serialized version or create it from scratch
    try {//from  w ww  .  j av  a2 s .c o m
        // System.out.println("Trying to load dictionary from serializable.");
        ObjectInputStream dictionaryReader = new ObjectInputStream(
                new BZip2CompressorInputStream(new FileInputStream(serializiblePath)));
        dictionary = (UkbDictionary) dictionaryReader.readObject();
        dictionaryReader.close();
        // System.out.println("Loaded dictionary from serializable.");
    } catch (Exception e) {
        // System.out.println("Trying to load dictionary from input.");
        dictionary = new UkbDictionary(inputPath, neededMentionsPath);
        System.out.println("Loaded dictionary from input.");

        ObjectOutputStream dictionaryWriter = new ObjectOutputStream(
                new BZip2CompressorOutputStream(new FileOutputStream(serializiblePath)));
        dictionaryWriter.writeObject(dictionary);
        dictionaryWriter.close();
        // System.out.println("Stored dictionary in serializable.");
    }

}