com.hfg.util.io.FileObj 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.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Matcher;
import com.hfg.exception.ProgrammingException;
import com.hfg.security.LoginCredentials;
import com.hfg.util.FileUtil;
import com.hfg.util.StringUtil;
//------------------------------------------------------------------------------
/**
Generic representation of a file object.
@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 interface FileObj extends FileSource
{
public String getPath();
public String getScheme();
/**
The requested path may differ from getPath() if the requested path was a symbolic link.
@return The (unwildcarded) path as it was originally requested.
*/
public String getRequestedPath();
public long length();
public Long lastModified();
public Calendar getTimestamp();
public URI getURI();
public OutputStream getOutputStream() throws IOException;
public long writeToStream(OutputStream stream) throws IOException;
public FileObj setCredentials(LoginCredentials inValue);
public LoginCredentials getCredentials();
/**
Checks if the file exists.
* @return whether the file exists
*/
public boolean exists();
/**
Checks if the file can be read.
* @return whether the file can be read
*/
public boolean canRead();
/**
Checks if the file can be written to.
* @return whether the file can be written to
*/
public boolean canWrite();
/**
Checks if the object represents a file.
* @return whether the object is a file
*/
public boolean isFile();
/**
Checks if the object represents a directory.
* @return whether the object is a directory
*/
public boolean isDirectory();
public FileObj getParentDir() throws IOException;
public List extends FileObj> listFiles();
public boolean mkdirs() throws IOException;
/**
Delete the file. Directories that are not empty are not deleted.
* @return whether the file was deleted
*/
public boolean delete() throws IOException;
/**
Copies the remote file to the specified local directory. If a local file exists and
is up-to-date (same size and an equivalent or newer last modified time),
no transfer is done.
@return Whether or not the file was actually transferred.
*/
public boolean writeToLocalDir(File inLocalDir) throws IOException;
/**
Copies the remote file to the specified local file. If the local file exists and
is up-to-date (same size and an equivalent or newer last modified time),
no transfer is done.
@return Whether or not the file was actually transferred.
*/
public boolean writeToLocalFile(File inLocalFile) throws IOException;
/**
Copies the remote file to the specified local root dir plus the file's remote path.
If the local file exists and is up-to-date (same size and an equivalent or newer last modified time),
no transfer is done.
@return Whether or not the file was actually transferred.
*/
public boolean copyLocallyPreservingPath(File inLocalRootDir) throws IOException;
public void setAttribute(String inKey, Object inValue);
public Object getAttribute(String inKey);
public boolean hasAttribute(String inKey);
public static final String LAST_MODIFIED_ATTR = "lastModified";
//--------------------------------------------------------------------------
public default String getExtension()
throws IOException
{
String result = null;
Matcher m = FileUtil.EXTENSION_PATTERN.matcher(getName());
if (m.find())
{
result = m.group().substring(1);
}
return result;
}
//--------------------------------------------------------------------------
public default boolean moveTo(FileObj inDest)
throws IOException
{
copyTo(inDest);
delete();
return true;
}
//--------------------------------------------------------------------------
public default boolean copyTo(FileObj inDest)
throws IOException
{
InputStream inputStream = getInputStream();
OutputStream outputStream = inDest.getOutputStream();
StreamUtil.writeToStream(inputStream, outputStream);
outputStream.close();
return true;
}
//--------------------------------------------------------------------------
public default List readLines()
throws IOException
{
if (! canRead())
{
throw new RuntimeIOException("No read permissions for file " + StringUtil.singleQuote(getURI()) + "!");
}
List lines = new ArrayList<>();
BufferedReader reader = null;
try
{
reader = new BufferedReader(new InputStreamReader(getInputStream()));
String line;
while ((line = reader.readLine()) != null)
{
lines.add(line);
}
}
finally
{
StreamUtil.close(reader);
}
return lines;
}
//--------------------------------------------------------------------------
public static FileObj allocateFileObj(FileObj inParent, String inFilename)
throws IOException
{
FileObj fileObj;
String scheme = inParent.getScheme();
if (scheme.equalsIgnoreCase("smb"))
{
fileObj = new SMBFileObj((SMBFileObj) inParent, inFilename);
}
else
{
URI uri = null;
try
{
//Two steps to clean file name of strange characters and casting spaces to %20 encoding vs + space...
String cleanFileName = URLEncoder.encode(inFilename, "UTF-8");
cleanFileName = StringUtil.replaceAll(cleanFileName, "+", "%20");
uri = new URI(inParent.getURI() + (inParent.getURI().toString().endsWith("/") ? "" : "/") + cleanFileName);
}
catch (URISyntaxException e)
{
// Ignore
}
if (scheme.equalsIgnoreCase("file"))
{
fileObj = new LocalFileObj(uri);
}
else if (scheme.equalsIgnoreCase("ftp"))
{
fileObj = new FTPFileObj(uri);
}
else if (scheme.equalsIgnoreCase("http")
|| scheme.equalsIgnoreCase("https"))
{
fileObj = new HTTPFileObj(uri);
}
else
{
throw new ProgrammingException("No support yet for " + StringUtil.singleQuote(scheme) + "!");
}
}
return fileObj;
}
//--------------------------------------------------------------------------
public static FileObj allocateFileObj(URI inURI)
throws IOException
{
FileObj fileObj;
String scheme = inURI.getScheme();
if (null == scheme)
{
try
{
inURI = new URI("file://" + inURI);
}
catch (URISyntaxException e)
{
// Ignore
}
scheme = inURI.getScheme();
}
if (scheme.equalsIgnoreCase("file"))
{
fileObj = new LocalFileObj(inURI);
}
else if (scheme.equalsIgnoreCase("smb"))
{
fileObj = new SMBFileObj(inURI);
}
else if (scheme.equalsIgnoreCase("ftp"))
{
fileObj = new FTPFileObj(inURI);
}
else if (scheme.equalsIgnoreCase("http")
|| scheme.equalsIgnoreCase("https"))
{
fileObj = new HTTPFileObj(inURI);
}
else
{
throw new ProgrammingException("No support yet for " + StringUtil.singleQuote(scheme) + "!");
}
return fileObj;
}
}