All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ilex.util.FileUtil Maven / Gradle / Ivy

Go to download

A collection of software for aggregatting and processing environmental data such as from NOAA GOES satellites.

The newest version!
/*
*  $Id$
*
*  $Log$
*  Revision 1.3  2014/11/19 16:12:28  mmaloney
*  removed debugs.
*
*  Revision 1.2  2014/10/07 12:49:20  mmaloney
*  added getFileBytes
*
*  Revision 1.1.1.1  2014/05/19 15:28:59  mmaloney
*  OPENDCS 6.0 Initial Checkin
*
*  Revision 1.3  2011/01/14 21:03:03  sparab
*  *** empty log message ***
*
*  Revision 1.2  2010/11/15 21:30:51  sparab
*  Added copyStream(InputStream in, OutputStream out,int timeout) for copying streams till a timeout is reached
*
*  Revision 1.1  2008/04/04 18:21:10  cvs
*  Added legacy code to repository
*
*  Revision 1.7  2008/01/14 14:57:46  mmaloney
*  dev
*
*  Revision 1.6  2006/07/19 21:29:59  mmaloney
*  dev
*
*  Revision 1.5  2005/12/10 21:43:57  mmaloney
*  dev
*
*  Revision 1.4  2005/10/10 19:47:47  mmaloney
*  dev
*
*  Revision 1.3  2004/11/14 21:51:28  mjmaloney
*  dos2unix
*
*  Revision 1.2  2004/11/10 16:27:10  mjmaloney
*  Added unzip capability
*
*  Revision 1.1  2004/08/26 14:14:37  mjmaloney
*  Added FileUtil
*
*/
package ilex.util;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;

/**
A collection of utilities to manipulate files.
*/
public class FileUtil
{
	/**
	  Copies a file.
	  @param from the file being copied
	  @param to the file we're copying to
	  @return number of bytes copied
	  @throws IOException on error.
	*/
	public static int copyFile(File from, File to)
		throws IOException
	{
		if (from.equals(to))
			return 0;

		FileOutputStream fos = null;
		FileInputStream fis = null;
		int total = 0;
		try
		{
			fos = new FileOutputStream(to);
			fis = new FileInputStream(from);
			byte buf[] = new byte[4096];
			int len;
			while((len = fis.read(buf)) > 0)
			{
				total += len;
				fos.write(buf, 0, len);
			}
			fos.close();
			fis.close();
		}
		finally
		{
			if (fis != null)
			{
				try { fis.close(); }
				catch(IOException ex) {}
			}
			if (fos != null)
			{
				try { fos.close(); }
				catch(IOException ex) {}
			}
		}
		return total;
	}

	/**
	 * Retrieve contents of given file as a String.
	 * 
	 * @param f The file desired.
	 * @return contents if it can be read.
	 * @throws IOException
	 */
	public static String getFileContents(File f)
		throws IOException
	{
		StringBuilder sb = new StringBuilder();
		try(FileReader fr = new FileReader(f);)
		{
			int c;
			while((c = fr.read()) != -1)
			{
				sb.append((char)c);
			}
			return sb.toString();
		}
		
	}
	
	public static byte[] getfileBytes(File f)
		throws IOException
	{
		byte ret[] = new byte[(int)f.length()];
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
		int b = 0;
		for(int idx = 0; idx < ret.length && (b = bis.read()) != -1; idx++)
			ret[idx] = (byte)b;
		bis.close();
		return ret;
	}

	/**
	  Copies an input stream to an output stream.
	  Both streams are closed after the transfer is complete.
	  @param in the intput stream
	  @param out the output stream
	*/
	public static void copyStream(InputStream in, OutputStream out)
		throws IOException
	{
		byte buffer[] = new byte[4096];
		int len;
		int total = 0;
		while((len = in.read(buffer)) >= 0)
		{
			out.write(buffer, 0, len);
			total += len;
		}
		in.close();
		out.close();
	}

	/**
	  Copies an input stream to an output stream until timeout or 
	  the End of stream is reached.
	  I/O streams are not closed after the copying completes.
	  @param in the intput stream
	  @param out the output stream
	  @param timeout time in milliseconds to timeout 
	*/
	public static void copyStream(InputStream in, OutputStream out,long timeout)
		throws IOException
	{
		byte buffer[] = new byte[4096];
		int len;
		int total = 0;
		long start_time = System.currentTimeMillis();
		long elapsed = 0L;
		
		while((len = in.read(buffer)) >= 0 && elapsed < timeout)
		{
			if (len > 0)
			{
				out.write(buffer, 0, len);
				total += len;
			}
			else // read returned 0, pause before trying again
			{
				try { Thread.sleep(100L); } catch(InterruptedException ex) {}
			}
			elapsed = System.currentTimeMillis() - start_time;
		}
	}
	

	/**
	  Moves a file.
	  @param from the file being moved
	  @param to the file we're moving to
	  @return number of bytes moved
	  @throws IOException on error.
	*/
	public static int moveFile(File from, File to)
		throws IOException
	{
		if (from.renameTo(to))
			return (int)to.length();
		int ret = copyFile(from, to);
		from.delete();
		return ret;
	}

	/**
	  Deletes a non-empty directory by recursively deleting the contents.
	  @param f the File object representing the file or directory to be deleted.
	  @return true if directory was successfully deleted.
	*/
	public static boolean deleteDir(File f) 
	{
		if (f.isDirectory()) 
		{
			String[] children = f.list();
			for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy