All Packages  Class Hierarchy  This Package  Previous  Next  Index
  Class Acme.Utils
java.lang.Object
   |
   +----Acme.Utils
  -  public class Utils
  
-  extends Object
  
Assorted static utility routines.
 
 Whenever I come up with a static routine that might be of general use,
 I put it here.  So far the class includes:
 
 -  some string routines that were left out of java.lang.String
 
 -  a general array-to-string routine
 
 -  a fixed version of java.io.InputStream's byte-array read routine
 
 -  a bunch of URL-hacking routines
 
 -  some easy-to-use wrappers for Runtime.exec
 
 -  a debugging routine to dump the current call stack
 
 -  a URLDecoder to match java.net.URLEncoder
 
 
 and lots more.
 
 Fetch the software.
 Fetch the entire Acme package.
  
  -  
	INT_DAY
   -  
  
 -  
	INT_DECADE
   -  
  
 -  
	INT_HOUR
   -  
  
 -  
	INT_MINUTE
   -  
  
 -  
	INT_MONTH
   -  
  
 -  
	INT_SECOND
   -  
  
 -  
	INT_WEEK
   -  
  
 -  
	INT_YEAR
   -  
 
  
  -  
	Utils()
   -  
 
  
  -  
	absoluteUrlStr(String, URL)
   -  
  
 -  
	arraycontains(Object[], Object)
   -  Check if an array contains a given element.
  
 -  
	arrayToString(Object)
   -  An array-to-String routine.
  
 -  
	base64Encode(byte[])
   -  A base-64 encoder, necessary for doing the client side of Basic
 Authentication.
  
 -  
	base64Encode(String)
   -  A base-64 encoder that takes a String, for convenience.
  
 -  
	baseUrlStr(String)
   -  Figure out the base URL for a given URL.
  
 -  
	charCount(String, char)
   -  Returns the number of times the given character appears in the string.
  
 -  
	copyStream(InputStream, OutputStream)
   -  Copy the input to the output until EOF.
  
 -  
	copyStream(InputStream, Writer)
   -  Copy the input to the output until EOF.
  
 -  
	copyStream(Reader, OutputStream)
   -  Copy the input to the output until EOF.
  
 -  
	copyStream(Reader, Writer)
   -  Copy the input to the output until EOF.
  
 -  
	countOnes(byte)
   -  Count the number of 1-bits in a byte.
  
 -  
	countOnes(int)
   -  Count the number of 1-bits in an int.
  
 -  
	countOnes(long)
   -  Count the number of 1-bits in a long.
  
 -  
	dumpStack()
   -  Dump out the current call stack onto System.err.
  
 -  
	dumpStack(PrintStream)
   -  Dump out the current call stack.
  
 -  
	equalsStrings(String[], String[])
   -  Compares two arrays of Strings for equality.
  
 -  
	even(long)
   -  Test is a number is even.
  
 -  
	fixDirUrlStr(String)
   -  Makes sure if a URL is a directory, it ends with a slash.
  
 -  
	flattenStrarr(String[])
   -  Turns an array of Strings into a single String, with the components
 separated by spaces.
  
 -  
	indexOfString(String[], String)
   -  Locates a String in an array of Strings.
  
 -  
	indexOfStringIgnoreCase(String[], String)
   -  Locates a String in an array of Strings, ignoring case.
  
 -  
	instanceOf(Object, Class)
   -  Check if an object extends a given class or one of its superclasses.
  
 -  
	intervalStr(long)
   -  Returns a string approximately describing a given time interval.
  
 -  
	lsDateStr(Date)
   -  Returns a date string formatted in Unix ls style - if it's within
 six months of now, Mmm dd hh:ss, else Mmm dd  yyyy.
  
 -  
	match(String, String)
   -  Checks whether a string matches a given wildcard pattern.
  
 -  
	odd(long)
   -  Test is a number is odd.
  
 -  
	parseInt(String, int)
   -  Parse an integer, returning a default value on errors.
  
 -  
	parseLong(String, long)
   -  Parse a long, returning a default value on errors.
  
 -  
	plainUrl(String)
   -  Make a URL with no ref part and no query string.
  
 -  
	plainUrl(URL, String)
   -  Make a URL with no ref part and no query string.
  
 -  
	pluralStr(long)
   -  Returns "s" for numbers other than one, and "" for one.
  
 -  
	popenr(String)
   -  Run a program on the host system, and capture the output.
  
 -  
	popenw(String)
   -  Run a program on the host system, and send it some input.
  
 -  
	pow(long, long)
   -  Returns the number a raised to the power of b.
  
 -  
	read(InputStream, byte[], int, int)
   -  A fixed version of java.io.InputStream.read(byte[], int, int).
  
 -  
	readFully(InputStream, byte[], int, int)
   -  A version of read that reads the entire requested block, instead
 of sometimes terminating early.
  
 -  
	runCommand(String)
   -  Run a program on the host system.
  
 -  
	sameSpan(String, String)
   -  Returns the length of the initial segment of str1 that equals str2.
  
 -  
	sortStrings(String[])
   -  Sorts an array of Strings.
  
 -  
	splitStr(String)
   -  Turns a String into an array of Strings, by using StringTokenizer
 to split it up at whitespace.
  
 -  
	splitStr(String, char)
   -  Turns a String into an array of Strings, by splitting it at
 the specified character.
  
 -  
	strCSpan(String, String)
   -  Returns the length of the initial segment of str which consists
 entirely of characters NOT from charSet.
  
 -  
	strCSpan(String, String, int)
   -  Returns the length of the initial segment of str which consists
 entirely of characters NOT from charSet, starting at the given index.
  
 -  
	strSpan(String, String)
   -  Returns the length of the initial segment of str which consists
 entirely of characters from charSet.
  
 -  
	strSpan(String, String, int)
   -  Returns the length of the initial segment of str which consists
 entirely of characters from charSet, starting at the given index.
  
 -  
	system(String)
   -  Run a program on the host system.
  
 -  
	urlDecoder(String)
   -  URLDecoder to go along with java.net.URLEncoder.
  
 -  
	urlStrIsAbsolute(String)
   -  
  
 -  
	urlStrIsDir(String)
   -  Figures out whether a URL points to a directory or not.
 
  
