to.etc.util
Class FileTool

java.lang.Object
  extended by to.etc.util.FileTool

public class FileTool
extends java.lang.Object

Contains some often-used file subroutines.

Version:
1.0
Author:
Frits Jalvingh

Method Summary
static void closeAll(java.lang.Object... list)
          This attempts to close all of the resources passed to it, without throwing exceptions.
static void compareDirectories(IDirectoryDelta delta, java.io.File a, java.io.File b)
          Compare the content of two directories, and callback methods on changes.
static java.io.InputStream copyAndDumpStream(java.lang.StringBuilder tgt, java.io.InputStream in, java.lang.String encoding)
           
static void copyDir(java.io.File destd, java.io.File srcd)
          Copies an entire directory structure from src to dest.
static void copyFile(java.io.File destf, java.io.File srcf)
          Copies a file.
static void copyFile(java.io.OutputStream os, java.io.InputStream is)
          Copies the inputstream to the output stream.
static void copyFile(java.io.Writer w, java.io.Reader r)
          Copy the input reader to the output reader.
static void copyResource(java.io.Writer w, java.lang.Class<?> cl, java.lang.String rid)
           
static void deleteDir(java.io.File f)
          Delete the directory and all it's contents.
static boolean dirEmpty(java.io.File dirf)
          Deletes all files in the directory.
static boolean dirEmpty(java.io.File dirf, java.util.Vector<java.lang.Object> elogb)
          Deprecated. 
static java.lang.String fileNameSansExtension(java.lang.String fn)
          Returns the file name excluding the suffix of the name.
static int findFilenameExtension(java.lang.String fn)
          Returns the start position of the filename extension in the string.
static java.lang.String getFileExtension(java.lang.String fn)
          Returns the extension of a file.
static java.io.File getTmpDir()
          Returns the java.io.tmpdir directory.
static java.io.InputStream getZipContent(java.io.File src, java.lang.String name)
           
static java.io.InputStream getZipContent(java.io.InputStream zipis, java.lang.String name)
          Returns a stream which is the uncompressed data stream for a zip file component.
static byte[] hashBuffers(byte[][] data)
          Create an MD5 hash for a buffer set.
static java.lang.String hashBuffersHex(byte[][] data)
          Create a HEX MD5 hash for a buffer set.
static byte[] hashFile(java.io.File f)
          Create an MD5 hash for a file's contents.
static byte[] hashFile(java.io.InputStream is)
          Hashes all data from an input stream and returns an MD5 hash.
static java.lang.String hashFileHex(java.io.File f)
          Hash a file and return it's hex MD5hash.
static java.lang.String hashFileHex(java.io.InputStream is)
          Hash an InputStream and return it's hex MD5hash.
static byte[][] loadByteBuffers(java.io.File in)
          Load an entire file in a byte buffer set.
static byte[][] loadByteBuffers(java.io.InputStream is)
          Loads a byte[][] from an input stream until exhaustion.
static java.util.Properties loadProperties(java.io.File f)
          Load a file as a Properties file.
static java.util.Properties loadPropertiesFromZip(java.io.File f, java.lang.String name)
          Opens the jar file and tries to load the plugin.properties file from it.
static java.util.Properties loadPropertiesFromZip(java.io.InputStream is, java.lang.String name)
          Opens the jar file and tries to load the plugin.properties file from it.
static java.lang.Object loadSerialized(java.io.File f)
          Load a single serialized object from a file.
static java.lang.Object loadSerialized(java.io.InputStream is)
          Load a single serialized object from a datastream.
static java.lang.Object loadSerializedNullOnError(java.io.File f)
           
static java.lang.Object loadSerializedNullOnError(java.io.InputStream is)
          Load a serialized object, and return null on any load exception.
static org.w3c.dom.Document loadXmlFromZip(java.io.File f, java.lang.String name, boolean nsaware)
          Opens the jar file and tries to load the plugin.properties file from it.
static org.w3c.dom.Document loadXmlFromZip(java.io.InputStream is, java.lang.String ident, java.lang.String name, boolean nsaware)
          Opens the jar file and tries to load the plugin.properties file from it.
static java.lang.ClassLoader makeJarLoader(java.io.File f)
          Creates a classloader to load data from the given jar file.
static java.lang.ClassLoader makeJarLoader(java.io.File f, java.lang.ClassLoader parent)
           
