예제 #1
0
  /**
   * Stores the cookies currently saved in memory, to the record store.
   *
   * @param cookiesStorage the name of the record store to hold the cookies.
   * @throws IOException
   */
  public void storeCookies(String cookiesStorage) throws IOException {
    if (savedCookies.size() == 0) {
      Log.logInfo("No cookies to store in recordstore.");
      return; // nothing to save.
    }
    try {
      try {
        connector.rmsDelete(cookiesStorage);
      } catch (Throwable e) { // try to delete last save cookies.
        Log.logWarn("Failed to delete old saved cookies file. " + e.getMessage());
      }

      ByteArrayOutputStream bout = new ByteArrayOutputStream();
      DataOutputStream dout = new DataOutputStream(bout);
      for (int i = 0; i < savedCookies.size(); ++i) {
        Cookie c = (Cookie) savedCookies.elementAt(i);
        Cookie.serialize(c, dout);
      }
      byte[] buf = bout.toByteArray();
      int free = connector.rmsFree();
      if (free < buf.length)
        throw new IOException("Not enough free storage space to save cookies.");
      connector.rmsWrite(cookiesStorage, buf);
    } catch (Throwable e) {
      if (e instanceof IOException) throw (IOException) e;
      else throw new IOException("Store cookies failed. " + e.getMessage());
    }
    Log.logInfo("Stored " + savedCookies.size() + " to record store: " + cookiesStorage);
  }