Esempio n. 1
1
 @Override
 public byte[] readAsBinary(String appWorkSpace, String filePath, int start, int end)
     throws FileNotFoundException, IOException, XInvalidModificationException {
   File file = new File(appWorkSpace, filePath);
   if (!XFileUtils.isFileAncestorOf(appWorkSpace, file.getCanonicalPath())) {
     throw new XInvalidModificationException(INVALID_MODIFICATION_EXCEPTION_NOT_IN_ROOT_DIR);
   }
   /** 要读取的文件长度 */
   long length = file.length();
   byte[] bytes = new byte[DEFAULT_READ_SIZE];
   InputStream inputStream = new FileInputStream(file.getAbsolutePath());
   BufferedInputStream bis = new BufferedInputStream(inputStream, DEFAULT_BUFFER_SIZE);
   ByteArrayOutputStream bos = new ByteArrayOutputStream();
   length = getFileSliceLength(start, end, file.length());
   int numRead = 0;
   long abosoluteStart = getAbosolutePosition(start, file.length());
   if (abosoluteStart > 0) {
     bis.skip(abosoluteStart);
   }
   while (length > 0
       && (numRead = bis.read(bytes, 0, (int) Math.min(DEFAULT_READ_SIZE, length))) >= 0) {
     length -= numRead;
     bos.write(bytes, 0, numRead);
   }
   byte[] content = bos.toByteArray();
   inputStream.close();
   bis.close();
   bos.close();
   return content;
 }
Esempio n. 2
0
 @Override
 public long truncateFile(String appWorkSpace, String filePath, long size)
     throws FileNotFoundException, IOException, XInvalidModificationException {
   File file = new File(appWorkSpace, filePath);
   if (!XFileUtils.isFileAncestorOf(appWorkSpace, file.getCanonicalPath())) {
     throw new XInvalidModificationException(INVALID_MODIFICATION_EXCEPTION_NOT_IN_ROOT_DIR);
   }
   return XFileUtils.truncateFile(file.getAbsolutePath(), size);
 }
Esempio n. 3
0
 @Override
 public boolean removeRecursively(String appWorkSpace, String filePath)
     throws IOException, XInvalidModificationException {
   File file = new File(appWorkSpace, filePath);
   if (!XFileUtils.isFileAncestorOf(appWorkSpace, file.getCanonicalPath())) {
     throw new XInvalidModificationException(INVALID_MODIFICATION_EXCEPTION_NOT_IN_ROOT_DIR);
   }
   if (isRootDirectory(appWorkSpace, file.getAbsolutePath())) {
     return false;
   }
   return XFileUtils.deleteFileRecursively(file.getAbsolutePath());
 }
 /**
  * 保存crashReport到sd卡
  *
  * @param crashInfo :崩溃信息类
  */
 public void saveCrashReportLocal(XCrashInfo crashInfo) {
   if (null != crashInfo && null != mFileDir) {
     String filePath = new File(mFileDir, generateReportFileNameByTime()).getAbsolutePath();
     String crashString = crashInfo.toString();
     String wrapString = crashString.replaceAll("\\\\n", "\n");
     String IndentationString = wrapString.replaceAll("\\\\t", "\t");
     XFileUtils.writeFileByString(filePath, IndentationString);
   }
 }
Esempio n. 5
0
  @Override
  public File transferTo(
      String appWorkSpace, String oldPath, String newPath, String newName, boolean move)
      throws XNoModificationAllowedException, IOException, XInvalidModificationException,
          XEncodingException {
    if (null != newName && newName.contains(":")) {
      throw new XEncodingException(ENCODING_EXCEPTION_NAME_CONTAINS_COLON);
    }
    File source = new File(appWorkSpace, oldPath);

    if (!source.exists()) {
      throw new FileNotFoundException(FILE_NOTFOUND_EXCEPTION_PATH_NOT_EXISTS);
    }

    File destinationDir = new File(appWorkSpace, newPath);
    if (!destinationDir.exists()) {
      throw new FileNotFoundException(FILE_NOTFOUND_EXCEPTION_PATH_NOT_EXISTS);
    }

    if (!XFileUtils.isFileAncestorOf(appWorkSpace, source.getCanonicalPath())
        || !XFileUtils.isFileAncestorOf(appWorkSpace, destinationDir.getCanonicalPath())) {
      throw new XInvalidModificationException(INVALID_MODIFICATION_EXCEPTION_NOT_IN_ROOT_DIR);
    }
    File destination = createDestination(newName, source, destinationDir);

    if (source.getAbsolutePath().equals(destination.getAbsolutePath())) {
      throw new XInvalidModificationException(INVALID_MODIFICATION_EXCEPTION_COPY_ERROR);
    }

    if (source.isDirectory()) {
      if (move) {
        return moveDirectory(source, destination);
      } else {
        return copyDirectory(source, destination);
      }
    } else {
      if (move) {
        return moveFile(source, destination);
      } else {
        return copyFile(source, destination);
      }
    }
  }
