/* * Util.java * * Created on December 2, 2003, 6:11 PM */ package gnt; import java.io.*; import java.util.ArrayList; import java.util.Iterator; // can remove this later import palm.conduit.*; /** * * @author Mike Kirschman */ public class Util { /** Creates a new instance of Util */ public Util() { } /** Reads the next byte in the DataInputStream and returns an boolean * representation of the Boolean (UInt 8) this byte represents. */ static boolean readBoolean(java.io.DataInputStream in) throws java.io.IOException { // return true iff the next byte on the input stream is not 0x0 return in.read() != ((byte) 0); } /** Writes the next byte in the DataInputStream in the representation of a * Boolean (UInt8), based on the boolean value passed in. */ static void writeBoolean(java.io.DataOutputStream out, boolean value) throws java.io.IOException { // write 1 if value passed was true, else write 0 if (value) out.writeByte(1); else out.writeByte(0); } /** Reads the next 2 bytes in the DataInputStream and returns an integer * representation of the UInt16 these bytes represent. */ static int readUInt16(java.io.DataInputStream in) throws java.io.IOException { int bytes = 2; int result = 0; for (int i = 0; i < bytes; i++){ result = result << 8; result += (byte) in.read(); } return result; } /** Reads the next 4 bytes in the DataInputStream and returns a long * representation of the UInt32 these bytes represent. */ static long readUInt32(java.io.DataInputStream in) throws java.io.IOException { int bytes = 4; long result = 0; for (int i = 0; i < bytes; i++){ result = result << 8; result += in.read(); } return result; } /** Writes the int as the next 2 bytes in the DataOutputStream in UInt16 * representation. */ static void writeUInt16(java.io.DataOutputStream out, int value) throws java.io.IOException { byte[] bytes = new byte[2]; for (int i = 0; i < bytes.length; i++){ bytes[i] = (byte) (value & 0xF); value = value >> 8; } for (int i = bytes.length - 1; i >= 0; i--) out.writeByte(bytes[i]); } /** Writes the long as the next 4 bytes in the DataOutputStream in UInt32 * representation. */ static void writeUInt32(java.io.DataOutputStream out, long value) throws java.io.IOException { byte[] bytes = new byte[4]; for (int i = 0; i < bytes.length; i++){ bytes[i] = (byte) (value & 0xF); value = value >> 8; } for (int i = bytes.length - 1; i >= 0; i--) out.writeByte(bytes[i]); } static String[] split(String data, String delimiter) { ArrayList tokenList = new ArrayList(); int oldIdx = 0; int newIdx = data.indexOf(delimiter); // while (newIdx > -1) { while (newIdx > -1 && newIdx != oldIdx) { tokenList.add(data.substring(oldIdx, newIdx)); oldIdx = newIdx + delimiter.length(); newIdx = data.indexOf(delimiter, oldIdx); } String[] tokens = new String[tokenList.size()]; int j = 0; for (Iterator i = tokenList.iterator(); i.hasNext();) { tokens[j] = (String) i.next(); j++; } return tokens; } }