예제 #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;
 }
예제 #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);
 }
예제 #3
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);
      }
    }
  }
예제 #4
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());
 }
예제 #5
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;
 }
예제 #6
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;
  }
예제 #7
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();
  }
예제 #8
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;
  }
예제 #9
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();
  }