INT_SECOND
 public static final long INT_SECOND
INT_MINUTE
 public static final long INT_MINUTE
INT_HOUR
 public static final long INT_HOUR
INT_DAY
 public static final long INT_DAY
INT_WEEK
 public static final long INT_WEEK
INT_MONTH
 public static final long INT_MONTH
INT_YEAR
 public static final long INT_YEAR
INT_DECADE
 public static final long INT_DECADE
  
Utils
 public Utils()
  
lsDateStr
 public static String lsDateStr(Date date)
  -  Returns a date string formatted in Unix ls style - if it's within
 six months of now, Mmm dd hh:ss, else Mmm dd  yyyy.
 
pluralStr
 public static String pluralStr(long n)
  -  Returns "s" for numbers other than one, and "" for one.
 
intervalStr
 public static String intervalStr(long interval)
  -  Returns a string approximately describing a given time interval.
  
    -  Parameters:
    
 -  interval - the interval, in milliseconds
  
 
 
 
strSpan
 public static int strSpan(String str,
                           String charSet)
  -  Returns the length of the initial segment of str which consists
 entirely of characters from charSet.
 
strSpan
 public static int strSpan(String str,
                           String charSet,
                           int fromIdx)
  -  Returns the length of the initial segment of str which consists
 entirely of characters from charSet, starting at the given index.
 
strCSpan
 public static int strCSpan(String str,
                            String charSet)
  -  Returns the length of the initial segment of str which consists
 entirely of characters NOT from charSet.
 
strCSpan
 public static int strCSpan(String str,
                            String charSet,
                            int fromIdx)
  -  Returns the length of the initial segment of str which consists
 entirely of characters NOT from charSet, starting at the given index.
 
match
 public static boolean match(String pattern,
                             String string)
  -  Checks whether a string matches a given wildcard pattern.
 Only does ? and *, and multiple patterns separated by |.
 
sameSpan
 public static int sameSpan(String str1,
                            String str2)
  -  Returns the length of the initial segment of str1 that equals str2.
 
