package org.gametrack.util; import java.text.SimpleDateFormat; import java.util.*; /** *

Title: BFTracker

*

Description: Tracks online Battlefield 1942 games, outputs game records

*

Copyright: Copyright (c) 2003

*

Company: bf1942.gametrack.org

* @author Brian Cairns * @version 1.0 */ // routines for formatting output public class Formatting { private static SimpleDateFormat timestampFormat = new SimpleDateFormat( "yyMMdd:HHmmss.SSS" ); private static Date dateTemp = new Date(); // used for abbreviateFileSize() private final static String[] stPostfix = { "bytes", "KB", "MB", "GB", "TB" }; private final static int DEFAULT_PRECISION = 1; // private final static NumberFormat nf = NumberFormat.getInstance(); private static float pFactor = ( float )Math.pow( 10, DEFAULT_PRECISION ); /** * Returns a timestamp for the given time * @param time the given time * @return the formatted timestamp */ public static String timestamp( long time ) { synchronized( dateTemp ) { dateTemp.setTime( time ); return timestampFormat.format( dateTemp ); } } public static String timestamp() { return timestamp( System.currentTimeMillis() ); } /** * Round to the given number of places * @param val the value to round * @param places the number of places * @return the rounded number */ public static float round( double val, int places ) { double factor = Math.pow( 10, places ); return ( float )( Math.round(val*factor) / factor ); } public static long roundToNearest( long val, int factor ) { return Math.round( val / ( double )factor ) * factor; } /** * Converts an array to a string * @param arr the array * @return output as {a1,a2,a3...} */ public static String arrayToString( byte[] arr ) { if( arr == null ) return "NULL"; if( arr.length == 0 ) return "{}"; StringBuffer buf = new StringBuffer( "{" ); for( int i = 0; i < arr.length; i++ ) buf.append( arr[i] ).append( i + 1 == arr.length ? '}' : ',' ); return buf.toString(); } /** * Converts an array to a string * @param arr the array * @return output as {a1,a2,a3...} */ public static String arrayToString( Object[] arr ) { if( arr == null ) return "NULL"; if( arr.length == 0 ) return "{}"; StringBuffer buf = new StringBuffer( "{" ); for( int i = 0; i < arr.length; i++ ) buf.append( arr[i].toString() ).append( i + 1 == arr.length ? '}' : ',' ); return buf.toString(); } /** * Repeat a string * @param str the string to repeat * @param count number of times to repeat * @return the string repeated count times */ public static String repeat( String str, int count ) { if( str == null ) return null; if( count == 0 ) return ""; StringBuffer buf = new StringBuffer( str ); for( int i = 1; i < count; i++ ) buf.append( str ); return buf.toString(); } /** * Prints a list, with tabbed columns * @param strs the List * @param cols number of tabbed columns */ public static void printList( List strs, int cols ) { for( int i = 0; i < strs.size(); i+=cols ) for ( int c = 0; c < cols; c++ ) { try { System.out.print( strs.get( i + c ) + ( c + 1 == cols ? "\n" : "\t" ) ); } catch( ArrayIndexOutOfBoundsException ex ) { System.out.println( "" + ( c + 1 == cols ? "\n" : "\t" ) ); } } } /** * Right justify text within a certain fieldspace * @param s the text * @param cols number of columns * @return the String, right justified */ public static String rightJustify( String s, int cols ) { return rightJustify( s, cols, " " ); } /** * Right justification with custom filler string * @param s * @param cols * @param filler * @return */ public static String rightJustify( String s, int cols, String filler ) { if( s == null ) s = ""; return repeat( filler, cols - s.length() ) + s; } /** * Left justification with custom filler string * @param s * @param cols * @param filler * @return */ public static String leftJustify( String s, int cols, String filler ) { if( s == null ) s = ""; return s + repeat( filler, cols - s.length() ); } public static String abbreviateFileSize( long bytes ) { // abbreviates a file size to a "human readable" string (ie "12.3 KB") float flValue = ( float )bytes; int iStep = 0; // check if this is a simple small byte count if( flValue < 1024.0 ) return String.valueOf( Math.round( flValue ) ) + " " + stPostfix[ 0 ]; // ok, reduce it until it's under 1024 while( flValue >= 1024.0 ) { flValue /= 1024.0; iStep++; } //round it to the right number of decimal places try{ flValue = ( float )Math.round( flValue * pFactor ) / pFactor; } catch( ArithmeticException x ){ flValue = ( float )Math.round( flValue * pFactor ); } // convert to string, add the postfix, and return return String.valueOf( flValue ) + " " + stPostfix[ iStep ]; } public static String leftJustify( String s, int cols ) { return leftJustify( s, cols, " " ); } public static String centreJustify( String s, int cols ) { return centreJustify( s, cols, " " ); } public static String centreJustify( String s, int cols, String filler ) { if( s == null ) s = ""; int left = ( cols - s.length() ) / 2; return repeat( filler, left ) + s + repeat( filler, cols - left - s.length() ); } public static void hexDump( byte[] data, int cols ) { hexDump( data, 0, data.length, cols ); } public static void hexDump( byte[] data, int offset, int len, int cols ) { System.out.println( len ); StringBuffer buf = new StringBuffer( len * 5 ); StringBuffer charBuf = new StringBuffer( cols ); int read = offset, i; byte b; while( read < ( len + offset ) ) { for( i = 0; i < cols; i++ ) { if( read < len ) { b = data[read++]; Debug.appendHex( b, buf ); // buf.append( Debug.byte2Hex( b ) ); charBuf.append( b == 0 ? '.' : (char)b ); } else buf.append( "--" ); buf.append( " " ); } buf.append( "| " ).append( charBuf ).append( "\n" ); charBuf.setLength( 0 ); } Log.direct( buf.toString() ); } }