wyvern.common.util
Class Util

java.lang.Object
  extended bywyvern.common.util.Util

public class Util
extends java.lang.Object

A bunch of utility functions that address omissions in the Java language and class libraries, plus some extra game-specific stuff.

Version:
1.0, Mar 20, 1997
Author:
Steve Yegge

Constructor Summary
Util()
           
 
Method Summary
static boolean appendToFile(java.lang.String path, java.lang.String text)
          Appends a string to the end of a text file.
static void copyFile(java.io.File src, java.io.File dest)
          Copies a file.
static void copyFile(java.lang.String src, java.lang.String dest)
          Copies a file.
static boolean derivedFrom(java.lang.Class subClass, java.lang.String superName)
          Figure out whether one class is derived from another, where the superclass is specified by a string, so you don't have to know about it statically.
static java.awt.Image filterImage(java.awt.Image src, java.awt.image.ImageFilter filter)
          Takes the specified image and creates a grayscale version.
static java.lang.String formatColor(java.awt.Color c)
          prints out a color as a 3-item list
static java.lang.String formatPoint(java.awt.Dimension d)
          Formats a Dimension object as (x, y).
static java.lang.String formatPoint(int x, int y)
          Use this for error-checking and diagnostics.
static java.lang.String formatPoint(int x, int y, int z)
           
static java.lang.String formatPoint(java.awt.Point p)
           
static java.lang.String formatTime(long sec)
          Turns a number of seconds into a string showing the days, hours, minutes and seconds for that interval.
static java.lang.String formatTimeShort(long sec)
          Formats a time (in seconds) as hrs:min, e.g. 12:34
static java.awt.Frame getFrame(java.awt.Component c)
          Gets the top-level Frame object by searching up the tree.
static java.awt.Image loadImage(java.awt.Component c, java.lang.String name)
           
static java.awt.Image loadImage(java.lang.String name)
          Loads an image from disk.
static void main(java.lang.String[] args)
          Tests everything.
static void markBusy(java.awt.Component c)
          Mark the app as busy.
static void markNotBusy(java.awt.Component c)
          Mark the app as not busy.
static byte[] pack3DIntArray(int[][][] data)
          Packs a 3D int array into an array of bytes, such that it can be read back in by a DataInputStream wrapping a ByteArrayInputStream.
static byte[] packShortArray(short[] data)
          Packs an array of shorts into an array of bytes such that it can be read back in by a DataInputStream wrapping a ByteArrayInputStream.
static byte[] packStringArray(java.lang.String[] data)
          Turns an array of strings into an array of bytes, using a DataOutputStream wrapping a ByteArrayOutputStream.
static void print(java.lang.Object[] objs)
          Prints an object array out by calling toString() on all the elements.
static void print2DIntArray(int[][] array)
          Prints a 2D array for debugging.
static void print3DIntArray(int[][][] array)
          Prints a 3D array for debugging.
static java.lang.String printByteArray(byte[] data)
          Used for debugging - produces really ugly output.
static java.lang.String printException(java.lang.Throwable t)
          Captures an Exception backtrace into a string.
static void printIntArray(int[] array)
          Prints out an array's values.
static void printObjectArray(java.lang.Object[] array)
          Prints out an array of arbitrary objects.
static void printProperties(java.util.Properties p)
          Prints a java.util.Properties list.
static void printStringArray(java.lang.String[] array)
          Prints an array's values.
static byte[] readFile(java.lang.String path)
          Reads a file as an array of bytes.
static java.lang.String readFileCache(java.lang.String path)
          Reads the contents of a file, caching the results.
 void showImageSize(java.awt.Image i)
          Used for debugging only.
static int[][][] unpack3DIntArray(byte[] data)
          Unpacks a 3D int[][][] that was written to a byte array using a DataOutputStream and a ByteArrayOutputStream.
static short[] unpackShortArray(byte[] data)
          Unpacks an array of shorts that was packaged up by writing the values onto a DataOutputStream wrapping a ByteArrayOutputStream.