charCount
 public static int charCount(String str,
                             char c)
  -  Returns the number of times the given character appears in the string.
 
splitStr
 public static String[] splitStr(String str)
  -  Turns a String into an array of Strings, by using StringTokenizer
 to split it up at whitespace.
 
splitStr
 public static String[] splitStr(String str,
                                 char delim)
  -  Turns a String into an array of Strings, by splitting it at
 the specified character.  This does not use StringTokenizer,
 and therefore can handle empty fields.
 
flattenStrarr
 public static String flattenStrarr(String strs[])
  -  Turns an array of Strings into a single String, with the components
 separated by spaces.
 
sortStrings
 public static void sortStrings(String strings[])
  -  Sorts an array of Strings.
 Java currently has no general sort function.  Sorting Strings is
 common enough that it's worth making a special case.
 
indexOfString
 public static int indexOfString(String strings[],
                                 String string)
  -  Locates a String in an array of Strings.
 Returns -1 if the String is not found.
 
indexOfStringIgnoreCase
 public static int indexOfStringIgnoreCase(String strings[],
                                           String string)
  -  Locates a String in an array of Strings, ignoring case.
 Returns -1 if the String is not found.
 
equalsStrings
 public static boolean equalsStrings(String strings1[],
                                     String strings2[])
  -  Compares two arrays of Strings for equality.
 
pow
 public static long pow(long a,
                        long b) throws ArithmeticException
  -  Returns the number a raised to the power of b.  Long version
 of Math.pow().  Throws ArithmeticException if b is negative.
 
parseInt
 public static int parseInt(String str,
                            int def)
  -  Parse an integer, returning a default value on errors.
 
parseLong
 public static long parseLong(String str,
                              long def)
  -  Parse a long, returning a default value on errors.
 
arrayToString
 public static String arrayToString(Object o)
  -  An array-to-String routine.  Handles arrays of arbitrary
 type, including nested arrays.  Sample output:
 
 byte[]:    { (byte)0, (byte)1, (byte)2 }
 char[]:    { '0', '1', '2' }
 short[]:   { (short)0, (short)1, (short)2 }
 int[]:     { 0, 1, 2 }
 long[]:    { 0L, 1L, 2L }
 float[]:   { 0F, 1F, 2F }
 double[]:  { 0D, 1D, 2D }
 String[]:  { "0", "1", "2" }
 int[][]:   { { 0, 1, 2 }, { 3, 4, 5 } }
 
 
instanceOf
 public static boolean instanceOf(Object o,
                                  Class cl)
  -  Check if an object extends a given class or one of its superclasses.
 An instanceof that works on Class objects at runtime, instead
 of type descriptors at compile time.
 
even
 public static boolean even(long n)
  -  Test is a number is even.
 
odd
 public static boolean odd(long n)
  -  Test is a number is odd.
 
countOnes
 public static int countOnes(byte n)
  -  Count the number of 1-bits in a byte.
 
countOnes
 public static int countOnes(int n)
  -  Count the number of 1-bits in an int.
 
countOnes
 public static int countOnes(long n)
  -  Count the number of 1-bits in a long.
 
read
 public static int read(InputStream in,
                        byte b[],
                        int off,
                        int len) throws IOException
  -  A fixed version of java.io.InputStream.read(byte[], int, int).  The
 standard version catches and ignores IOExceptions from below.
 This version sends them on to the caller.
 
readFully
 public static int readFully(InputStream in,
                             byte b[],
                             int off,
                             int len) throws IOException
  -  A version of read that reads the entire requested block, instead
 of sometimes terminating early.
  
    -  Returns:
    
 -  -1 on EOF, otherwise len
  
 
 
 
plainUrl
 public static URL plainUrl(URL context,
                            String urlStr) throws MalformedURLException
  -  Make a URL with no ref part and no query string.  Also, if it's
 a directory then make sure there's a trailing slash.
 
plainUrl
 public static URL plainUrl(String urlStr) throws MalformedURLException
  -  Make a URL with no ref part and no query string.  Also, if it's
 a directory then make sure there's a trailing slash.
 
