Example usage for java.io ObjectOutputStream close

List of usage examples for java.io ObjectOutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes the stream.

Usage

From source file:dkpro.similarity.algorithms.wikipedia.measures.WikiLinkCache.java

/**
 * Serializes the cache and saves it to the given file.
 *
 * @param file the file to save the cache to
 * @throws IOException/* w  w  w.  ja  v  a2  s  .  co  m*/
 * @throws FileNotFoundException
 * @throws IOException if the file cannot be read
 */
public void serializeObject(Object o, File file) throws FileNotFoundException, IOException {
    logger.info("Writing cache to file: " + file.getAbsolutePath());
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
    oos.writeObject(o);
    oos.flush();
    oos.close();
}

From source file:net.mindengine.oculus.frontend.web.controllers.report.ReportBrowseSaveCollectedRunsController.java

@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    Session session = Session.create(request);
    String name = request.getParameter("saveName");
    String redirect = request.getParameter("redirect");
    if (redirect == null) {
        redirect = "";
    }// ww  w  . j av a2 s . c  om
    User user = Auth.getAuthorizedUser(request);
    if (user == null)
        throw new NotAuthorizedException();

    List<TestRunSearchData> collectedTestRuns = session.getCollectedTestRuns();

    SavedRun savedRun = new SavedRun();
    savedRun.setDate(new Date());
    savedRun.setName(name);
    savedRun.setUserId(user.getId());

    Long id = testRunDAO.saveRun(savedRun);

    savedRun.setId(id);
    FileUtils.mkdirs(config.getDataFolder() + File.separator + savedRun.generateDirUrl());

    File file = new File(config.getDataFolder() + File.separator + savedRun.generateFileUrl());

    file.createNewFile();

    FileOutputStream fos = new FileOutputStream(file);
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(collectedTestRuns);
    oos.flush();
    oos.close();

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String strDate = sdf.format(savedRun.getDate());

    String convertedName = savedRun.getName().replaceAll("[^a-zA-Z0-9]", "_");

    String url = "../report/saved-" + user.getLogin() + "-" + strDate + "-" + convertedName + "-" + id;
    session.setTemporaryMessage("Your collected test runs were successfully saved."
            + " You can use them with the following url:<br/>" + "HTML: <a href=\"" + url
            + ".html\">Html version</a>" + "<br/>" + "Excel: <a href=\"" + url + ".xls\">Excel version</>");
    return new ModelAndView(new RedirectView(redirect));
}

From source file:Base64.java

/**
 * Serializes an object and returns the Base64-encoded
 * version of that serialized object. If the object
 * cannot be serialized or there is another error,
 * the method will return <tt>null</tt>.
 *
 * @param serializableObject The object to encode
 * @return The Base64-encoded object//from  ww w  .ja  v a  2 s  .  c om
 * @since 1.4
 */
public static String encodeObject(java.io.Serializable serializableObject) {

    java.io.ByteArrayOutputStream baos = null;
    java.io.OutputStream b64os = null;
    java.io.ObjectOutputStream oos = null;

    try {
        baos = new java.io.ByteArrayOutputStream();
        b64os = new OutputStream(baos, Base64.ENCODE);
        oos = new java.io.ObjectOutputStream(b64os);

        oos.writeObject(serializableObject);
    } // end try
    catch (java.io.IOException e) {

        e.printStackTrace();
        return null;
    } // end catch
    finally {
        try {
            oos.close();
            b64os.close();
            baos.close();
        } catch (Exception e) {
        }
    } // end finally

    return new String(baos.toByteArray());
}

From source file:edu.yale.cs.hadoopdb.connector.DBInputSplit.java

/**
 * Serializes DBChunk /*from   w  ww .j  a v a  2s.  co  m*/
 */
private void serializeChunk(DBChunk chunk, DataOutput out) throws IOException {
    ByteArrayOutputStream byte_stream = new ByteArrayOutputStream();
    ObjectOutputStream object_stream = new ObjectOutputStream(byte_stream);
    object_stream.writeObject(chunk);
    object_stream.close();

    byte[] buf = byte_stream.toByteArray();
    BytesWritable bw = new BytesWritable(buf);
    bw.write(out);
}

From source file:com.tempescope.wunderground.WeatherLocationManager.java

