@Override public int read() throws IOException { int read = in.read(); if(read!=-1) offset += 1; return read; }
@Override public void close() throws IOException { in.close(); } }
protected FTPFile(FileURL fileURL, org.apache.commons.net.ftp.FTPFile file) throws IOException { super(fileURL); this.absPath = fileURL.getPath(); if(file==null) { this.file = getFTPFile(fileURL); // If file doesn't exist (could not be resolved), create it if(this.file==null) { String name = fileURL.getFilename(); // Filename could potentially be null this.file = createFTPFile(name==null?"":name, false); this.fileExists = false; } else { this.fileExists = true; } } else { this.file = file; this.fileExists = true; } this.permissions = new FTPFilePermissions(this.file); }
@Override public void delete() throws IOException { // Retrieve a ConnectionHandler and lock it FTPConnectionHandler connHandler = (FTPConnectionHandler)ConnectionPool.getConnectionHandler(this, fileURL, true); try { // Makes sure the connection is started, if not starts it connHandler.checkConnection(); if(isDirectory()) connHandler.ftpClient.removeDirectory(absPath); else connHandler.ftpClient.deleteFile(absPath); fileExists = false; // need to set to false since the file is cached // Throw an IOException if server replied with an error connHandler.checkServerReply(); } catch(IOException e) { // Checks if the IOException corresponds to a socket error and in that case, closes the connection connHandler.checkSocketException(e); // Re-throw IOException throw e; } finally { // Release the lock on the ConnectionHandler connHandler.releaseLock(); } }
@Override public FilePermissions getPermissions() { if(isSymlink()) return getCanonicalFile().getAncestor(FTPFile.class).permissions; return permissions; }
public void seek(final long offset) throws IOException { try { in.close(); } catch(IOException e) {} in = new FTPInputStream(offset); this.offset = offset; }
/** * Checks if the given IOException corresponds to a low-level socket exception, and if that is the case, * closes the connection so that {@link #isConnected()} returns false. * All IOException raised by FTPClient should be checked by this method so that socket errors are properly detected. */ private void checkSocketException(IOException e) { if(((e instanceof FTPConnectionClosedException) || (e instanceof SocketException) || (e instanceof SocketTimeoutException)) && isConnected()) { LOGGER.info("socket exception detected, closing connection", e); closeConnection(); } }
@Override public InputStream getInputStream(long offset) throws IOException { return new FTPInputStream(offset); }
@Override public OutputStream getOutputStream() throws IOException { return new FTPOutputStream(false); }
public long getLength() throws IOException { return FTPFile.this.getSize(); }
@Override public InputStream getInputStream() throws IOException { return getInputStream(0); }
public ConnectionHandler createConnectionHandler(FileURL location) { return new FTPConnectionHandler(location); }
@Override public void keepAlive() { // Send a NOOP command to the server to keep the connection alive. // Note: not all FTP servers support the NOOP command. if(ftpClient!=null) { try { ftpClient.sendNoOp(); } catch(IOException e) { // Checks if the IOException corresponds to a socket error and in that case, closes the connection checkSocketException(e); } } } }
@Override public AbstractFile getTemporaryFile() throws IOException { return tempFolder.getDirectChild(getPseudoUniqueFilename(FTPFileTest.class.getName())); }
@Override public AbstractFile getParent() { if(!parentValSet) { FileURL parentFileURL = this.fileURL.getParent(); if(parentFileURL!=null) { try { parent = FileFactory.getFile(parentFileURL, null, createFTPFile(parentFileURL.getFilename(), true)); } catch(IOException e) { // No parent, that's all } } parentValSet = true; } return parent; }
@Override public long getSize() { if(isSymlink()) return ((org.apache.commons.net.ftp.FTPFile)getCanonicalFile().getUnderlyingFileObject()).getSize(); return file.getSize(); }
@Override public int read(byte b[], int off, int len) throws IOException { int nbRead = in.read(b, off, len); if(nbRead!=-1) offset += nbRead; return nbRead; }
private FTPRandomAccessInputStream() throws IOException { this.in = new FTPInputStream(0); }
@Override public OutputStream getAppendOutputStream() throws IOException { return new FTPOutputStream(true); }