it.geosolutions.tools.io.file
Class IOUtils

java.lang.Object
  extended by org.apache.commons.io.IOUtils
      extended by it.geosolutions.tools.io.file.IOUtils

public final class IOUtils
extends org.apache.commons.io.IOUtils

Assorted IO related utilities

Version:
0.2
Author:
Simone Giannecchini, GeoSolutions SAS, (r2)Carlo Cancellieri - carlo.cancellieri@geo-solutions.it

Field Summary
static String FILE_SEPARATOR
           
static long MAX_WAITING_TIME_FOR_LOCK
          The max time the node will wait for, prior to stop to attempt for acquiring a lock on a File.
 
Fields inherited from class org.apache.commons.io.IOUtils
DIR_SEPARATOR, DIR_SEPARATOR_UNIX, DIR_SEPARATOR_WINDOWS, LINE_SEPARATOR, LINE_SEPARATOR_UNIX, LINE_SEPARATOR_WINDOWS
 
Method Summary
static boolean acquireLock(Object caller, File inputFile)
          This method is responsible for checking if the input file is still being written or if its available for being parsed.
static boolean acquireLock(Object caller, File inputFile, long maxwait)
          This method is responsible for checking if the input file is still being written or if its available for being parsed.
static void closeQuietly(Channel channel)
          Close the specified input FileChannel
static void copyChannel(int bufferSize, ReadableByteChannel source, WritableByteChannel destination)
          Copies the content of the source channel onto the destination channel.
static void copyFileChannel(int bufferSize, FileChannel source, FileChannel destination)
          Optimize version of copy method for file channels.
static void copyStream(InputStream sourceStream, OutputStream destinationStream, boolean closeInput, boolean closeOutput)
          Copy InputStream to OutputStream.
static void copyStream(InputStream sourceStream, OutputStream destinationStream, int size, boolean closeInput, boolean closeOutput)
          Copy InputStream to OutputStream.
static File createTodayDirectory(File destDir)
          Create a subDirectory having the actual date as name, within a specified destination directory.
 
Methods inherited from class org.apache.commons.io.IOUtils
closeQuietly, closeQuietly, closeQuietly, closeQuietly, closeQuietly, closeQuietly, contentEquals, contentEquals, copy, copy, copy, copy, copy, copy, copyLarge, copyLarge, lineIterator, lineIterator, readLines, readLines, readLines, skip, skip, skipFully, skipFully, toBufferedInputStream, toByteArray, toByteArray, toByteArray, toByteArray, toByteArray, toByteArray, toCharArray, toCharArray, toCharArray, toInputStream, toInputStream, toInputStream, toInputStream, toString, toString, toString, toString, toString, toString, toString, toString, toString, write, write, write, write, write, write, write, write, write, write, write, write, write, write, write, writeLines, writeLines, writeLines
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_WAITING_TIME_FOR_LOCK

public static final long MAX_WAITING_TIME_FOR_LOCK
The max time the node will wait for, prior to stop to attempt for acquiring a lock on a File.

See Also:
Constant Field Values

FILE_SEPARATOR

public static final String FILE_SEPARATOR
Method Detail

copyChannel

public static void copyChannel(int bufferSize,
                               ReadableByteChannel source,
                               WritableByteChannel destination)
                        throws IOException
Copies the content of the source channel onto the destination channel.

Parameters:
bufferSize - size of the temp buffer to use for this copy.
source - the source ReadableByteChannel.
destination - the destination WritableByteChannel;.
Throws:
IOException - in case something bad happens.

copyFileChannel

public static void copyFileChannel(int bufferSize,
                                   FileChannel source,
                                   FileChannel destination)
                            throws IOException
Optimize version of copy method for file channels.

Parameters:
bufferSize - size of the temp buffer to use for this copy.
source - the source ReadableByteChannel.
destination - the destination WritableByteChannel;.
Throws:
IOException - in case something bad happens.

closeQuietly

public static void closeQuietly(Channel channel)
                         throws IOException
Close the specified input FileChannel

Throws:
IOException - in case something bad happens.

acquireLock

public static boolean acquireLock(Object caller,
                                  File inputFile)
                           throws InterruptedException,
                                  IOException
This method is responsible for checking if the input file is still being written or if its available for being parsed.

Specifically this method tries to open up a "rw" channel on the provided input file. If the file is still being written this operation fails with an exception, we therefore catch this exception and sleep for seconds as defined in the constant #ATOMIC_WAIT.

If after having waited for MAX_WAITING_TIME_FOR_LOCK (which is read from the configuration or set to the default value of #DEFAULT_WAITING_TIME) we have not yet acquired the channel we skip this file but we signal this situation.

Parameters:
inputFile -
Returns:
true if the lock has been successfully acquired. false otherwise
Throws:
InterruptedException
IOException

copyStream

public static void copyStream(InputStream sourceStream,
                              OutputStream destinationStream,
                              boolean closeInput,
                              boolean closeOutput)
                       throws IOException
Copy InputStream to OutputStream.

Parameters:
sourceStream - InputStream to copy from.
destinationStream - OutputStream to copy to.
closeInput - quietly close InputStream.
closeOutput - quietly close OutputStream
Throws:
IOException - in case something bad happens.

copyStream

public static void copyStream(InputStream sourceStream,
                              OutputStream destinationStream,
                              int size,
                              boolean closeInput,
                              boolean closeOutput)
                       throws IOException
Copy InputStream to OutputStream.

Parameters:
sourceStream - InputStream to copy from.
destinationStream - OutputStream to copy to.
size - size of the buffer to use internally.
closeInput - quietly close InputStream.
closeOutput - quietly close OutputStream
Throws:
IOException - in case something bad happens.

createTodayDirectory

public static File createTodayDirectory(File destDir)
Create a subDirectory having the actual date as name, within a specified destination directory.

Parameters:
destDir - the destination directory where to build the "today" directory.
Returns:
the created directory.

acquireLock

public static boolean acquireLock(Object caller,
                                  File inputFile,
                                  long maxwait)
                           throws InterruptedException,
                                  IOException
This method is responsible for checking if the input file is still being written or if its available for being parsed.

Specifically this method tries to open up a "rw" channel on the provided input file. If the file is still being written this operation fails with an exception, we therefore catch this exception and sleep for seconds as defined in the constant #ATOMIC_WAIT.

If after having waited for MAX_WAITING_TIME_FOR_LOCK (which is read from the configuration or set to the default value of #DEFAULT_WAITING_TIME) we have not yet acquired the channel we skip this file but we signal this situation. NOTE: To make use of mandatory locks, mandatory locking must be enabled both on the file system that contains the file to be locked, and on the file itself. Mandatory locking is enabled on a file system using the "-o mand" option to mount(8), or the MS_MANDLOCK flag for mount(2). Mandatory locking is enabled on a file by disabling group execute permission on the file and enabling the set-group-ID permission bit (see chmod(1) and chmod(2)).

Parameters:
caller -
inputFile -
maxwait -
Returns:
true if the lock has been successfully acquired. false otherwise
Throws:
InterruptedException
IOException


Copyright © 2011-2012 GeoSolutions. All Rights Reserved.