com.hfg.util.io.FileObjImpl 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.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Calendar;
import com.hfg.security.LoginCredentials;
import com.hfg.units.TimeUnit;
import com.hfg.util.AttributeMgr;
import com.hfg.util.CompareUtil;
import com.hfg.util.ThreadUtil;
//------------------------------------------------------------------------------
/**
* Abstract FileObj base class.
*
* @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 abstract class FileObjImpl implements FileObj, Comparable
{
private LoginCredentials mCredentials;
private AttributeMgr mAttributeMgr;
private int mNumDownloadAttempts = 0;
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public FileObjImpl()
{
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
@Override
public String toString()
{
String theString = getURI().toString();
return theString;
}
//---------------------------------------------------------------------------
@Override
public int hashCode()
{
return getURI().hashCode();
}
//---------------------------------------------------------------------------
@Override
public boolean equals(Object inObj2)
{
return 0 == compareTo(inObj2);
}
//---------------------------------------------------------------------------
public int compareTo(Object inObj2)
{
int result = -1;
if (inObj2 instanceof FileObj)
{
result = CompareUtil.compare(getURI(), ((FileObj) inObj2).getURI());
}
return result;
}
//---------------------------------------------------------------------------
@Override
public Long lastModified()
{
return (Long) getAttribute(LAST_MODIFIED_ATTR);
}
//---------------------------------------------------------------------------
public boolean writeToLocalDir(File inLocalDir)
throws IOException
{
return writeToLocalFile(new File(inLocalDir, getName()));
}
//---------------------------------------------------------------------------
public boolean copyLocallyPreservingPath(File inLocalDir)
throws IOException
{
return writeToLocalFile(new File(inLocalDir, getRequestedPath()));
}
//---------------------------------------------------------------------------
public long writeToStream(OutputStream stream)
throws IOException
{
InputStream inputStream = getInputStream();
byte buffer[] = new byte[8 * 1024];
long fileSize = 0;
int bytesRead;
while ((bytesRead = inputStream.read(buffer, 0, buffer.length)) != -1)
{
stream.write(buffer, 0, bytesRead);
fileSize += bytesRead;
}
inputStream.close();
return fileSize;
}
//---------------------------------------------------------------------------
public boolean writeToLocalFile(File inLocalFile)
throws IOException
{
File localDir = inLocalFile.getParentFile();
if (localDir != null
&& ! localDir.exists())
{
if (!localDir.mkdirs())
{
throw new RuntimeException("Couldn't make the dir '" + localDir + "'!");
}
}
// Compare size
if (inLocalFile.exists()
&& inLocalFile.length() == length())
{
// Compare date
Calendar timestamp = getTimestamp();
if (timestamp != null
&& inLocalFile.lastModified() >= timestamp.getTimeInMillis())
{
// The local file appears to be up-to-date.
System.out.println("Local file is up-to-date.");
return false;
}
}
mNumDownloadAttempts++;
BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(inLocalFile));
writeToStream(outStream);
outStream.close();
// Double check that the file was received completely
if (! inLocalFile.exists()
|| inLocalFile.length() != length())
{
if (mNumDownloadAttempts < 3)
{
System.err.println("Downloaded file is not the correct size. Retrying...");
ThreadUtil.sleep(TimeUnit.minute.getMilliseconds() * mNumDownloadAttempts);
// Repeat the download
writeToLocalFile(inLocalFile);
}
else
{
throw new RuntimeIOException("The downloaded file " + inLocalFile + " is not the correct size!");
}
}
// Attempt to preserve the file's last modified timestamp
Calendar timestamp = getTimestamp();
if (timestamp != null
&& timestamp.getTimeInMillis() > 0)
{
inLocalFile.setLastModified(timestamp.getTimeInMillis());
}
return true;
}
//--------------------------------------------------------------------------
public FileObj setCredentials(LoginCredentials inValue)
{
mCredentials = inValue;
return this;
}
//--------------------------------------------------------------------------
public LoginCredentials getCredentials()
{
return mCredentials;
}
//--------------------------------------------------------------------------
@Override
public void setAttribute(String inKey, Object inValue)
{
getAttributeMgr().setAttribute(inKey, inValue);
}
//--------------------------------------------------------------------------
@Override
public Object getAttribute(String inKey)
{
return getAttributeMgr().getAttribute(inKey);
}
//--------------------------------------------------------------------------
@Override
public boolean hasAttribute(String inKey)
{
return getAttributeMgr().hasAttribute(inKey);
}
//##########################################################################
// PRIVATE METHODS
//##########################################################################
//--------------------------------------------------------------------------
private AttributeMgr getAttributeMgr()
{
if (null == mAttributeMgr)
{
mAttributeMgr = new AttributeMgr();
}
return mAttributeMgr;
}
}