static java.io.File makeTempFile(java.io.File root)
          Create a temp file within the specified root directory.
static java.io.File newDir(java.io.File root)
          Create a temp directory within the root directory.
static void prepareDir(java.io.File dir)
          prepare a directory in this way: if it does not exist, create it.
static void readFileAsString(java.lang.Appendable o, java.io.File f)
           
static java.lang.String readFileAsString(java.io.File f)
          Read a file's contents in a string using the default encoding of the platform.
static java.lang.String readFileAsString(java.io.File f, java.lang.String encoding)
          Read a file into a string using the specified encoding.
static void readHeadAndTail(java.lang.StringBuffer sb, java.io.File f, int headsize, int tailsize)
          mbp, moved here from old DaemonBase with some adaptions.
static int readInt(java.io.InputStream is)
          Reads a 4-byte bigendian int off the connection.
static long readLong(java.io.InputStream is)
           
static void readStreamAsString(java.lang.Appendable o, java.io.InputStream f, java.lang.String enc)
           
static void readStreamAsString(java.lang.Appendable o, java.io.Reader r)
           
static java.lang.String readStreamAsString(java.io.InputStream is, java.lang.String enc)
           
static java.lang.String readStreamAsString(java.io.Reader r)
           
static java.lang.String readString(java.io.InputStream is)
           
static void save(java.io.File of, byte[][] data)
          Save the data in byte buffers to a file.
static void save(java.io.OutputStream os, byte[][] data)
          Save the data in byte buffers to an output stream.
static void saveProperties(java.io.File f, java.util.Properties p)
          Save a properties file.
static void saveSerialized(java.io.File f, java.io.Serializable obj)
           
static void saveSerialized(java.io.OutputStream os, java.io.Serializable obj)
          Save a serializable object to a datastream.
static void unzip(java.io.File dest, java.io.File zipfile)
          Unzip the contents of the zipfile to the directory.
static void unzip(java.io.File dest, java.io.InputStream is)
           
static java.io.InputStream wrapInputStream(java.io.InputStream rawStream, ILogSink s, int maxinmemory)
           
static void writeFileFromString(java.io.File f, java.lang.String v, java.lang.String enc)
           
static void writeFileFromString(java.io.OutputStream os, java.lang.String v, java.lang.String enc)
           
static void writeInt(java.io.OutputStream os, int val)
          Sends an int fragment
static void writeLong(java.io.OutputStream os, long val)
          Sends a long
static void writeString(java.io.OutputStream os, java.lang.String s)
           
static void zip(java.io.File zipfile, java.io.File dir)
          Zip the contents of dir or file to the zipfile.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getTmpDir

public static java.io.File getTmpDir()
Returns the java.io.tmpdir directory. Throws an exception if it does not exist or is inaccessible.

Returns:

newDir

public static java.io.File newDir(java.io.File root)
Create a temp directory within the root directory.

Parameters:
root -
Returns:

makeTempFile

public static java.io.File makeTempFile(java.io.File root)
Create a temp file within the specified root directory.

Parameters:
root -
Returns:

dirEmpty

public static boolean dirEmpty(java.io.File dirf)
Deletes all files in the directory. It skips errors and tries to delete as much as possible. If elogb is not null then all errors are written there.


deleteDir

public static void deleteDir(java.io.File f)
Delete the directory and all it's contents.

Parameters:
f -

prepareDir

public static void prepareDir(java.io.File dir)
                       throws java.lang.Exception
prepare a directory in this way: if it does not exist, create it. if it does exist then delete all files from the dir.

Parameters:
dir - the directory that must be made existent
Throws:
java.lang.Exception - when creation fails or when removing old contents fails.

dirEmpty

@Deprecated
public static boolean dirEmpty(java.io.File dirf,
                                          java.util.Vector<java.lang.Object> elogb)
Deprecated. 

Deletes all files in the directory. It skips errors and tries to delete as much as possible. If elogb is not null then all errors are written there.


getFileExtension

public static java.lang.String getFileExtension(java.lang.String fn)
Returns the extension of a file. The extension DOES NOT INCLUDE the . If no extension is present then the empty string is returned ("").


findFilenameExtension

public static int findFilenameExtension(java.lang.String fn)
Returns the start position of the filename extension in the string. If the string has no extension then this returns -1.


fileNameSansExtension

public static java.lang.String fileNameSansExtension(java.lang.String fn)
Returns the file name excluding the suffix of the name. So test.java returns test.


copyFile

public static void copyFile(java.io.File destf,
                            java.io.File srcf)
                     throws java.io.IOException
Copies a file.

Parameters:
destf - the destination
srcf - the source
Throws:
java.io.IOException - the error

copyFile

public static void copyFile(java.io.OutputStream os,
                            java.io.InputStream is)
                     throws java.io.IOException
Copies the inputstream to the output stream.

Parameters:
destf - the destination
srcf - the source
Throws:
java.io.IOException - the error

copyFile

public static void copyFile(java.io.Writer w,
                            java.io.Reader r)
                     throws java.io.IOException
Copy the input reader to the output reader.

Parameters:
w -
r -
Throws:
java.io.IOException

copyDir

public static void copyDir(java.io.File destd,
                           java.io.File srcd)
                    throws java.io.IOException
Copies an entire directory structure from src to dest. This copies the files from src into destd; it does not remove files in destd that are not in srcd. Use synchronizeDir() for that.

Parameters:
destd -
srcd -
Throws:
java.io.IOException

readFileAsString

public static java.lang.String readFileAsString(java.io.File f)
                                         throws java.lang.Exception
Read a file's contents in a string using the default encoding of the platform.

Parameters:
f -
Returns:
Throws:
java.lang.Exception

readFileAsString

public static void readFileAsString(java.lang.Appendable o,
                                    java.io.File f)
                             throws java.lang.Exception
Parameters:
o -
f -
Throws:
java.lang.Exception

readFileAsString

public static java.lang.String readFileAsString(java.io.File f,
                                                java.lang.String encoding)
                                         throws java.lang.Exception
Read a file into a string using the specified encoding.

Parameters:
f -
encoding -
Returns:
Throws:
java.lang.Exception

readHeadAndTail

public static void readHeadAndTail(java.lang.StringBuffer sb,
                                   java.io.File f,
                                   int headsize,
                                   int tailsize)
                            throws java.lang.Exception
mbp, moved here from old DaemonBase with some adaptions. Reads the head and tail lines of a text file into the stringbuffer. The number of lines in the head is at most "headsize". If this count is exceeded the read lines will be placed in a circular string buffer of size "tailsize", and appended to the stringbuffer when the whole file has been processed. Note that if headsize plus tailsize exceeds the actual number of lines, this means that the whole file will be placed in the stringbuffer. Intended use is to mail (excerpts from) logfiles from Daemon processes.

Throws:
java.lang.Exception

readStreamAsString

public static java.lang.String readStreamAsString(java.io.InputStream is,
                                                  java.lang.String enc)
                                           throws java.lang.Exception
Throws:
java.lang.Exception

readStreamAsString

public static void readStreamAsString(java.lang.Appendable o,
                                      java.io.InputStream f,
                                      java.lang.String enc)
                               throws java.lang.Exception
Throws:
java.lang.Exception

readStreamAsString

public static void readStreamAsString(java.lang.Appendable o,
                                      java.io.Reader r)
                               throws java.lang.Exception
Throws:
java.lang.Exception

readStreamAsString

public static java.lang.String readStreamAsString(java.io.Reader r)
                                           throws java.lang.Exception
Throws:
java.lang.Exception

writeFileFromString

public static void writeFileFromString(java.io.File f,
                                       java.lang.String v,
                                       java.lang.String enc)
                                throws java.lang.Exception
Throws:
java.lang.Exception

writeFileFromString

public static void writeFileFromString(java.io.OutputStream os,
                                       java.lang.String v,
                                       java.lang.String enc)
                                throws java.lang.Exception
Throws:
java.lang.Exception

hashFile

public static byte[] hashFile(java.io.File f)
                       throws java.io.IOException
Create an MD5 hash for a file's contents.

Throws:
java.io.IOException

hashBuffers

public static byte[] hashBuffers(byte[][] data)
Create an MD5 hash for a buffer set.

Parameters:
data -
Returns:

hashBuffersHex

public static java.lang.String hashBuffersHex(byte[][] data)
Create a HEX MD5 hash for a buffer set.

Parameters:
data -
Returns:

hashFile

public static byte[] hashFile(java.io.InputStream is)
                       throws java.io.IOException
Hashes all data from an input stream and returns an MD5 hash.

Parameters:
is - The stream to read and hash.
Returns:
A hash (16 bytes MD5)
Throws:
java.io.IOException

hashFileHex

public static java.lang.String hashFileHex(java.io.File f)
                                    throws java.io.IOException
Hash a file and return it's hex MD5hash.

Parameters:
f -
Returns:
Throws:
java.io.IOException

hashFileHex

public static java.lang.String hashFileHex(java.io.InputStream is)
                                    throws java.io.IOException
Hash an InputStream and return it's hex MD5hash.

Parameters:
is -
Returns:
Throws:
java.io.IOException

loadProperties

public static java.util.Properties loadProperties(java.io.File f)
                                           throws java.lang.Exception
Load a file as a Properties file.

Parameters:
f -
Returns:
Throws:
java.lang.Exception

saveProperties

public static void saveProperties(java.io.File f,
                                  java.util.Properties p)
                           throws java.lang.Exception
Save a properties file.

Parameters:
f -
p -
Throws:
java.lang.Exception

loadPropertiesFromZip

public static java.util.Properties loadPropertiesFromZip(java.io.File f,
                                                         java.lang.String name)
                                                  throws java.lang.Exception
Opens the jar file and tries to load the plugin.properties file from it.

Parameters:
f -
Returns:
Throws:
java.lang.Exception

loadPropertiesFromZip

public static java.util.Properties loadPropertiesFromZip(java.io.InputStream is,
                                                         java.lang.String name)
                                                  throws java.lang.Exception
Opens the jar file and tries to load the plugin.properties file from it.

Parameters:
f -
Returns:
Throws:
java.lang.Exception

loadXmlFromZip

public static org.w3c.dom.Document loadXmlFromZip(java.io.File f,
                                                  java.lang.String name,
                                                  boolean nsaware)
                                           throws java.lang.Exception
Opens the jar file and tries to load the plugin.properties file from it.

Parameters:
f -
Returns:
Throws:
java.lang.Exception

loadXmlFromZip

public static org.w3c.dom.Document loadXmlFromZip(java.io.InputStream is,
                                                  java.lang.String ident,
                                                  java.lang.String name,
                                                  boolean nsaware)
                                           throws java.lang.Exception
Opens the jar file and tries to load the plugin.properties file from it.

Parameters:
f -
Returns:
Throws:
java.lang.Exception

makeJarLoader

public static java.lang.ClassLoader makeJarLoader(java.io.File f)
                                           throws java.net.MalformedURLException
Creates a classloader to load data from the given jar file.

Parameters:
f -
Returns:
Throws:
java.net.MalformedURLException
java.lang.Exception

makeJarLoader

public static java.lang.ClassLoader makeJarLoader(java.io.File f,
                                                  java.lang.ClassLoader parent)
                                           throws java.net.MalformedURLException
Throws:
java.net.MalformedURLException

copyResource

public static void copyResource(java.io.Writer w,
                                java.lang.Class<?> cl,
                                java.lang.String rid)
                         throws java.lang.Exception
Throws:
java.lang.Exception

zip

public static void zip(java.io.File zipfile,
                       java.io.File dir)
                throws java.lang.Exception
Zip the contents of dir or file to the zipfile. The zipfile is deleted before the new contents are added to it.

Throws:
java.lang.Exception

unzip

public static void unzip(java.io.File dest,
                         java.io.InputStream is)
                  throws java.lang.Exception
Throws:
java.lang.Exception

unzip

public static void unzip(java.io.File dest,
                         java.io.File zipfile)
                  throws java.lang.Exception
Unzip the contents of the zipfile to the directory. The directory is created if it does not yet exist.

Throws:
java.lang.Exception

getZipContent

public static java.io.InputStream getZipContent(java.io.File src,
                                                java.lang.String name)
                                         throws java.io.IOException
Throws:
java.io.IOException

getZipContent

public static java.io.InputStream getZipContent(java.io.InputStream zipis,
                                                java.lang.String name)
                                         throws java.io.IOException
Returns a stream which is the uncompressed data stream for a zip file component.

Parameters:
zipis -
Returns:
Throws:
java.io.IOException

loadByteBuffers

public static byte[][] loadByteBuffers(java.io.InputStream is)
                                throws java.io.IOException
Loads a byte[][] from an input stream until exhaustion.

Parameters:
is -
Returns:
Throws:
java.io.IOException

loadByteBuffers

public static byte[][] loadByteBuffers(java.io.File in)
                                throws java.io.IOException
