/** Saves the authorization database to disk. */
 public void save() throws CoreException {
   if (!needsSaving || file == null) return;
   try {
     file.delete();
     if ((!file.getParentFile().exists() && !file.getParentFile().mkdirs())
         || !canWrite(file.getParentFile()))
       throw new CoreException(
           new Status(
               IStatus.ERROR,
               Platform.PI_RUNTIME,
               Platform.FAILED_WRITE_METADATA,
               NLS.bind(Messages.meta_unableToWriteAuthorization, file),
               null));
     file.createNewFile();
     FileOutputStream out = new FileOutputStream(file);
     try {
       save(out);
     } finally {
       out.close();
     }
   } catch (IOException e) {
     throw new CoreException(
         new Status(
             IStatus.ERROR,
             Platform.PI_RUNTIME,
             Platform.FAILED_WRITE_METADATA,
             NLS.bind(Messages.meta_unableToWriteAuthorization, file),
             e));
   }
   needsSaving = false;
 }
Example #2
0
 /** A put is used to send a chunk of a file. */
 void doPut(HttpServletRequest req, HttpServletResponse resp)
     throws ServletException, IOException {
   int transferred = getTransferred();
   int length = getLength();
   int headerLength = Integer.valueOf(req.getHeader(ProtocolConstants.HEADER_CONTENT_LENGTH));
   String rangeString = req.getHeader(ProtocolConstants.HEADER_CONTENT_RANGE);
   if (rangeString == null) rangeString = "bytes 0-" + (length - 1) + '/' + length; // $NON-NLS-1$
   ContentRange range = ContentRange.parse(rangeString);
   if (length != range.getLength()) {
     fail(req, resp, "Chunk specifies an incorrect document length");
     return;
   }
   if (range.getStartByte() > transferred) {
     fail(req, resp, "Chunk missing; Expected start byte: " + transferred);
     return;
   }
   if (range.getEndByte() < range.getStartByte()) {
     fail(req, resp, "Invalid range: " + rangeString);
     return;
   }
   int chunkSize = 1 + range.getEndByte() - range.getStartByte();
   if (chunkSize != headerLength) {
     fail(req, resp, "Content-Range doesn't agree with Content-Length");
     return;
   }
   byte[] chunk = readChunk(req, chunkSize);
   FileOutputStream fout = null;
   try {
     fout = new FileOutputStream(new File(getStorageDirectory(), FILE_DATA), true);
     FileChannel channel = fout.getChannel();
     channel.position(range.getStartByte());
     channel.write(ByteBuffer.wrap(chunk));
     channel.close();
   } finally {
     try {
       if (fout != null) fout.close();
     } catch (IOException e) {
       // ignore secondary failure
     }
   }
   transferred = range.getEndByte() + 1;
   setTransferred(transferred);
   save();
   if (transferred >= length) {
     completeTransfer(req, resp);
     return;
   }
   resp.setStatus(308); // Resume Incomplete
   resp.setHeader("Range", "bytes 0-" + range.getEndByte()); // $NON-NLS-2$
   setResponseLocationHeader(req, resp);
 }
  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();
    }
  }