Java Code Examples for org.apache.commons.net.ftp.ftpclient#allocate()

The following examples show how to use org.apache.commons.net.ftp.ftpclient#allocate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FTPFileSystem.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public FSDataInputStream open(Path file, int bufferSize) throws IOException {
  FTPClient client = connect();
  Path workDir = new Path(client.printWorkingDirectory());
  Path absolute = makeAbsolute(workDir, file);
  FileStatus fileStat = getFileStatus(client, absolute);
  if (fileStat.isDirectory()) {
    disconnect(client);
    throw new FileNotFoundException("Path " + file + " is a directory.");
  }
  client.allocate(bufferSize);
  Path parent = absolute.getParent();
  // Change to parent directory on the
  // server. Only then can we read the
  // file
  // on the server by opening up an InputStream. As a side effect the working
  // directory on the server is changed to the parent directory of the file.
  // The FTP client connection is closed when close() is called on the
  // FSDataInputStream.
  client.changeWorkingDirectory(parent.toUri().getPath());
  InputStream is = client.retrieveFileStream(file.getName());
  FSDataInputStream fis = new FSDataInputStream(new FTPInputStream(is,
      client, statistics));
  if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
    // The ftpClient is an inconsistent state. Must close the stream
    // which in turn will logout and disconnect from FTP server
    fis.close();
    throw new IOException("Unable to open file: " + file + ", Aborting");
  }
  return fis;
}
 
Example 2
Source File: FTPFileSystem.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public FSDataInputStream open(Path file, int bufferSize) throws IOException {
  FTPClient client = connect();
  Path workDir = new Path(client.printWorkingDirectory());
  Path absolute = makeAbsolute(workDir, file);
  FileStatus fileStat = getFileStatus(client, absolute);
  if (fileStat.isDirectory()) {
    disconnect(client);
    throw new FileNotFoundException("Path " + file + " is a directory.");
  }
  client.allocate(bufferSize);
  Path parent = absolute.getParent();
  // Change to parent directory on the
  // server. Only then can we read the
  // file
  // on the server by opening up an InputStream. As a side effect the working
  // directory on the server is changed to the parent directory of the file.
  // The FTP client connection is closed when close() is called on the
  // FSDataInputStream.
  client.changeWorkingDirectory(parent.toUri().getPath());
  InputStream is = client.retrieveFileStream(file.getName());
  FSDataInputStream fis = new FSDataInputStream(new FTPInputStream(is,
      client, statistics));
  if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
    // The ftpClient is an inconsistent state. Must close the stream
    // which in turn will logout and disconnect from FTP server
    fis.close();
    throw new IOException("Unable to open file: " + file + ", Aborting");
  }
  return fis;
}
 
Example 3
Source File: FTPFileSystem.java    From RDFS with Apache License 2.0 5 votes vote down vote up
@Override
public FSDataInputStream open(Path file, int bufferSize) throws IOException {
  FTPClient client = connect();
  Path workDir = new Path(client.printWorkingDirectory());
  Path absolute = makeAbsolute(workDir, file);
  FileStatus fileStat = getFileStatus(client, absolute);
  if (fileStat.isDir()) {
    disconnect(client);
    throw new IOException("Path " + file + " is a directory.");
  }
  client.allocate(bufferSize);
  Path parent = absolute.getParent();
  // Change to parent directory on the
  // server. Only then can we read the
  // file
  // on the server by opening up an InputStream. As a side effect the working
  // directory on the server is changed to the parent directory of the file.
  // The FTP client connection is closed when close() is called on the
  // FSDataInputStream.
  client.changeWorkingDirectory(parent.toUri().getPath());
  InputStream is = client.retrieveFileStream(file.getName());
  FSDataInputStream fis = new FSDataInputStream(new FTPInputStream(is,
      client, statistics));
  if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
    // The ftpClient is an inconsistent state. Must close the stream
    // which in turn will logout and disconnect from FTP server
    fis.close();
    throw new IOException("Unable to open file: " + file + ", Aborting");
  }
  return fis;
}
 
Example 4
Source File: FTPFileSystem.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
@Override
public FSDataInputStream open(Path file, int bufferSize) throws IOException {
  FTPClient client = connect();
  Path workDir = new Path(client.printWorkingDirectory());
  Path absolute = makeAbsolute(workDir, file);
  FileStatus fileStat = getFileStatus(client, absolute);
  if (fileStat.isDir()) {
    disconnect(client);
    throw new IOException("Path " + file + " is a directory.");
  }
  client.allocate(bufferSize);
  Path parent = absolute.getParent();
  // Change to parent directory on the
  // server. Only then can we read the
  // file
  // on the server by opening up an InputStream. As a side effect the working
  // directory on the server is changed to the parent directory of the file.
  // The FTP client connection is closed when close() is called on the
  // FSDataInputStream.
  client.changeWorkingDirectory(parent.toUri().getPath());
  InputStream is = client.retrieveFileStream(file.getName());
  FSDataInputStream fis = new FSDataInputStream(new FTPInputStream(is,
      client, statistics));
  if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
    // The ftpClient is an inconsistent state. Must close the stream
    // which in turn will logout and disconnect from FTP server
    fis.close();
    throw new IOException("Unable to open file: " + file + ", Aborting");
  }
  return fis;
}
 
