com.hfg.util.io.LocalFileObj 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.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import com.hfg.util.StringUtil;
import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
//------------------------------------------------------------------------------
/**
* Implementation of FileObj for local files.
*
* @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 LocalFileObj extends FileObjImpl
{
private final File mFile;
private String mRequestedFilePath;
private static final String SCHEME = "file";
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public LocalFileObj(File inFile)
{
mFile = inFile;
}
//---------------------------------------------------------------------------
public LocalFileObj(String inFilePath)
{
mFile = new File(inFilePath);
}
//---------------------------------------------------------------------------
public LocalFileObj(URI inURI)
{
if (! inURI.getScheme().equals("file"))
{
throw new RuntimeIOException("URIs with scheme " + StringUtil.singleQuote(inURI.getScheme())
+ " cannot be used to create a " + getClass().getSimpleName() + "!");
}
mFile = new File(inURI.getPath());
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
@Override
public String getScheme()
{
return SCHEME;
}
//---------------------------------------------------------------------------
@Override
public String getName()
{
return mFile.getName();
}
//---------------------------------------------------------------------------
@Override
public String getPath()
{
return mFile.getPath();
}
//---------------------------------------------------------------------------
public FileObj setRequestedPath(String inValue)
{
mRequestedFilePath = inValue;
return this;
}
//---------------------------------------------------------------------------
@Override
public String getRequestedPath()
{
return mRequestedFilePath != null ? mRequestedFilePath : getPath();
}
//---------------------------------------------------------------------------
@Override
public long length()
{
return mFile.length();
}
//---------------------------------------------------------------------------
@Override
public Long lastModified()
{
Long lastModifiedOverride = super.lastModified();
return lastModifiedOverride != null ? lastModifiedOverride : mFile.lastModified();
}
//---------------------------------------------------------------------------
@Override
public Calendar getTimestamp()
{
Calendar timestamp = new GregorianCalendar();
timestamp.setTime(new Date(mFile.lastModified()));
return timestamp;
}
//---------------------------------------------------------------------------
@Override
public URI getURI()
{
URI uri;
try
{
uri = new URI(getScheme(), "", getPath(), null);
}
catch (Exception e)
{
throw new RuntimeIOException(e);
}
return uri;
}
//---------------------------------------------------------------------------
@Override
public InputStream getInputStream()
throws IOException
{
return new BufferedInputStream(new FileInputStream(mFile));
}
//---------------------------------------------------------------------------
@Override
public OutputStream getOutputStream()
throws IOException
{
return new BufferedOutputStream(new FileOutputStream(mFile));
}
//---------------------------------------------------------------------------
@Override
public boolean exists()
{
return mFile.exists();
}
//---------------------------------------------------------------------------
@Override
public boolean canRead()
{
return mFile.canRead();
}
//---------------------------------------------------------------------------
@Override
public boolean canWrite()
{
return mFile.canWrite();
}
//---------------------------------------------------------------------------
@Override
public boolean isFile()
{
return mFile.isFile();
}
//---------------------------------------------------------------------------
@Override
public boolean isDirectory()
{
return mFile.isDirectory();
}
//---------------------------------------------------------------------------
@Override
public boolean mkdirs()
throws IOException
{
return mFile.mkdirs();
}
//--------------------------------------------------------------------------
@Override
public boolean moveTo(FileObj inDest)
throws IOException
{
boolean result;
if (inDest instanceof LocalFileObj)
{
Files.move(mFile.toPath(), ((LocalFileObj)(inDest)).mFile.toPath(), REPLACE_EXISTING);
result = true;
}
else
{
// The destination isn't a filesystem location so use the generic moveTo().
result = super.moveTo(inDest);
}
return result;
}
//--------------------------------------------------------------------------
@Override
public boolean copyTo(FileObj inDest)
throws IOException
{
boolean result;
if (inDest instanceof LocalFileObj)
{
Files.copy(mFile.toPath(), ((LocalFileObj)(inDest)).mFile.toPath(), COPY_ATTRIBUTES, REPLACE_EXISTING);
result = true;
}
else
{
// The destination isn't a filesystem location so use the generic copyTo().
result = super.copyTo(inDest);
}
return result;
}
//---------------------------------------------------------------------------
@Override
public List listFiles()
{
List fileObjs = null;
File[] files = mFile.listFiles();
if (files.length > 0)
{
fileObjs = new ArrayList<>(files.length);
for (File file : files)
{
fileObjs.add(new LocalFileObj(file));
}
}
return fileObjs;
}
//---------------------------------------------------------------------------
@Override
public boolean delete()
{
return mFile.delete();
}
//---------------------------------------------------------------------------
@Override
public FileObj getParentDir()
throws IOException
{
File parentFile = mFile.getParentFile();
FileObj parent = null;
if (parentFile != null)
{
parent = new LocalFileObj(parentFile);
}
return parent;
}
}