static java.lang.String[] unpackStringArray(byte[] data)
          Unpacks an array of Strings that was packaged up by writing the values onto a DataOutputStream wrapping a ByteArrayOutputStream.
static void waitForImage(java.awt.Component component, java.awt.Image image)
           
static void waitForImage(java.awt.Image image)
          Waits for an image to finish being loaded or rendered (e.g. from getScaledInstance()), by creating a component to do the waiting.
static boolean writeFile(java.lang.String path, byte[] bytes)
          Writes the contents of a file.
static boolean writeFile(java.lang.String path, java.lang.String body)
          Writes the contents of a file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Util

public Util()
Method Detail

derivedFrom

public static boolean derivedFrom(java.lang.Class subClass,
                                  java.lang.String superName)
Figure out whether one class is derived from another, where the superclass is specified by a string, so you don't have to know about it statically. I'm pretty sure Java doesn't have a builtin for this, at least in JDK 1.1.

Parameters:
subClass - a Class you want to verify derived from superName.
superName - the String name of the class you're deriving from. Needs to be the full package name (e.g. java.util.Vector).
Returns:
boolean true if subClass is subclassed from superClass param

showImageSize

public void showImageSize(java.awt.Image i)
Used for debugging only.


loadImage

public static java.awt.Image loadImage(java.lang.String name)
Loads an image from disk.

Parameters:
name - the absolute path to the image

loadImage

public static java.awt.Image loadImage(java.awt.Component c,
                                       java.lang.String name)

waitForImage

public static void waitForImage(java.awt.Component component,
                                java.awt.Image image)

waitForImage

public static void waitForImage(java.awt.Image image)
Waits for an image to finish being loaded or rendered (e.g. from getScaledInstance()), by creating a component to do the waiting.


formatPoint

public static java.lang.String formatPoint(java.awt.Dimension d)
Formats a Dimension object as (x, y).


formatPoint

public static java.lang.String formatPoint(int x,
                                           int y)
Use this for error-checking and diagnostics. It's too slow to be useful in the game.


formatPoint

public static java.lang.String formatPoint(java.awt.Point p)

formatPoint

public static java.lang.String formatPoint(int x,
                                           int y,
                                           int z)

formatColor

public static java.lang.String formatColor(java.awt.Color c)
prints out a color as a 3-item list


getFrame

public static java.awt.Frame getFrame(java.awt.Component c)
Gets the top-level Frame object by searching up the tree.

Returns:
Frame or null

markBusy

public static void markBusy(java.awt.Component c)
Mark the app as busy.


markNotBusy

public static void markNotBusy(java.awt.Component c)
Mark the app as not busy.


printIntArray

public static void printIntArray(int[] array)
Prints out an array's values.


print2DIntArray

public static void print2DIntArray(int[][] array)
Prints a 2D array for debugging. Best at printing arrays whose width is less than 40, with all values less than 100 (and negative values > -10).

Parameters:
array - any 2D int array

print3DIntArray

public static void print3DIntArray(int[][][] array)
Prints a 3D array for debugging. Best at printing arrays whose width is less than 40, with all values less than 100 (and negative values > -10).

Parameters:
array - any 3D int array

printStringArray

public static void printStringArray(java.lang.String[] array)
Prints an array's values.


printObjectArray

public static void printObjectArray(java.lang.Object[] array)
Prints out an array of arbitrary objects.


copyFile

public static void copyFile(java.lang.String src,
                            java.lang.String dest)
                     throws java.io.IOException
Copies a file.

Throws:
java.io.IOException

copyFile

public static void copyFile(java.io.File src,
                            java.io.File dest)
                     throws java.io.IOException
Copies a file.

Throws:
java.io.IOException

readFile

public static byte[] readFile(java.lang.String path)
Reads a file as an array of bytes.

Parameters:
path - the absolute path to the file
Returns:
a byte array with the file data, or null if it couldn't be read.

writeFile

