/* * Last change: $Date: 2005/03/15 00:46:38 $ * $Revision: 1.10 $ * * Copyright (c) 2004, The Black Sheep, Department of Computer Science, The University of Auckland * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of The Black Sheep, The Department of Computer Science or The University of Auckland nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package rescuecore; import java.util.*; import java.io.UnsupportedEncodingException; import rescuecore.commands.*; /** A collection of useful methods */ public class Handy { private final static String[] HEX_DIGITS = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}; private final static String CHARACTER_ENCODING = "US-ASCII"; private final static Object PRINT_LOCK = new Object(); private Handy() {} /** Print an array of bytes to System.out in a nice way @param data The bytes to print out */ public static void printBytes(byte[] data) { printBytes(null,data); } /** Print an array of bytes to System.out in a nice way @param header A string to print out as a title @param data The bytes to print out */ public static void printBytes(String header, byte[] data) { synchronized(PRINT_LOCK) { if (header!=null) System.out.println(header); System.out.println("OFFSET\tBYTES"); for (int i=0;istartIndex+j) { buffer.append(hex(data[startIndex+j])); if (j!=3) buffer.append(" "); } } buffer.append("\t"); char charValue = (char)decodeInt(data,startIndex); if (Character.isLetterOrDigit(charValue)) buffer.append(charValue); buffer.append("\t"); buffer.append(decodeInt(data,startIndex)); buffer.append("\t"); buffer.append(description); System.out.println(buffer.toString()); } } /** Turn a byte into a hexadecimal String @param b The byte to convert @return The byte in hexadecimal, as a String */ public static String hex(byte b) { byte left = (byte)((b >> 4) & 0xF); byte right = (byte)(b & 0xF); return HEX_DIGITS[left]+HEX_DIGITS[right]; } /** Decode a byte from a buffer @param buffer The buffer we are looking at @param off The offset into the buffer to start decoding from @return The next byte in the buffer */ /* public static byte decodeByte(byte[] buffer, int off) { return buffer[off]; } */ /** Decode a byte array from a buffer @param buffer The buffer we are looking at @param off The offset into the buffer to start decoding from @param length The number of bytes to read @return The next byte array in the buffer */ /* public static byte[] decodeBytes(byte[] buffer, int off, int length) { byte[] result = new byte[length]; System.arraycopy(buffer,off,result,0,length); return result; } */ /** Decode a short from a buffer @param buffer The buffer we are looking at @param off The offset into the buffer to start decoding from @return The next short in the buffer */ /* public static short decodeShort(byte[] buffer, int off) { int result = ((buffer[off] << 8) & 0x0000FF00) | (buffer[off+1] & 0x000000FF); return (short)result; } */ /** Decode an int from a buffer @param buffer The buffer we are looking at @param off The offset into the buffer to start decoding from @return The next int in the buffer */ private static int decodeInt(byte[] buffer, int off) { int result = ((buffer[off] << 24) & 0xFF000000) | ((buffer[off+1] << 16) & 0x00FF0000) | ((buffer[off+2] << 8) & 0x0000FF00) | (buffer[off+3] & 0x000000FF); return result; } /** Decode a String from a buffer @param buffer The buffer we are looking at @param off The offset into the buffer to start decoding from @param length The number of characters in the String @return The next String in the buffer */ /* public static String decodeString(byte[] buffer, int off, int length) { int realLength = Math.min(length,buffer.length-off); byte[] data = new byte[realLength]; System.arraycopy(buffer,off,data,0,realLength); try { return new String(data,CHARACTER_ENCODING); } catch (UnsupportedEncodingException e) { return new String(data); } } */ /** Encode a byte into a byte array @param value The byte to encode @return A byte array representation of the input value */ /* public static byte[] encodeByte(int value) { return new byte[] {(byte)(value & 0xFF)}; } */ /** Encode a byte into a buffer @param value The byte to encode @param buf The buffer to write the result into @param off The offset to start writing at */ /* public static void encodeByte(int value, byte[] buf, int off) { buf[off] = (byte)(value&0xFF); } */ /** Encode a byte arrray into a buffer @param bytes The byte array to encode @param buf The buffer to write the result into @param off The offset to start writing at */ /* public static void encodeBytes(byte[] bytes, byte[] buf, int off) { System.arraycopy(bytes,0,buf,off,bytes.length); } */ /** Encode part of a byte array into a buffer @param bytes The byte arrray to encode @param bytesOffset The offset into bytes to start writing from @param bytesLength The number of bytes to write @param buf The buffer to write the result into @param off The offset to start writing at */ /* public static void encodeBytes(byte[] bytes, int bytesOffset, int bytesLength, byte[] buf, int off) { System.arraycopy(bytes,bytesOffset,buf,off,bytesLength); } */ /** Encode a short into a byte array @param value The short to encode @return A byte array representation of the input value */ /* public static byte[] encodeShort(int value) { byte[] result = new byte[2]; result[0] = (byte)(value >> 8 & 0xFF); result[1] = (byte)(value & 0xFF); return result; } */ /** Encode a short into a buffer @param value The short to encode @param buf The buffer to write the result into @param off The offset to start writing at */ /* public static void encodeShort(int value, byte[] buf, int off) { buf[off] = (byte)(value >> 8 & 0xFF); buf[off+1] = (byte)(value & 0xFF); } */ /** Encode an int into a byte array @param value The int to encode @return A byte array representation of the input value */ /* public static byte[] encodeInt(int value) { byte[] result = new byte[4]; result[0] = (byte)(value >> 24 & 0xFF); result[1] = (byte)(value >> 16 & 0xFF); result[2] = (byte)(value >> 8 & 0xFF); result[3] = (byte)(value & 0xFF); return result; } */ /** Encode an int into a buffer @param value The int to encode @param buf The buffer to write the result into @param off The offset to start writing at */ /* public static void encodeInt(int value, byte[] buf, int off) { buf[off] = (byte)(value >> 24 & 0xFF); buf[off+1] = (byte)(value >> 16 & 0xFF); buf[off+2] = (byte)(value >> 8 & 0xFF); buf[off+3] = (byte)(value & 0xFF); } */ /** Encode a String into a byte array @param value The String to encode @param length The maximum number of bytes to use @return A byte array representation of the input byte */ /* public static byte[] encodeString(String value, int length) { byte[] result = new byte[length]; byte[] data; try { data = value.getBytes(CHARACTER_ENCODING); } catch (UnsupportedEncodingException e) { data = value.getBytes(); } System.arraycopy(data,0,result,0,Math.min(data.length,length)); return result; } */ /** Encode a String into a buffer @param value The String to encode @param length The maximum number of bytes to write @param buf The buffer to write the result into @param off The offset to start writing at */ /* public static void encodeString(String value, int length, byte[] buf, int off) { byte[] data; try { data = value.getBytes(CHARACTER_ENCODING); } catch (UnsupportedEncodingException e) { data = value.getBytes(); } System.arraycopy(data,0,buf,off,Math.min(data.length,length)); } */ /** Find out if two int arrays are different. The two arrays are not different if and only if they are the same size and contain the same elements (possibly out of order). @param a The first array @param b The second array @return true if and only if the two arrays are different */ /* public static boolean isDifferent(int[] a, int[] b) { if (a==null && b==null) return false; if (a==null || b==null) return true; if (a.length!=b.length) return true; int[] aSorted = new int[a.length]; int[] bSorted = new int[b.length]; System.arraycopy(a,0,aSorted,0,a.length); System.arraycopy(b,0,bSorted,0,b.length); Arrays.sort(aSorted); Arrays.sort(bSorted); for (int i=0;i0) return s.substring(0,s.length()-2); return "Unknown"; } /** Turn an array of integers into a String containing a comma-seperated list of numbers @param array The array to convert @return A human-readable String containing a comma-seperated list of numbers */ public static String arrayAsString(int[] array) { if (array==null) return "null"; StringBuffer result = new StringBuffer(); for (int i=0;i