Example 5
Source File: FTPFileSystem.java    From hadoop with Apache License 2.0 4 votes vote down vote up
/**
 * A stream obtained via this call must be closed before using other APIs of
 * this class or else the invocation will block.
 */
@Override
public FSDataOutputStream create(Path file, FsPermission permission,
    boolean overwrite, int bufferSize, short replication, long blockSize,
    Progressable progress) throws IOException {
  final FTPClient client = connect();
  Path workDir = new Path(client.printWorkingDirectory());
  Path absolute = makeAbsolute(workDir, file);
  FileStatus status;
  try {
    status = getFileStatus(client, file);
  } catch (FileNotFoundException fnfe) {
    status = null;
  }
  if (status != null) {
    if (overwrite && !status.isDirectory()) {
      delete(client, file, false);
    } else {
      disconnect(client);
      throw new FileAlreadyExistsException("File already exists: " + file);
    }
  }
  
  Path parent = absolute.getParent();
  if (parent == null || !mkdirs(client, parent, FsPermission.getDirDefault())) {
    parent = (parent == null) ? new Path("/") : parent;
    disconnect(client);
    throw new IOException("create(): Mkdirs failed to create: " + parent);
  }
  client.allocate(bufferSize);
  // Change to parent directory on the server. Only then can we write to the
  // file on the server by opening up an OutputStream. As a side effect the
  // working directory on the server is changed to the parent directory of the
  // file. The FTP client connection is closed when close() is called on the
  // FSDataOutputStream.
  client.changeWorkingDirectory(parent.toUri().getPath());
  FSDataOutputStream fos = new FSDataOutputStream(client.storeFileStream(file
      .getName()), statistics) {
    @Override
    public void close() throws IOException {
      super.close();
      if (!client.isConnected()) {
        throw new FTPException("Client not connected");
      }
      boolean cmdCompleted = client.completePendingCommand();
      disconnect(client);
      if (!cmdCompleted) {
        throw new FTPException("Could not complete transfer, Reply Code - "
            + client.getReplyCode());
      }
    }
  };
  if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
    // The ftpClient is an inconsistent state. Must close the stream
    // which in turn will logout and disconnect from FTP server
    fos.close();
    throw new IOException("Unable to create file: " + file + ", Aborting");
  }
  return fos;
}
 
Example 6
Source File: FTPFileSystem.java    From big-c with Apache License 2.0 4 votes vote down vote up
/**
 * A stream obtained via this call must be closed before using other APIs of
 * this class or else the invocation will block.
 */
@Override
public FSDataOutputStream create(Path file, FsPermission permission,
    boolean overwrite, int bufferSize, short replication, long blockSize,
    Progressable progress) throws IOException {
  final FTPClient client = connect();
  Path workDir = new Path(client.printWorkingDirectory());
  Path absolute = makeAbsolute(workDir, file);
  FileStatus status;
  try {
    status = getFileStatus(client, file);
  } catch (FileNotFoundException fnfe) {
    status = null;
  }
  if (status != null) {
    if (overwrite && !status.isDirectory()) {
      delete(client, file, false);
    } else {
      disconnect(client);
      throw new FileAlreadyExistsException("File already exists: " + file);
    }
  }
  
  Path parent = absolute.getParent();
  if (parent == null || !mkdirs(client, parent, FsPermission.getDirDefault())) {
    parent = (parent == null) ? new Path("/") : parent;
    disconnect(client);
    throw new IOException("create(): Mkdirs failed to create: " + parent);
  }
  client.allocate(bufferSize);
  // Change to parent directory on the server. Only then can we write to the
  // file on the server by opening up an OutputStream. As a side effect the
  // working directory on the server is changed to the parent directory of the
  // file. The FTP client connection is closed when close() is called on the
  // FSDataOutputStream.
  client.changeWorkingDirectory(parent.toUri().getPath());
  FSDataOutputStream fos = new FSDataOutputStream(client.storeFileStream(file
      .getName()), statistics) {
    @Override
    public void close() throws IOException {
      super.close();
      if (!client.isConnected()) {
        throw new FTPException("Client not connected");
      }
      boolean cmdCompleted = client.completePendingCommand();
      disconnect(client);
      if (!cmdCompleted) {
        throw new FTPException("Could not complete transfer, Reply Code - "
            + client.getReplyCode());
      }
    }
  };
  if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
    // The ftpClient is an inconsistent state. Must close the stream
    // which in turn will logout and disconnect from FTP server
    fos.close();
    throw new IOException("Unable to create file: " + file + ", Aborting");
  }
  return fos;
}
 
