Exemplo n.º 1
0
        /// <summary>
        /// A stream obtained via this call must be closed before using other APIs of
        /// this class or else the invocation will block.
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        public override FSDataOutputStream Create(Path file, FsPermission permission, bool
                                                  overwrite, int bufferSize, short replication, long blockSize, Progressable progress
                                                  )
        {
            FTPClient  client   = Connect();
            Path       workDir  = new Path(client.PrintWorkingDirectory());
            Path       absolute = MakeAbsolute(workDir, file);
            FileStatus status;

            try
            {
                status = GetFileStatus(client, file);
            }
            catch (FileNotFoundException)
            {
                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_263(this, client, client.StoreFileStream
                                                                     (file.GetName()), 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
                fos.Close();
                throw new IOException("Unable to create file: " + file + ", Aborting");
            }
            return(fos);
        }
Exemplo n.º 2
0
        /// <exception cref="System.IO.IOException"/>
        public override FSDataInputStream Open(Path file, int bufferSize)
        {
            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);
        }