public void saveCache(
     JMXInstrumentedCache<K, V> cache, File savedCachePath, Function<K, byte[]> converter)
     throws IOException {
   long start = System.currentTimeMillis();
   String msgSuffix = " " + savedCachePath.getName() + " for " + cfname + " of " + ksname;
   logger.debug("saving" + msgSuffix);
   int count = 0;
   File tmpFile =
       File.createTempFile(savedCachePath.getName(), null, savedCachePath.getParentFile());
   FileOutputStream fout = new FileOutputStream(tmpFile);
   ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(fout));
   FileDescriptor fd = fout.getFD();
   for (K key : cache.getKeySet()) {
     byte[] bytes = converter.apply(key);
     out.writeInt(bytes.length);
     out.write(bytes);
     ++count;
   }
   out.flush();
   fd.sync();
   out.close();
   if (!tmpFile.renameTo(savedCachePath))
     throw new IOException("Unable to rename cache to " + savedCachePath);
   if (logger.isDebugEnabled())
     logger.debug(
         "saved "
             + count
             + " keys in "
             + (System.currentTimeMillis() - start)
             + " ms from"
             + msgSuffix);
 }
Beispiel #2
0
 protected void add(Object value) {
   File f = null;
   FileOutputStream fos = null;
   try {
     f = createTempFile("S", dir);
     fos = new FileOutputStream(f);
     ObjectOutputStream fout = new ObjectOutputStream(new BufferedOutputStream(fos));
     fout.writeObject(value);
     fout.flush();
     fos.getFD().sync();
   } catch (Exception e) {
     throw new SpaceError(e);
   } finally {
     if (fos != null) {
       try {
         fos.close();
       } catch (Exception e) {
         throw new SpaceError(e);
       }
     }
   }
   stored.add(f.getAbsolutePath());
   /* fill cache */
   if (cacheSize > data.size()) if ((data.size() + 1) == stored.size()) data.add(value);
 }
  private void save(FileOutputStream os) throws IOException {
    // write the version number
    os.write(KEYRING_FILE_VERSION);

    CipherOutputStream cos = new CipherOutputStream(os, password);
    ObjectOutputStream oos = new ObjectOutputStream(cos);
    // write the data
    try {
      oos.writeObject(authorizationInfo);
      oos.writeObject(protectionSpace);
      os.flush();
      os.getFD().sync();
    } finally {
      oos.close();
    }
  }