com.aragost.javahg.internals
Class Utils

java.lang.Object
  extended by com.aragost.javahg.internals.Utils

public class Utils
extends Object

Miscellaneous utils methods for JavaHg.


Constructor Summary
Utils()
           
 
Method Summary
static
<T> T[]
arrayConcat(T[] a, T[] b)
          Concatenate two arrays.
static
<T> T[]
arrayUnshift(T first, T[] rest)
          Insert an element as the first to an array.
static RuntimeException asRuntime(Throwable e)
          Convert the specified exception into a RuntimeException
static void consumeAll(InputStream stream)
          Read and discard everything from the specified InputStream
static File createTempFile(String suffix)
          Create a temporary file.
static String decodeBytes(byte[] bytes, CharsetDecoder decoder)
          Decode the bytes with the decoder
static void deleteTempDir(File file)
          Delete a directory in the system temporary directory (java.io.tmpdir).
static String[] fileArray2StringArray(File[] files)
          Convert an array of Files to an array of Strings.
static Charset getUtf8Charset()
           
static boolean isSymlink(File file)
          Determines whether the specified file is a Symbolic Link rather than an actual file.
static boolean isWindows()
           
static String obfuscateLoginData(String line)
           
static byte[] randomBytes()
          Generate some random bytes that is printable Ascii characters.
static int readBigEndian(InputStream stream)
          Read four bytes from the stream, and return the integer represented by these bytes in big endian.
static String readStream(InputStream in, CharsetDecoder decoder)
          Read everything from the stream and return it as a String
static File resolveSymlinks(File file)
          Return a new File object where symlinks are resolved.
static File resourceAsFile(String resourceName)
          Return a File that is created from a resource name found on the classpath.
static File resourceAsFile(String resourceName, Map<String,byte[]> replacements)
          Return a File that is created from a resource name found on the classpath.
static
<T> T
single(Collection<T> coll)
          Return the single element in the specified Collection.
static File[] stringArray2FileArray(File baseDir, String[] strings)
           
static void writeBigEndian(int n, OutputStream stream)
          Write 4 bytes representing the specified integer in big-endian.
 
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

isWindows

public static boolean isWindows()
Returns:
true if the OS is Windows, otherwise false

getUtf8Charset

public static Charset getUtf8Charset()

readBigEndian

public static int readBigEndian(InputStream stream)
                         throws IOException
Read four bytes from the stream, and return the integer represented by these bytes in big endian. If EOF is reached then -1 is returned.

Parameters:
stream - the input stream
Returns:
the decoded integer.
Throws:
IOException

writeBigEndian

public static void writeBigEndian(int n,
                                  OutputStream stream)
                           throws IOException
Write 4 bytes representing the specified integer in big-endian.

Parameters:
n - the integer
stream - the output stream
Throws:
IOException

consumeAll

public static void consumeAll(InputStream stream)
                       throws IOException
Read and discard everything from the specified InputStream

Parameters:
stream -
Throws:
IOException

readStream

public static String readStream(InputStream in,
                                CharsetDecoder decoder)
Read everything from the stream and return it as a String

Parameters:
in - the input stream
decoder - the CharsetDecoder encapsulating the policy of handling errors while decoding.
Returns:
the decoded String.

decodeBytes

public static String decodeBytes(byte[] bytes,
                                 CharsetDecoder decoder)
Decode the bytes with the decoder

Parameters:
bytes -
decoder -
Returns:
the decoded bytes

arrayUnshift

public static <T> T[] arrayUnshift(T first,
                                   T[] rest)
Insert an element as the first to an array. The input array is not changed, instead a copy is returned.

Type Parameters:
T -
Parameters:
first - element to insert at position 0 in the result.
rest - elements to insert at position 1 to rest.length.
Returns:
the unshifted array of length 1 + rest.length.

arrayConcat

public static <T> T[] arrayConcat(T[] a,
                                  T[] b)
Concatenate two arrays. The input arrays are copied into the output array.

Type Parameters:
T -
Parameters:
a -
b -
Returns:
the concatenated array of length a.length + b.length.

fileArray2StringArray

public static String[] fileArray2StringArray(File[] files)
Convert an array of Files to an array of Strings.

Each file is converted with the getPath() method.

Parameters:
files -
Returns:
array of file names as Strings

stringArray2FileArray

public static File[] stringArray2FileArray(File baseDir,
                                           String[] strings)
Parameters:
baseDir -
strings -
Returns:
array of File objects

resolveSymlinks

public static File resolveSymlinks(File file)
Return a new File object where symlinks are resolved.

Parameters:
file -
Returns:
the canonical file name

single

public static <T> T single(Collection<T> coll)
Return the single element in the specified Collection.

If the collection is empty null is return

Parameters:
coll -
Returns:
null or the singleton element in the collection
Throws:
IllegalArgumentException - if the collection has more than one element

createTempFile

public static File createTempFile(String suffix)
Create a temporary file.

This method is identical to File.createTempFile(String, String) except it throws a RuntimeIOException.

Parameters:
suffix -
Returns:
the temporary file

isSymlink

public static boolean isSymlink(File file)
Determines whether the specified file is a Symbolic Link rather than an actual file.

Will not return true if there is a Symbolic Link anywhere in the path, only if the specific file is. Note: The implementation is adapted from the similar method in Apache Commons IO

Parameters:
file - the file to check
Returns:
true if the file is a Symbolic Link

asRuntime

public static RuntimeException asRuntime(Throwable e)
Convert the specified exception into a RuntimeException

Parameters:
e -
Returns:
the RuntimeException

deleteTempDir

public static void deleteTempDir(File file)
                          throws IOException
Delete a directory in the system temporary directory (java.io.tmpdir).

Throws:
IOException

resourceAsFile

public static File resourceAsFile(String resourceName)
Return a File that is created from a resource name found on the classpath. The resource is written into the File the first time it's needed.

Parameters:
resourceName -
Returns:
the File
Throws:
IOException

resourceAsFile

public static File resourceAsFile(String resourceName,
                                  Map<String,byte[]> replacements)
Return a File that is created from a resource name found on the classpath. The resource is written into the File the first time it's needed.

Patterns of the form %{name} will be replace with the corresponding value from the replacement map.

Parameters:
resourceName -
Returns:
the File
Throws:
IOException

randomBytes

public static byte[] randomBytes()
Generate some random bytes that is printable Ascii characters.

They will be used in a Mercurial style file. To make it easy to use the bytes in the style file the characters '"' and '\' will not be part of the byte array.

Returns:

obfuscateLoginData

public static String obfuscateLoginData(String line)


Copyright © 2011-2013 aragost Trifork ag. All Rights Reserved.