Esempio n. 6
0
 @Override
 public String getParentPath(String appWorkSpace, String filePath)
     throws IOException, XInvalidModificationException {
   File file = new File(appWorkSpace, filePath);
   if (!XFileUtils.isFileAncestorOf(appWorkSpace, file.getCanonicalPath())) {
     throw new XInvalidModificationException(INVALID_MODIFICATION_EXCEPTION_NOT_IN_ROOT_DIR);
   }
   if (isRootDirectory(appWorkSpace, file.getAbsolutePath())) {
     return appWorkSpace;
   }
   filePath = file.getParent();
   return filePath;
 }
Esempio n. 7
0
  @Override
  public File getFileObject(
      String appWorkSpace,
      String dirPath,
      String fileName,
      boolean create,
      boolean exclusive,
      boolean directory)
      throws XFileExistsException, IOException, XTypeMismatchException, XEncodingException,
          XInvalidModificationException {
    if (fileName.contains(":")) {
      throw new XEncodingException(ENCODING_EXCEPTION_NAME_CONTAINS_COLON);
    }

    File fp = new File(appWorkSpace, dirPath);
    File file = null;
    if (fileName.startsWith(File.separator)) {
      // 当传进来的文件名是以"/"开头时,在unix下认为是根路径开始
      file = new File(appWorkSpace, fileName);
    } else {
      file = new File(fp, fileName);
    }
    if (!XFileUtils.isFileAncestorOf(appWorkSpace, file.getCanonicalPath())) {
      throw new XInvalidModificationException(INVALID_MODIFICATION_EXCEPTION_NOT_IN_ROOT_DIR);
    }
    if (create) {
      if (exclusive && file.exists()) {
        throw new XFileExistsException(FILE_EXISTS_EXCEPTION_CREATE_OR_EXCLUSIVE_FAILS);
      }
      if (directory) {
        file.mkdir();
      } else {
        file.createNewFile();
      }
      if (!file.exists()) {
        throw new XFileExistsException(FILE_EXISTS_EXCEPTION_CREATE_FAILS);
      }
    } else {
      if (!file.exists()) {
        throw new FileNotFoundException(FILE_NOTFOUND_EXCEPTION_PATH_NOT_EXISTS);
      }
      if (directory) {
        if (file.isFile()) {
          throw new XTypeMismatchException(TYPE_MISMATCH_EXCEPTION_PATH_NOT_EXISTS_OR_IS_FILE);
        }
      } else if (file.isDirectory()) {
        throw new XTypeMismatchException(TYPE_MISMATCH_EXCEPTION_PATH_NOT_EXISTS_OR_IS_DIR);
      }
    }
    return file;
  }
Esempio n. 8
0
  @Override
  public long getMetadata(String appWorkSpace, String filePath)
      throws FileNotFoundException, IOException, XInvalidModificationException {
    File file = new File(appWorkSpace, filePath);
    if (!XFileUtils.isFileAncestorOf(appWorkSpace, file.getCanonicalPath())) {
      throw new XInvalidModificationException(INVALID_MODIFICATION_EXCEPTION_NOT_IN_ROOT_DIR);
    }

    if (!file.exists()) {
      throw new FileNotFoundException(FILE_NOTFOUND_EXCEPTION_PATH_NOT_EXISTS);
    }

    return file.lastModified();
  }
Esempio n. 9
0
  @Override
  public File[] getFileEntries(String appWorkSpace, String filePath)
      throws FileNotFoundException, IOException, XInvalidModificationException {
    File file = new File(appWorkSpace, filePath);
    if (!XFileUtils.isFileAncestorOf(appWorkSpace, file.getCanonicalPath())) {
      throw new XInvalidModificationException(INVALID_MODIFICATION_EXCEPTION_NOT_IN_ROOT_DIR);
    }

    if (!file.exists()) {
      throw new FileNotFoundException();
    }
    if (file.isDirectory()) {
      File[] files = file.listFiles();
      return files;
    }
    return null;
  }
Esempio n. 10
0
  @Override
  public boolean remove(String appWorkSpace, String filePath)
      throws IOException, XNoModificationAllowedException, XInvalidModificationException {
    File file = new File(appWorkSpace, filePath);
    if (!XFileUtils.isFileAncestorOf(appWorkSpace, file.getCanonicalPath())) {
      throw new XInvalidModificationException(INVALID_MODIFICATION_EXCEPTION_NOT_IN_ROOT_DIR);
    }
    // 不能删除根目录.
    if (isRootDirectory(appWorkSpace, file.getAbsolutePath())) {
      throw new XNoModificationAllowedException(INVALID_MODIFICATION_EXCEPTION_DELETE_ROOT_DIR_ERR);
    }

    // 不能删除不为空的文件夹
    if (file.isDirectory() && file.list().length > 0) {
      throw new XInvalidModificationException(INVALID_MODIFICATION_EXCEPTION_DIR_IS_NOT_EMPTY);
    }

    return file.delete();
  }