Load an entire file in a byte buffer set.

Parameters:
in -
Returns:
Throws:
java.io.IOException

save

public static void save(java.io.File of,
                        byte[][] data)
                 throws java.io.IOException
Save the data in byte buffers to a file.

Parameters:
of -
data -
Throws:
java.io.IOException

save

public static void save(java.io.OutputStream os,
                        byte[][] data)
                 throws java.io.IOException
Save the data in byte buffers to an output stream.

Parameters:
os -
data -
Throws:
java.io.IOException

writeInt

public static void writeInt(java.io.OutputStream os,
                            int val)
                     throws java.io.IOException
Sends an int fragment

Parameters:
val -
Throws:
java.lang.Exception
java.io.IOException

writeLong

public static void writeLong(java.io.OutputStream os,
                             long val)
                      throws java.io.IOException
Sends a long

Parameters:
val -
Throws:
java.lang.Exception
java.io.IOException

writeString

public static void writeString(java.io.OutputStream os,
                               java.lang.String s)
                        throws java.io.IOException
Throws:
java.io.IOException

readInt

public static int readInt(java.io.InputStream is)
                   throws java.io.IOException
Reads a 4-byte bigendian int off the connection.

Returns:
Throws:
java.lang.Exception
java.io.IOException

readLong

public static long readLong(java.io.InputStream is)
                     throws java.io.IOException
Throws:
java.io.IOException

readString

public static java.lang.String readString(java.io.InputStream is)
                                   throws java.io.IOException
Throws:
java.io.IOException

wrapInputStream

public static java.io.InputStream wrapInputStream(java.io.InputStream rawStream,
                                                  ILogSink s,
                                                  int maxinmemory)
                                           throws java.lang.Exception
Throws:
java.lang.Exception

copyAndDumpStream

public static java.io.InputStream copyAndDumpStream(java.lang.StringBuilder tgt,
                                                    java.io.InputStream in,
                                                    java.lang.String encoding)
                                             throws java.io.IOException
Throws:
java.io.IOException

saveSerialized

public static void saveSerialized(@WillClose
                                  java.io.OutputStream os,
                                  java.io.Serializable obj)
                           throws java.io.IOException
Save a serializable object to a datastream.

Parameters:
os -
obj -
Throws:
java.io.IOException

saveSerialized

public static void saveSerialized(java.io.File f,
                                  java.io.Serializable obj)
                           throws java.io.IOException
Throws:
java.io.IOException

loadSerialized

@Nullable
public static java.lang.Object loadSerialized(@WillNotClose
                                                       java.io.InputStream is)
                                       throws java.io.IOException,
                                              java.lang.ClassNotFoundException
Load a single serialized object from a datastream.

Parameters:
is -
Returns:
Throws:
java.io.IOException
java.lang.ClassNotFoundException

loadSerialized

@Nullable
public static java.lang.Object loadSerialized(java.io.File f)
                                       throws java.io.IOException,
                                              java.lang.ClassNotFoundException
Load a single serialized object from a file.

Parameters:
f -
Returns:
Throws:
java.io.IOException
java.lang.ClassNotFoundException

loadSerializedNullOnError

@Nullable
public static java.lang.Object loadSerializedNullOnError(@WillNotClose
                                                                  java.io.InputStream is)
Load a serialized object, and return null on any load exception.

Parameters:
is -
Returns:

loadSerializedNullOnError

@Nullable
public static java.lang.Object loadSerializedNullOnError(java.io.File f)
                                                  throws java.io.IOException,
                                                         java.lang.ClassNotFoundException
Throws:
java.io.IOException
java.lang.ClassNotFoundException

closeAll

public static void closeAll(@WillClose
                            java.lang.Object... list)
This attempts to close all of the resources passed to it, without throwing exceptions. It is meant to be used from finally clauses. Please take care: objects that require a succesful close (like writers or outputstreams) should NOT be closed by this method! They must be closed using a normal close within the exception handler. This list can also contain File objects; these files/directories will be deleted.

Parameters:
list -

compareDirectories

public static void compareDirectories(IDirectoryDelta delta,
                                      java.io.File a,
                                      java.io.File b)
                               throws java.lang.Exception
Compare the content of two directories, and callback methods on changes. The callbacks define what needs to be done to change "a" (old) into "b" (new).

Throws:
java.lang.Exception