jsh.shell
Class Utils

java.lang.Object
  extended by jsh.shell.Utils

public class Utils
extends java.lang.Object

The Java Shell: Utility pack.

Author:
Romain Guy., Osvaldo Pinali Doederlein.

Constructor Summary
Utils()
           
 
Method Summary
static boolean beginsWithRoot(java.lang.String path)
          It can be necessary to determine if a path begins with a root.
static java.lang.String constructPath(java.lang.String change)
          Constructs a new path from current user path.
static java.lang.String fmtBytes(long bytes)
          Formats a number of bytes for output.
static java.lang.String fmtTime(long ms)
          Formats time for output.
static java.lang.String getRoot(java.lang.String path)
          It can be necessary to determine which is the root of a path.
static java.lang.String getShortStringOf(java.lang.String longString, int maxLength)
          Sometimes, Strings are too long to be correctly displayed.
static int getSize(java.util.Enumeration enu)
          Counts things in an Enumeration (and destroys it as a side effect).
static java.lang.String getSpaces(int len)
          Return a String made of spaces.
static java.lang.String getUserDirectory()
          Returns the user current directory.
static java.lang.String[] getWildCardMatches(java.lang.String s, boolean sort)
          When the user has to specify file names, he can use wildcards (*, ?).
 boolean isHidden(java.io.File file)
          We override a Java2 spcific method.
static java.io.File[] listFiles(java.lang.String[] names, boolean construct)
          Because a lot of people still use JDK 1.1, we need this method to create an array of Files from an array of String.
static java.io.File[] listRoots(java.io.File f)
          We override a Java2 specific method.
static boolean match(java.lang.String pattern, java.lang.String string)
          This method can determine if a String matches a pattern of wildcards
static void setUserDir(java.lang.String newPath)
           
static void sortStrings(java.lang.String[] strings)
          Quick sort an array of Strings.
static void sortStrings(java.lang.String[] a, int lo0, int hi0)
          Quick sort an array of Strings.
static java.lang.String[] subarray(java.lang.String[] arr, int first)
          Removes some leading elements from a String array.
static java.lang.String[] subarray(java.lang.String[] arr, int first, int last)
          Removes some elements from a String array.
static void swap(java.lang.String[] a, int i, int j)
          Swaps two Strings.
static java.lang.String[] toArray(java.util.Vector strings)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utils

public Utils()
Method Detail

subarray

public static java.lang.String[] subarray(java.lang.String[] arr,
                                          int first,
                                          int last)
Removes some elements from a String array.

Parameters:
arr - The array.
first - First element to keep (new 0-index).
last - Last element to keep.
Returns:
Copy of arr[first..]

subarray

public static java.lang.String[] subarray(java.lang.String[] arr,
                                          int first)
Removes some leading elements from a String array.

Parameters:
arr - The array.
first - First element to keep (new 0-index).
Returns:
Copy of arr[first..]

fmtBytes

public static java.lang.String fmtBytes(long bytes)
Formats a number of bytes for output.

Parameters:
bytes - Number of bytes.
Returns:
"xxxxK" form.

fmtTime

public static java.lang.String fmtTime(long ms)
Formats time for output.

Parameters:
bytes - Number of milliseconds.
Returns:
"x,yyys" form.

getSpaces

public static java.lang.String getSpaces(int len)
Return a String made of spaces.

Parameters:
len - Number of spaces

toArray

public static java.lang.String[] toArray(java.util.Vector strings)

getWildCardMatches

public static java.lang.String[] getWildCardMatches(java.lang.String s,
                                                    boolean sort)
When the user has to specify file names, he can use wildcards (*, ?). This methods handles the usage of these wildcards.

Parameters:
s - Wilcards
sort - Set to true will sort file names
Returns:
An array of String which contains all files matching s in current directory.
See Also:
match(String, String)

match

public static boolean match(java.lang.String pattern,
                            java.lang.String string)
This method can determine if a String matches a pattern of wildcards

Parameters:
pattern - The pattern used for comparison
string - The String to be checked
Returns:
true if string matches pattern
See Also:
Utils#getWildCardMatches(String)

sortStrings

public static void sortStrings(java.lang.String[] strings)
Quick sort an array of Strings.

Parameters:
string - Strings to be sorted

sortStrings

public static void sortStrings(java.lang.String[] a,
                               int lo0,
                               int hi0)
Quick sort an array of Strings.

Parameters:
a - Strings to be sorted
lo0 - Lower bound
hi0 - Higher bound

swap

public static void swap(java.lang.String[] a,
                        int i,
                        int j)
Swaps two Strings.

Parameters:
a - The array to be swapped
i - First String index
j - Second String index

getUserDirectory

public static java.lang.String getUserDirectory()
Returns the user current directory.


getShortStringOf

public static java.lang.String getShortStringOf(java.lang.String longString,
                                                int maxLength)
Sometimes, Strings are too long to be correctly displayed. This method will reduce a String, keeping first and last characters.

Parameters:
longString - The String to be modified
maxLength - The maximum length of the String
Returns:
A shorter String

listFiles

public static java.io.File[] listFiles(java.lang.String[] names,
                                       boolean construct)
Because a lot of people still use JDK 1.1, we need this method to create an array of Files from an array of String.

Parameters:
names - Names of the files
construct - Set it to true if names does not contain full paths
Returns:
An array of Files

getSize

public static int getSize(java.util.Enumeration enu)
Counts things in an Enumeration (and destroys it as a side effect).

Parameters:
enum - The enumeration, in the start position.
Returns:
Elements found.

constructPath

public static java.lang.String constructPath(java.lang.String change)
Constructs a new path from current user path. This is an easy way to get a path if the user specified, for example, "..\Java" as new path. This method will return the argument if this one is a path to a root (i.e, if change is equal to C:\Jdk, constructPath will return C:\Jdk).

Parameters:
change - The modification to apply to the path
See Also:
Utils#beginsWithRoot(String), Utils#getRoot(String)

getRoot

public static java.lang.String getRoot(java.lang.String path)
It can be necessary to determine which is the root of a path. For example, the root of D:\Projects\Java is D:\.

Parameters:
path - The path used to get a root
Returns:
The root which contais the specified path

beginsWithRoot

public static boolean beginsWithRoot(java.lang.String path)
It can be necessary to determine if a path begins with a root.

Parameters:
path - The path to check
Returns:
True if path begins with a root, false otherwise

listRoots

public static java.io.File[] listRoots(java.io.File f)
We override a Java2 specific method.

Parameters:
f - A File
Returns:
A list of standards roots

isHidden

public boolean isHidden(java.io.File file)
We override a Java2 spcific method.

Parameters:
file - Determine if this file is hidden or not
Returns:
Always false

setUserDir

public static void setUserDir(java.lang.String newPath)