ch.claudio.lib
Class Util

java.lang.Object
  extended by ch.claudio.lib.Util

public class Util
extends java.lang.Object

Version:
$Id: Util.java 278 2008-07-06 12:46:20Z claudio $
Author:
Claudio Nieder

Copyright (C) 2004-2008 Claudio Nieder <private@claudio.ch>, CH-8610 Uster

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA


Field Summary
static long day
          Number of milliseconds in a day.
static long hour
          Number of milliseconds in a hour.
static java.text.DateFormat isoDate
          Time in ISO format with just the date, e.g.
static java.text.DateFormat isoDateTime
          Time in ISO format with seconds resolution, e.g.
static java.text.DateFormat isoDateTimeMin
          Time in ISO format with minute resolution, e.g.
static long minute
          Number of milliseconds in a minute.
static long second
          Number of milliseconds in a second.
static long week
          Number of milliseconds in a week.
 
Method Summary
static byte[] base64ToByte(java.lang.String data)
          From a base 64 representation, returns the corresponding byte[]
static java.lang.String bytes2asc(byte[] bytes, int offset, int length)
          Return an ascii string containing just the visible characters of the byte array.
static java.lang.String byteToBase64(byte[] data)
          From a byte[] returns a base 64 representation
static void delay(long ms)
          Delay procedure which calls sleep ignoring any interrupt.
static java.lang.String dumpBytes(byte[] bytes)
          Return a hex and text dump of the byte array
static int exec(java.lang.String[] command, java.lang.String input, java.lang.StringBuffer output, int outputSize, long timeout)
          Execute a unix command with a timeout.
static byte[] getHash(int iterations, java.lang.String password, byte[] salt)
          From a password, a number of iterations and a salt, returns the corresponding digest
static java.lang.String groupThree(java.lang.String number)
          Reformat string so that e.g.
static java.lang.String join(java.lang.String separator, java.util.List<?> parts)
          Join all elements of a string list to one string with separator in between each element.
static java.lang.String join(java.lang.String separator, java.lang.Object[] parts)
          Join all elements of a string array to one string with separator in between each element.
static java.lang.String joinByteArray(java.lang.String separator, java.util.List<byte[]> parts)
          Join all elements of a string list to one string with separator in between each element.
static java.lang.Object loadMessageClass(java.lang.Class<?> baseClass, java.util.Locale locale)
          Give a base class and a locale this method tries to find a class most appropriate.
static void main(java.lang.String[] args)
          Print license.
static byte[] readAndAdjustImage(java.io.File pictureFile, int maxDimension, boolean highQuality)
          Reads a picture from a file and converts it to a jpeg byte array.
static java.net.URI relativeURI(java.net.URI reference, java.net.URI target)
          Create a relative URI if possible.
static void replaceIfChanged(java.io.File newFile, java.io.File oldFile)
          This function is intended to help to modify a file "only if needed".
static java.net.URL stringToURL(java.lang.String s)
          Creat an URL object out of a string without throwing an exception.
static java.net.URL stringToURL(java.net.URL base, java.lang.String s)
          Creat an URL object out of a base url and a string "relative" to the base url without throwing an exception.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

isoDate

public static final java.text.DateFormat isoDate
Time in ISO format with just the date, e.g. 2006-07-17


isoDateTime

public static final java.text.DateFormat isoDateTime
Time in ISO format with seconds resolution, e.g. 2006-07-17 10:31:47


isoDateTimeMin

public static final java.text.DateFormat isoDateTimeMin
Time in ISO format with minute resolution, e.g. 2006-07-17 10:31


second

public static final long second
Number of milliseconds in a second.

See Also:
Constant Field Values

minute

public static final long minute
Number of milliseconds in a minute.

See Also:
Constant Field Values

hour

public static final long hour
Number of milliseconds in a hour.

See Also:
Constant Field Values

day

public static final long day
Number of milliseconds in a day.

See Also:
Constant Field Values

week

public static final long week
Number of milliseconds in a week.

See Also:
Constant Field Values
Method Detail

bytes2asc

public static java.lang.String bytes2asc(byte[] bytes,
                                         int offset,
                                         int length)
Return an ascii string containing just the visible characters of the byte array. The other characters are replaced by an underline character.

Parameters:
bytes - byte array
offset - start
length - length
Returns:
ascii dump

dumpBytes

public static java.lang.String dumpBytes(byte[] bytes)
Return a hex and text dump of the byte array

Parameters:
bytes - byte array
Returns:
hex dump

join

public static java.lang.String join(java.lang.String separator,
                                    java.lang.Object[] parts)
Join all elements of a string array to one string with separator in between each element.

Parameters:
separator -
parts -
Returns:
concatenated string

join

public static java.lang.String join(java.lang.String separator,
                                    java.util.List<?> parts)
Join all elements of a string list to one string with separator in between each element.

Parameters:
separator -
parts -
Returns:
concatenated string

joinByteArray

public static java.lang.String joinByteArray(java.lang.String separator,
                                             java.util.List<byte[]> parts)
