com.hfg.util.io.AbstractRemoteFile 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.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Calendar;
//------------------------------------------------------------------------------
/**
* Abstract implementation of RemoteFile.
*
* @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 AbstractRemoteFile implements RemoteFile
{
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public AbstractRemoteFile()
{
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
@Override
public String toString()
{
/*
StringBuilder buffer = new StringBuilder();
buffer.append("\nNAME: " + getName() + "\n");
buffer.append("PATH: " + getPath() + "\n");
buffer.append("SIZE: " + getSize() + "\n");
buffer.append("TIMESTAMP: " + getTimestamp().getTime() + "\n");
return buffer.toString();
*/
return getURL();
}
//---------------------------------------------------------------------------
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, getPath()));
}
//---------------------------------------------------------------------------
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 + "'!");
}
}
if (inLocalFile.exists()
&& inLocalFile.length() == getSize())
{
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;
}
}
BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(inLocalFile));
writeToStream(outStream);
outStream.close();
// Attempt to preserve the file's last modified timestamp
Calendar timestamp = getTimestamp();
if (timestamp != null
&& timestamp.getTimeInMillis() > 0)
{
inLocalFile.setLastModified(timestamp.getTimeInMillis());
}
return true;
}
}