com.lts.ipc
Class Utils

java.lang.Object
  extended by com.lts.ipc.Utils

public class Utils
extends java.lang.Object

An internal class that provides utility methods common to IPC classes.

Author:
cnh

Constructor Summary
Utils()
           
 
Method Summary
static boolean checkFile(java.io.File f)
          Create a file if it does not already exist.
static void checkFile(java.lang.String name)
           
static void closeNoExceptions(java.io.BufferedReader reader)
           
static void closeNoExceptions(java.io.FileWriter writer)
           
static void closeNoExceptions(java.io.InputStream istream)
           
static void closeNoExceptions(java.io.OutputStream ostream)
           
static void closeNoExceptions(java.io.Reader reader)
           
static byte[] createAndFillWith(int size, java.lang.String pattern)
          Create a byte array that is populated with a particular string.
static void fillArrayWith(byte[] dest, byte[] pattern)
           
static void listProperties()
           
static java.lang.Object matchIgnoreCase(java.lang.Object[] values, java.lang.String s)
           
static void printEnv()
           
static java.lang.String readFile(java.io.File file)
           
static java.lang.String readFile(java.lang.String name)
           
static java.lang.String readOrCreate(java.io.File file, java.lang.String contents)
          Read or create a file.
static java.lang.String readOrCreate(java.lang.String name, java.lang.String contents)
           
static long toMilliseconds(long seconds, long nanos)
           
static long toNanoseconds(long sec, long nanos)
           
static java.lang.Object toValueIgnoreCase(java.lang.Object[] values, java.lang.String s)
          Find the enum tag that matches a string without regards to case.
static void writeFile(java.io.File file, java.lang.String contents)
           
static void writeFile(java.lang.String name, java.lang.String contents)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utils

public Utils()
Method Detail

checkFile

public static boolean checkFile(java.io.File f)
                         throws IPCException
Create a file if it does not already exist.

The IPC classes in this package all expect a file to exist that corresponds to the resource that they are manipulating. This method checks to see if the corresponding file does indeed exist on the underlying system. If it does not, then the method will try to create it.

Parameters:
name - The absolute path to the file.
Returns:
true if the method creates the file, false otherwise.
Throws:
IPCException - If the method tries to create the file but an exception is thrown. The message will be set to IPCException.Errors.ExceptionCreatingFile and the cause will be set to the offending exception.

checkFile

public static void checkFile(java.lang.String name)
                      throws IPCException
Throws:
IPCException

toMilliseconds

public static long toMilliseconds(long seconds,
                                  long nanos)

toNanoseconds

public static long toNanoseconds(long sec,
                                 long nanos)

closeNoExceptions

public static void closeNoExceptions(java.io.Reader reader)

closeNoExceptions

public static void closeNoExceptions(java.io.BufferedReader reader)

closeNoExceptions

public static void closeNoExceptions(java.io.InputStream istream)

readFile

public static java.lang.String readFile(java.io.File file)
                                 throws java.io.IOException
Throws:
java.io.IOException

readFile

public static java.lang.String readFile(java.lang.String name)
                                 throws java.io.IOException
Throws:
java.io.IOException

writeFile

public static void writeFile(java.io.File file,
                             java.lang.String contents)
                      throws java.io.IOException
Throws:
java.io.IOException

writeFile

public static void writeFile(java.lang.String name,
                             java.lang.String contents)
                      throws java.io.IOException
Throws:
java.io.IOException

closeNoExceptions

public static void closeNoExceptions(java.io.FileWriter writer)

toValueIgnoreCase

public static java.lang.Object toValueIgnoreCase(java.lang.Object[] values,
                                                 java.lang.String s)
Find the enum tag that matches a string without regards to case.

This method is intended to be used like so:

 enum Foo {
     One,Two,Three,Four;
     public static Foo toValueIgnoreCase(String s) {
         return (Foo) Utils.toValueIgnoreCase(Foo.values(), s);
     }
 }
 

Parameters:
values - The enum tag values.
s - The string we are trying to convert.
Returns:
The matching enum tag.
Throws:
java.lang.RuntimeException - If there is no match.

matchIgnoreCase

public static java.lang.Object matchIgnoreCase(java.lang.Object[] values,
                                               java.lang.String s)

closeNoExceptions

public static void closeNoExceptions(java.io.OutputStream ostream)

fillArrayWith

public static void fillArrayWith(byte[] dest,
                                 byte[] pattern)

createAndFillWith

public static byte[] createAndFillWith(int size,
                                       java.lang.String pattern)
Create a byte array that is populated with a particular string.

Useful when initializing shared memory.

Parameters:
size - The size of the returned array.
pattern - The pattern to use to populate the array.
Returns:
The populated array.

listProperties

public static void listProperties()

readOrCreate

public static java.lang.String readOrCreate(java.io.File file,
                                            java.lang.String contents)
                                     throws java.io.IOException
Read or create a file.

This method opens the named text file and returns its contents, assuming the file exists.

If the file does not exist, then it creates it and writes the supplied string into it. This operation happens in an "atomic" fashion, so that another thread or process that if another process or thread attempts to write the same file at the same time, only one of them will succeed.

Parameters:
file - The file to read or create.
contents - If the file needs to be created, the contents of the file.
Returns:
The contents of the file. If the file does not exist, this will simply return the supplied string.
Throws:
java.io.IOException - If a problem exists while trying to read or write the file.

readOrCreate

public static java.lang.String readOrCreate(java.lang.String name,
                                            java.lang.String contents)
                                     throws java.io.IOException
Throws:
java.io.IOException

printEnv

public static void printEnv()