Example 7
Source File: FTPFileSystem.java    From RDFS with Apache License 2.0 4 votes vote down vote up
/**
 * A stream obtained via this call must be closed before using other APIs of
 * this class or else the invocation will block.
 */
@Override
public FSDataOutputStream create(Path file, FsPermission permission,
    boolean overwrite, int bufferSize, short replication, long blockSize,
    Progressable progress) throws IOException {
  final FTPClient client = connect();
  Path workDir = new Path(client.printWorkingDirectory());
  Path absolute = makeAbsolute(workDir, file);
  if (exists(client, file)) {
    if (overwrite) {
      delete(client, file);
    } else {
      disconnect(client);
      throw new IOException("File already exists: " + file);
    }
  }
  Path parent = absolute.getParent();
  if (parent == null || !mkdirs(client, parent, FsPermission.getDefault())) {
    parent = (parent == null) ? new Path("/") : parent;
    disconnect(client);
    throw new IOException("create(): Mkdirs failed to create: " + parent);
  }
  client.allocate(bufferSize);
  // Change to parent directory on the server. Only then can we write to the
  // file on the server by opening up an OutputStream. As a side effect the
  // working directory on the server is changed to the parent directory of the
  // file. The FTP client connection is closed when close() is called on the
  // FSDataOutputStream.
  client.changeWorkingDirectory(parent.toUri().getPath());
  FSDataOutputStream fos = new FSDataOutputStream(client.storeFileStream(file
      .getName()), statistics) {
    @Override
    public void close() throws IOException {
      super.close();
      if (!client.isConnected()) {
        throw new FTPException("Client not connected");
      }
      boolean cmdCompleted = client.completePendingCommand();
      disconnect(client);
      if (!cmdCompleted) {
        throw new FTPException("Could not complete transfer, Reply Code - "
            + client.getReplyCode());
      }
    }
  };
  if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
    // The ftpClient is an inconsistent state. Must close the stream
    // which in turn will logout and disconnect from FTP server
    fos.close();
    throw new IOException("Unable to create file: " + file + ", Aborting");
  }
  return fos;
}
 
Example 8
Source File: FTPFileSystem.java    From hadoop-gpu with Apache License 2.0 4 votes vote down vote up
/**
 * A stream obtained via this call must be closed before using other APIs of
 * this class or else the invocation will block.
 */
@Override
public FSDataOutputStream create(Path file, FsPermission permission,
    boolean overwrite, int bufferSize, short replication, long blockSize,
    Progressable progress) throws IOException {
  final FTPClient client = connect();
  Path workDir = new Path(client.printWorkingDirectory());
  Path absolute = makeAbsolute(workDir, file);
  if (exists(client, file)) {
    if (overwrite) {
      delete(client, file);
    } else {
      disconnect(client);
      throw new IOException("File already exists: " + file);
    }
  }
  Path parent = absolute.getParent();
  if (parent == null || !mkdirs(client, parent, FsPermission.getDefault())) {
    parent = (parent == null) ? new Path("/") : parent;
    disconnect(client);
    throw new IOException("create(): Mkdirs failed to create: " + parent);
  }
  client.allocate(bufferSize);
  // Change to parent directory on the server. Only then can we write to the
  // file on the server by opening up an OutputStream. As a side effect the
  // working directory on the server is changed to the parent directory of the
  // file. The FTP client connection is closed when close() is called on the
  // FSDataOutputStream.
  client.changeWorkingDirectory(parent.toUri().getPath());
  FSDataOutputStream fos = new FSDataOutputStream(client.storeFileStream(file
      .getName()), statistics) {
    @Override
    public void close() throws IOException {
      super.close();
      if (!client.isConnected()) {
        throw new FTPException("Client not connected");
      }
      boolean cmdCompleted = client.completePendingCommand();
      disconnect(client);
      if (!cmdCompleted) {
        throw new FTPException("Could not complete transfer, Reply Code - "
            + client.getReplyCode());
      }
    }
  };
  if (!FTPReply.isPositivePreliminary(client.getReplyCode())) {
    // The ftpClient is an inconsistent state. Must close the stream
    // which in turn will logout and disconnect from FTP server
    fos.close();
    throw new IOException("Unable to create file: " + file + ", Aborting");
  }
  return fos;
}