コード例 #1
0
 public static FileSaver create(File directory, String name, long size) throws Throwable {
   String name2 = name;
   for (int ii = 1; ; ++ii, name2 = name + '-' + ii) {
     File resultFile = new File(directory, name2);
     if (resultFile.exists()) {
       continue;
     }
     File partialFile = new File(directory, String.format(PARTIAL_FORMAT, name2, size));
     if (partialFile.exists()) {
       continue;
     }
     boolean success = false;
     FileSaver fileSaver = new FileSaver(partialFile, resultFile, size);
     try {
       if (0 >= size) {
         fileSaver.file = new RandomAccessFile(resultFile, "rw");
         fileSaver.file.setLength(0);
         fileSaver.file.close();
         fileSaver.file = null;
       }
       success = true;
       return fileSaver;
     } finally {
       if (!success) {
         fileSaver.close();
       }
     }
   }
 }
コード例 #2
0
 public static FileSaver continuePartialFile(File partialFile) throws Throwable {
   if ((!partialFile.isFile()) || (PARTIAL_EXTRA_DATA > partialFile.length())) {
     return null;
   }
   Matcher matcher = PARTIAL_PATTERN.matcher(partialFile.getName());
   if (!matcher.matches()) {
     return null;
   }
   File resultFile = new File(partialFile.getParentFile(), matcher.group(1));
   if (resultFile.exists()) {
     return null;
   }
   long size;
   try {
     size = Long.parseLong(matcher.group(2), 16);
   } catch (NumberFormatException ex) {
     return null;
   }
   if (0 >= size) {
     return null;
   }
   boolean success = false;
   FileSaver fileSaver = new FileSaver(partialFile, resultFile, size);
   try {
     fileSaver.file = new RandomAccessFile(partialFile, "rw");
     fileSaver.saved = fileSaver.file.length() - PARTIAL_EXTRA_DATA;
     fileSaver.file.seek(fileSaver.saved);
     fileSaver.lastClock = fileSaver.file.readBoolean();
     fileSaver.lastCrc = fileSaver.file.read() << 24;
     fileSaver.lastCrc |= fileSaver.file.read() << 16;
     fileSaver.lastCrc |= fileSaver.file.read() << 8;
     fileSaver.lastCrc |= fileSaver.file.read();
     fileSaver.file.close();
     fileSaver.file = null;
     success = true;
     return fileSaver;
   } finally {
     if (!success) {
       fileSaver.close();
     }
   }
 }