コード例 #1
0
 private String createUniqueFileName(String origFileName) throws IOException {
   // Search for the extension
   int lastPeriodIdx = origFileName.lastIndexOf('.');
   String prefix = "";
   String suffix = "";
   if (lastPeriodIdx == -1) {
     prefix = origFileName;
   } else {
     prefix = origFileName.substring(0, lastPeriodIdx);
     if (lastPeriodIdx < origFileName.length() - 1) {
       suffix = origFileName.substring(lastPeriodIdx + 1);
     }
   }
   // Search for a possible file name
   for (int i = 0; i < 1000; ++i) {
     StringBuffer n = new StringBuffer();
     n.append(prefix).append("-").append(i).append(".").append(suffix);
     String newName = n.toString();
     FileAdapter f = new FileAdapter(newName);
     try {
       if (!f.exists()) {
         return newName;
       }
     } finally {
       f.close();
     }
   }
   return origFileName;
 }
コード例 #2
0
  protected int addItem(SyncItem item) throws SyncException {
    if (Log.isLoggable(Log.DEBUG)) {
      Log.debug(TAG_LOG, "addItem");
    }
    JSONSyncItem jsonSyncItem = (JSONSyncItem) item;
    try {
      String fullName = getFileFullName(jsonSyncItem.getContentName());

      FileAdapter tgtFile = new FileAdapter(fullName);
      if (tgtFile.exists()) {
        // This is the case where the client and the server have a file
        // with the very same name but different content. In this case
        // we rename the destination file
        fullName = createUniqueFileName(fullName);
        if (Log.isLoggable(Log.INFO)) {
          Log.info(TAG_LOG, "Changing target file name to avoid clashing " + fullName);
        }
      }
      tgtFile.close();

      item.setKey(fullName);
      if (Log.isLoggable(Log.DEBUG)) {
        Log.debug(TAG_LOG, "key set to:" + fullName);
      }
      // This is a new file, rename the temp file
      String sourceFileName = createTempFileName(jsonSyncItem.getContentName());
      renameTempFile(sourceFileName, fullName);

      super.addItem(item);
      return SyncSource.SUCCESS_STATUS;
    } catch (IOException ioe) {
      Log.error(TAG_LOG, "Cannot rename temporary file", ioe);
      throw new SyncException(SyncException.CLIENT_ERROR, "Cannot rename temporary file");
    }
  }
コード例 #3
0
 public boolean exists(String luid) {
   FileAdapter fa = null;
   try {
     fa = new FileAdapter(luid);
     return fa.exists();
   } catch (Throwable t) {
     return false;
   } finally {
     if (fa != null) {
       try {
         fa.close();
       } catch (Exception ex) {
       }
     }
   }
 }
コード例 #4
0
  public long getPartiallyReceivedItemSize(String luid) {

    FileAdapter fa = null;
    try {
      String tempFileName = createTempFileName(getFileNameFromKey(luid));
      fa = new FileAdapter(tempFileName);
      if (!fa.exists()) {
        return -1;
      }
      return fa.getSize();
    } catch (Exception e) {
      return -1;
    } finally {
      if (fa != null) {
        try {
          fa.close();
        } catch (Exception ex) {
        }
      }
    }
  }
コード例 #5
0
 /**
  * Delete an item from the local store.
  *
  * @param key the item key
  * @throws SyncException if the operation fails for any reason
  */
 public int deleteItem(String key) throws SyncException {
   if (Log.isLoggable(Log.INFO)) {
     Log.info(TAG_LOG, "Deleting item " + key);
   }
   FileAdapter fa = null;
   try {
     fa = new FileAdapter(key);
     if (fa.exists()) {
       fa.delete();
     }
     return SyncSource.SUCCESS_STATUS;
   } catch (Exception e) {
     Log.error(TAG_LOG, "Cannot delete item", e);
     return SyncSource.ERROR_STATUS;
   } finally {
     if (fa != null) {
       try {
         fa.close();
       } catch (IOException ex) {
       }
     }
   }
 }
コード例 #6
0
  /**
   * Twin detection implementation
   *
   * @param item
   * @return the twin sync item, whose key is the LUID
   */
  public SyncItem findTwin(SyncItem item) {

    if (item instanceof JSONSyncItem) {

      JSONFileObject json = ((JSONSyncItem) item).getJSONFileObject();
      String fileName = json.getName();
      String fullName = getFileFullName(fileName);

      // Does this existing in our directory?
      if (Log.isLoggable(Log.DEBUG)) {
        Log.debug(TAG_LOG, "Checking for twin for: " + fileName);
      }
      FileAdapter fa = null;
      try {
        fa = new FileAdapter(fullName);
        if (fa.exists() && fa.getSize() == json.getSize()) {
          if (Log.isLoggable(Log.DEBUG)) {
            Log.debug(TAG_LOG, "Twin found");
          }
          item.setKey(fullName);
          return item;
        }
      } catch (Throwable t) {
        Log.error(TAG_LOG, "Cannot check for twins", t);
      } finally {
        if (fa != null) {
          try {
            fa.close();
          } catch (IOException ioe) {
          }
        }
      }
    }
    // No twin found
    return null;
  }