baseUrlStr
 public static String baseUrlStr(String urlStr)
  -  Figure out the base URL for a given URL.  What this means is
 if the URL points to a directory, you get that directory; if the
 URL points to a file, you get the directory the file is in.
 
fixDirUrlStr
 public static String fixDirUrlStr(String urlStr)
  -  Makes sure if a URL is a directory, it ends with a slash.
 
urlStrIsDir
 public static boolean urlStrIsDir(String urlStr)
  -  Figures out whether a URL points to a directory or not.
 Web servers are lenient and accept directory-URLs without
 the trailing slash.  What they actually do is return a
 redirect to the same URL with the trailing slash appended.
 Unfortunately, Java doesn't let us see that such a redirect
 happened.  Instead we have to figure out it's a directory
 indirectly and heuristically.
 
urlStrIsAbsolute
 public static boolean urlStrIsAbsolute(String urlStr)
absoluteUrlStr
 public static String absoluteUrlStr(String urlStr,
                                     URL contextUrl) throws MalformedURLException
urlDecoder
 public static String urlDecoder(String encoded)
  -  URLDecoder to go along with java.net.URLEncoder.  Why there isn't
 already a decoder in the standard library is a mystery to me.
 
base64Encode
 public static String base64Encode(byte src[])
  -  A base-64 encoder, necessary for doing the client side of Basic
 Authentication.  This encodes binary data as printable ASCII
 characters.  Three 8-bit binary bytes are turned into four 6-bit
 values, like so:
   [11111111]  [22222222]  [33333333]
   [111111] [112222] [222233] [333333]
 Then the 6-bit values are represented using the characters "A-Za-z0-9+/".
 
base64Encode
 public static String base64Encode(String srcString)
  -  A base-64 encoder that takes a String, for convenience.
 
arraycontains
 public static boolean arraycontains(Object array[],
                                     Object element)
  -  Check if an array contains a given element.
 
system
 public static int system(String cmd)
  -  Run a program on the host system.
 
 This routine runs the specified command, waits for it to
 finish, and returns the exit status.
 This is like the Unix system() routine.  Unlike the Unix version,
 though, stdout and stderr get thrown away unless you redirect them.
 
popenr
 public static InputStream popenr(String cmd)
  -  Run a program on the host system, and capture the output.
 
 This routine runs the specified command, and returns an InputStream
 for reading the output of the program.
 
 WARNING: In JDK1.0.2 there is a serious bug in the process
 IO routines, such that reading all the way to the end of a process's
 output will invariably get you an IOException( "read error" ).
 In some cases you will also lose the last bufferload of
 the output.  The workaround is to add a " ; sleep 1" to the end of
 your command, and to ignore the "read error" IOException.
 
popenw
 public static OutputStream popenw(String cmd)
  -  Run a program on the host system, and send it some input.
 
 This routine runs the specified command, and returns an OutputStream
 for writing the program's input.
 
runCommand
 public static Process runCommand(String cmd) throws IOException
  -  Run a program on the host system.
 
 This routine runs the specified command, and returns a Process
 object so you can do what you like with it.
 
 WARNING: In JDK1.0.2 there is a serious bug in the process
 IO routines, such that reading all the way to the end of a process's
 output will invariably get you an IOException( "read error" ).
 In some cases you will also lose the last bufferload of
 the output.  The workaround is to add a " ; sleep 1" to the end of
 your command, and to ignore the "read error" IOException.
 
copyStream
 public static void copyStream(InputStream in,
                               OutputStream out) throws IOException
  -  Copy the input to the output until EOF.
 
copyStream
 public static void copyStream(Reader in,
                               Writer out) throws IOException
  -  Copy the input to the output until EOF.
 
copyStream
 public static void copyStream(InputStream in,
                               Writer out) throws IOException
  -  Copy the input to the output until EOF.
 
copyStream
 public static void copyStream(Reader in,
                               OutputStream out) throws IOException
  -  Copy the input to the output until EOF.
 
dumpStack
 public static void dumpStack(PrintStream p)
  -  Dump out the current call stack.
 
dumpStack
 public static void dumpStack()
  -  Dump out the current call stack onto System.err.
 
All Packages  Class Hierarchy  This Package  Previous  Next  Index
ACME Java  ACME Labs