Join all elements of a string list to one string with separator in between each element.

Parameters:
separator -
parts -
Returns:
concatenated string

delay

public static void delay(long ms)
Delay procedure which calls sleep ignoring any interrupt. Thus it might return much earlier than planned.

Parameters:
ms - Miliseconds to wait

exec

public static int exec(java.lang.String[] command,
                       java.lang.String input,
                       java.lang.StringBuffer output,
                       int outputSize,
                       long timeout)
Execute a unix command with a timeout. stdout and stderr are collected. stdin can be supplied through a string.

Note: This method creates a temporary buffer of the size supplied by outputSize.

Parameters:
command - Command to execute with all arguments
input - If non-null this content is supplied to the stdin of the process.
output - Buffer to which stdout and stderr are written.
outputSize - Maximal number of characters needed of the output.
timeout - maximum execution time if a positive value.
Returns:
return code

stringToURL

public static java.net.URL stringToURL(java.lang.String s)
Creat an URL object out of a string without throwing an exception. If the URL is malformed, then null will be returned. This is useful for declaring final static URLs out of constant string which you know is correct.

Parameters:
s - url in string
Returns:
url or null

stringToURL

public static java.net.URL stringToURL(java.net.URL base,
                                       java.lang.String s)
Creat an URL object out of a base url and a string "relative" to the base url without throwing an exception. If the URL is malformed, then null will be returned. This is useful for declaring final static URLs out of constant string which you know is correct.

Parameters:
base - base
s - path relative to base url
Returns:
url or null

relativeURI

public static java.net.URI relativeURI(java.net.URI reference,
                                       java.net.URI target)
Create a relative URI if possible. In contrast to URI.relativize it will use ../ is possible, thus reference needs not to be a prefix of the target URI. Also port numbers are ignored.

Parameters:
reference -
target -
Returns:
relative URI if possible

readAndAdjustImage

public static byte[] readAndAdjustImage(java.io.File pictureFile,
                                        int maxDimension,
                                        boolean highQuality)
                                 throws java.io.IOException
Reads a picture from a file and converts it to a jpeg byte array. Resizes the picture so that the larger of height or width corresponds to maxDimension.

Parameters:
pictureFile - file containing a picture
maxDimension - maximal height or length
highQuality - if true a better but slower algorithm is used to scale
Returns:
jpeg byte array
Throws:
java.io.IOException

byteToBase64

public static java.lang.String byteToBase64(byte[] data)
From a byte[] returns a base 64 representation

Parameters:
data - byte[]
Returns:
String

base64ToByte

public static byte[] base64ToByte(java.lang.String data)
                           throws java.io.IOException
From a base 64 representation, returns the corresponding byte[]

Parameters:
data - String The base64 representation
Returns:
byte[]
Throws:
java.io.IOException

getHash

public static byte[] getHash(int iterations,
                             java.lang.String password,
                             byte[] salt)
                      throws java.security.NoSuchAlgorithmException
From a password, a number of iterations and a salt, returns the corresponding digest

Parameters:
iterations - int The number of iterations of the algorithm
password - String The password to encrypt
salt - byte[] The salt
Returns:
byte[] The digested password
Throws:
java.security.NoSuchAlgorithmException - If the algorithm doesn't exist

loadMessageClass

public static java.lang.Object loadMessageClass(java.lang.Class<?> baseClass,
                                                java.util.Locale locale)
Give a base class and a locale this method tries to find a class most appropriate. It starts looking for a class who's name is composed of the base name class and the locale name. If none is found, than the locale name is shortened one character by one until a match is found.

Only classes which are actually subclasses of the base class are taken. I.e. if the base class is example.Messages and the locale is de_CH and a class example.Messages_de_CH exists, but is not a subclass of example.Messages, then it is ignored.

Parameters:
locale -
baseClass - Base message class
Returns:
appropriate message class or null if no subclass was found.

replaceIfChanged

public static final void replaceIfChanged(java.io.File newFile,
                                          java.io.File oldFile)
This function is intended to help to modify a file "only if needed". The application first writes data to a temporary file. Then calls this method with a File object for the temporary file and a File object containing the target name. This method will test if the target already exists, and if it is the same. If this is the case, the temporary file is deleted. Otherwise the temporary file will replace the existing one.

Such an approach makes sense, if one does not want to blindly overwrite a file, but keep the existing one with existing time stamp if the newly created file does not introduce any change.

Note: Assumes that the new file was created in the same directory as the old one, so that rename will always work.

Note: Does not guard against modifications of the file between check and rename.

Parameters:
newFile - The temporary new file
oldFile - The file which shall be replaced by the temporary one

groupThree

public static java.lang.String groupThree(java.lang.String number)
Reformat string so that e.g. 12345 becomes 12'345.

Parameters:
number - non-negative without leading or trailing spaces
Returns:
number with group marks

main

public static void main(java.lang.String[] args)
Print license.

Parameters:
args - ignored