public void save() {
    if (file != null) {
        try {/* w  ww  .  j a v  a 2s.c  o  m*/
            ObjectOutputStream out = new ObjectOutputStream(
                    new BufferedOutputStream(new FileOutputStream(file)));
            out.writeObject(this);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.navercorp.pinpoint.web.filter.Base64.java

public static String encodeObject(Serializable serializableObject, int options) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputStream b64os = null;//from w ww  .ja  va  2 s.  c  o m
    ObjectOutputStream oos = null;

    String var6;
    try {
        b64os = new Base64.Base64OutputStream(baos, 1 | options);
        oos = (options & 2) == 2 ? new ObjectOutputStream(new GZIPOutputStream(b64os))
                : new ObjectOutputStream(b64os);
        oos.writeObject(serializableObject);
        String var5 = new String(baos.toByteArray(), "UTF-8");
        return var5;
    } catch (UnsupportedEncodingException var28) {
        var6 = new String(baos.toByteArray());
        return var6;
    } catch (IOException var29) {
        LOG.error("error encoding object", var29);
        var6 = null;
    } finally {
        if (oos != null) {
            try {
                oos.close();
            } catch (Exception var27) {
                LOG.error("error closing ObjectOutputStream", var27);
            }
        }

        if (b64os != null) {
            try {
                b64os.close();
            } catch (Exception var26) {
                LOG.error("error closing Base64OutputStream", var26);
            }
        }

        try {
            baos.close();
        } catch (Exception var25) {
            LOG.error("error closing ByteArrayOutputStream", var25);
        }

    }

    return var6;
}

From source file:de.scoopgmbh.copper.test.versioning.compatibility.TestJavaSerializer.java

private String serialize(final Object o) throws IOException {
    if (o == null)
        return null;
    final ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    final ObjectOutputStream oos = new ObjectOutputStream(baos);

    oos.writeObject(o);/*from  ww  w  .  j a  v  a 2  s . c  om*/
    oos.close();
    baos.close();
    byte[] data = baos.toByteArray();
    final ByteArrayOutputStream baos2 = new ByteArrayOutputStream(1024);
    baos2.write(data, 0, 8);
    baos2.write(classNameReplacement.getBytes("UTF-8"));
    int offset = classNameReplacement.length() + 8;
    baos2.write(data, offset, data.length - offset);
    baos2.flush();
    baos2.close();
    data = baos2.toByteArray();
    boolean isCompressed = false;
    if (compress && compressThresholdSize <= data.length && data.length <= compressorMaxSize) {
        data = compressorTL.get().compress(data);
        isCompressed = true;
    }
    final String encoded = new Base64().encodeToString(data);
    final StringBuilder sb = new StringBuilder(encoded.length() + 4);
    sb.append(isCompressed ? 'C' : 'U').append(encoded);
    return sb.toString();
}

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

private void saveOnCache(Context context, Mesas mesas) {
    try {//from ww w . ja va2  s  .c o m
        FileOutputStream mesasFOS = context.openFileOutput("mesas.df", Context.MODE_PRIVATE);
        ObjectOutputStream mesasOOS = new ObjectOutputStream(mesasFOS);
        mesasOOS.writeObject(mesas);
        mesasOOS.close();
        PreferenceManager.getDefaultSharedPreferences(context).edit()
                .putLong("mesas_last_cache_saved", System.currentTimeMillis()).commit();
    } catch (IOException e) {
        Log.e("tachmun", "Error opening cache file for mesas in content provider. " + "No cache will be saved",
                e);
    }
}

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

private void saveOnCache(Context context, Eventos eventos, long mesaId) {
    try {// ww w .j  a va 2s. c  om
        FileOutputStream mesasFOS = context.openFileOutput("eventos_" + mesaId + ".df", Context.MODE_PRIVATE);
        ObjectOutputStream mesasOOS = new ObjectOutputStream(mesasFOS);
        mesasOOS.writeObject(eventos);
        mesasOOS.close();
        PreferenceManager.getDefaultSharedPreferences(context).edit()
                .putLong("eventos_last_refresh", System.currentTimeMillis()).commit();
        //We just cached the events set the preference to not force the refresh again until expiration time
        PreferenceManager.getDefaultSharedPreferences(context).edit()
                .putBoolean("force_eventos_refresh_" + mesaId, false).commit();
    } catch (IOException e) {
        Log.e("tachmun", "Error opening cache file for mesas in content provider. " + "No cache will be saved",
                e);
    }
}

From source file:finale.year.stage.utility.Util.java

/**
 * User wishes to save credentials on current system directory
 * First time instance , provided he supplies his credentials
 * Only checking for one user at present
 * @param email/*w w  w  .j  a  va  2 s  .  com*/
 * @param password 
 */
public static void rememberLogin(String email, String password) {
    //If user is has checked Remember me for first time
    String response = null;
    ObjectOutputStream writer = null;
    userFile = new File("config.txt");

    //Write to File
    try {

        writer = new ObjectOutputStream(new FileOutputStream(userFile));
        writer.writeObject(email); //Write Email to File
        writer.writeObject(encryptPassword(password)); //Write Password to File
        Authentification.handleResponse(login(email, encryptPassword(password)));
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();

    } catch (IOException ex) {
        ex.printStackTrace();
    } catch (NoSuchAlgorithmException ex) {
        ex.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close(); //Close the Reader
            } catch (IOException ex) {
                ex.printStackTrace();
                return;
            }
        }
    } //End of If Block
}