com.hfg.util.io.FTPRemoteFile Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.util.io;
import java.io.InputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.OutputStream;
import java.util.Calendar;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTP;
import com.hfg.util.StringUtil;
//------------------------------------------------------------------------------
/**
* Implementation of RemoteFile for FTP files
*
* This class depends on Jakarta's commons-net library (which depends on jakarta-oro).
*
* @author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class FTPRemoteFile extends AbstractRemoteFile
{
//###########################################################################
// PRIVATE FIELDS
//###########################################################################
private FTPClient mFTP;
private String mFTPServer;
private FTPFile mFTPFile;
private String mParentDirPath;
private String mName;
private int mFileTransferMode = sDefaultFileTransferMode;
private static int sDefaultFileTransferMode = FTP.BINARY_FILE_TYPE;
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public FTPRemoteFile(FTPClient inFTP, String inFTPServer, String inParentDirPath, FTPFile inFTPFile)
{
StringBuilder path = new StringBuilder(StringUtil.isSet(inParentDirPath) ? inParentDirPath : "");
if (path.length() == 0 || path.charAt(path.length() - 1) != '/') path.append("/");
path.append(inFTPFile.getName().startsWith("/") ? inFTPFile.getName().substring(1) : inFTPFile.getName());
mParentDirPath = path.substring(0, path.lastIndexOf("/"));
// mParentDirPath = inParentDirPath;
mName = path.substring(path.lastIndexOf("/") + 1);
mFTP = inFTP;
mFTPServer = inFTPServer;
mFTPFile = inFTPFile;
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public String getName()
{
// return mFTPFile.getName();
return mName;
}
//---------------------------------------------------------------------------
public String getPath()
{
return mParentDirPath + "/" + getName();
}
//---------------------------------------------------------------------------
public String getURL()
{
return "ftp://" + mFTPServer + (mParentDirPath.startsWith("/") ? "" : "/") + mParentDirPath + "/" + getName();
}
//---------------------------------------------------------------------------
public long getSize()
{
return mFTPFile.getSize();
}
//---------------------------------------------------------------------------
public Calendar getTimestamp()
{
// Round to seconds since I was seeing some odd behavior with millis precision.
Calendar cal = mFTPFile.getTimestamp();
cal.setTimeInMillis((cal.getTimeInMillis() / 1000) * 1000);
return cal;
}
//---------------------------------------------------------------------------
/**
* Use FTP.ASCII_FILE_TYPE or FTP.BINARY_FILE_TYPE (default).
*/
public void setFileTransferMode(int inValue)
{
mFileTransferMode = FTP.ASCII_FILE_TYPE;
}
//---------------------------------------------------------------------------
/**
* Note that the FTP connection is closed when the returned InputStream is closed.
*/
public InputStream getInputStream()
throws IOException
{
mFTP.setFileType(mFileTransferMode);
InputStream stream = mFTP.retrieveFileStream(mParentDirPath + "/" + getName());
return new BufferedInputStream(stream);
}
//---------------------------------------------------------------------------
public long writeToStream(OutputStream stream)
throws IOException
{
mFTP.setFileType(mFileTransferMode);
mFTP.changeWorkingDirectory(mParentDirPath);
mFTP.retrieveFile(getName(), stream);
// mFTP.retrieveFile(mParentDirPath + "/" + getName(), stream);
return getSize();
}
}