public static boolean writeFile(java.lang.String path,
                                java.lang.String body)
Writes the contents of a file. The destination directory must exist already.

Parameters:
path - the absolute path to the file to write, including the filename.
body - the contents of the file
Returns:
true if successful

writeFile

public static boolean writeFile(java.lang.String path,
                                byte[] bytes)
Writes the contents of a file. Creates the directories in the path if necessary.

Parameters:
path - the absolute path to the file to write, including the filename.
bytes - the contents of the file
Returns:
true if successful

appendToFile

public static boolean appendToFile(java.lang.String path,
                                   java.lang.String text)
Appends a string to the end of a text file. Creates the file if it doesn't exist, but won't create directories.

Parameters:
path - the full path to the file to append to. Should be a text file.
text - the text to append. If you're appending multiple lines to the file, you shouldn't use this method, since it opens and writes the file on every call.
Returns:
true if we succeeded

readFileCache

public static java.lang.String readFileCache(java.lang.String path)
Reads the contents of a file, caching the results. Checks the file modification time, and if the file has been modified since it was last read,

Parameters:
path - the absolute path to the file
Returns:
the file contents.

printException

public static java.lang.String printException(java.lang.Throwable t)
Captures an Exception backtrace into a string.

Parameters:
t - any Throwable or Exception subclass
Returns:
the exception's stack trace in a string

formatTime

public static java.lang.String formatTime(long sec)
Turns a number of seconds into a string showing the days, hours, minutes and seconds for that interval.

Parameters:
sec - number of seconds in the interval
Returns:
a string showing the interval in days/hours/minutes/seconds

formatTimeShort

public static java.lang.String formatTimeShort(long sec)
Formats a time (in seconds) as hrs:min, e.g. 12:34

Parameters:
sec - the interval to format

pack3DIntArray

public static byte[] pack3DIntArray(int[][][] data)
Packs a 3D int array into an array of bytes, such that it can be read back in by a DataInputStream wrapping a ByteArrayInputStream.

Parameters:
data - the input data
Returns:
the input data, packaged into a byte array suitable for zipping up and streaming over a network.

unpack3DIntArray

public static int[][][] unpack3DIntArray(byte[] data)
Unpacks a 3D int[][][] that was written to a byte array using a DataOutputStream and a ByteArrayOutputStream.

Parameters:
data - the packed array

packShortArray

public static byte[] packShortArray(short[] data)
Packs an array of shorts into an array of bytes such that it can be read back in by a DataInputStream wrapping a ByteArrayInputStream.

Parameters:
data - an array of short values
Returns:
the packaged data

unpackShortArray

public static short[] unpackShortArray(byte[] data)
Unpacks an array of shorts that was packaged up by writing the values onto a DataOutputStream wrapping a ByteArrayOutputStream.

Parameters:
data - the byte array
Returns:
the array unpacked into an array of short values

packStringArray

public static byte[] packStringArray(java.lang.String[] data)
Turns an array of strings into an array of bytes, using a DataOutputStream wrapping a ByteArrayOutputStream.

Parameters:
data - the string array
Returns:
the strings packed into a byte array. First value in the stream is an int representing the number of strings. Strings are written as UTF values.

unpackStringArray

public static java.lang.String[] unpackStringArray(byte[] data)
Unpacks an array of Strings that was packaged up by writing the values onto a DataOutputStream wrapping a ByteArrayOutputStream.

Parameters:
data - the byte array
Returns:
the array unpacked into an array of strings

printByteArray

public static java.lang.String printByteArray(byte[] data)
Used for debugging - produces really ugly output.


print

public static void print(java.lang.Object[] objs)
Prints an object array out by calling toString() on all the elements.


printProperties

public static void printProperties(java.util.Properties p)
Prints a java.util.Properties list.


filterImage

public static java.awt.Image filterImage(java.awt.Image src,
                                         java.awt.image.ImageFilter filter)
Takes the specified image and creates a grayscale version.


main

public static void main(java.lang.String[] args)
Tests everything.