1 package com.ubermq.util; 2 3 import EDU.oswego.cs.dl.util.concurrent.*; 4 import java.io.*; 5 import java.nio.*; 6 import java.nio.charset.*; 7 import java.text.*; 8 import java.util.*; 9 import org.apache.log4j.*; 10 11 14 public class Utility 15 { 16 private static final Random random; 18 19 private static final Logger log; 21 22 private static SynchronizedInt nextInt = new SynchronizedInt(0); 24 private static SynchronizedLong nextLong = new SynchronizedLong(0); 25 26 public static final String ENCODING_NAME = "UTF-8"; 28 private static final Charset encoding; 29 private static final CharsetEncoder encoder; 30 private static final CharsetDecoder decoder; 31 32 33 static 35 { 36 log = Logger.getLogger("com.ubermq"); 38 39 try 41 { 42 random = new Random(); 43 } catch(Exception x) { 44 log.fatal("", x); 45 throw new Error (); 46 } 47 48 encoding = Charset.forName(ENCODING_NAME); 50 encoder = encoding.newEncoder(); 51 decoder = encoding.newDecoder(); 52 53 encoder.onMalformedInput(CodingErrorAction.REPLACE); 55 decoder.onMalformedInput(CodingErrorAction.REPLACE); 56 } 57 58 62 public static void outputBuffer(ByteBuffer b) 63 { 64 System.out.println(displayBuffer(b)); 65 } 66 67 72 public static String displayBuffer(ByteBuffer b) 73 { 74 ByteBuffer bb = b.duplicate(); 75 StringBuffer sz = new StringBuffer (); 76 77 int n=0; 78 bb.rewind(); 79 while(bb.hasRemaining()) 80 { 81 int v = bb.get() & 0xFF; 82 sz.append(((v <= 15) ? "0" : "") + Integer.toHexString(v).toUpperCase() + " "); 83 if (0 == (++n % 16)) sz.append("\n"); 84 } 85 86 return sz.toString(); 87 } 88 89 95 public static int allocateLocallyUniqueInt() 96 { 97 return nextInt.increment(); 98 } 99 100 106 public synchronized static long allocateLocallyUniqueLong() 107 { 108 return nextLong.increment(); 109 } 110 111 117 public static long allocateGloballyUniqueLong() 118 { 119 return ( (long)(random.nextInt() << 32) + (int)(System.currentTimeMillis() & 0xFFFFFFFF)); 120 } 121 122 130 public static void encode(String sz, ByteBuffer out) 131 { 132 synchronized(encoder) 133 { 134 encoder.reset(); 135 if (encoder.encode(CharBuffer.wrap(sz), out, true) == CoderResult.OVERFLOW) 136 throw new BufferOverflowException(); 137 if (encoder.flush(out) == CoderResult.OVERFLOW) 138 throw new BufferOverflowException(); 139 } 140 } 141 142 150 public static ByteBuffer encode(String sz) 151 { 152 synchronized(encoder) 157 { 158 try 159 { 160 return encoder.encode(CharBuffer.wrap(sz)); 161 } 162 catch (CharacterCodingException e) { 163 return ByteBuffer.allocate(0); 164 } 165 } 166 } 167 168 176 public static CharBuffer decode(ByteBuffer szb) 177 { 178 synchronized(decoder) 183 { 184 try 185 { 186 return decoder.decode(szb); 187 } 188 catch(CharacterCodingException cee) 189 { 190 getLogger().error("", cee); 191 return CharBuffer.allocate(0); 192 } 193 } 194 } 195 196 public static final String escapeForHTML(String text) 197 { 198 return text.replaceAll("&", "&") 199 .replaceAll("<", "<") 200 .replaceAll(">", ">") 201 .replaceAll("\"", """); 202 } 203 204 210 public static final Logger getLogger() 211 { 212 return log; 213 } 214 215 219 private static class UberFormatter 220 extends java.util.logging.Formatter 221 { 222 private static final DateFormat df = new SimpleDateFormat("dd MMM yy HH:mm:ss.SSS"); 223 224 238 public String format(java.util.logging.LogRecord record) 239 { 240 String className = record.getSourceClassName(); 241 242 StringBuffer fmt = new StringBuffer (); 243 fmt.append(record.getLevel()); 244 fmt.append(" "); 245 synchronized(df) { 246 fmt.append(df.format(new Date(record.getMillis()))); 247 } 248 fmt.append(" "); 249 fmt.append(className.substring(className.lastIndexOf('.')+1)); 250 fmt.append("."); 251 fmt.append(record.getSourceMethodName()); 252 fmt.append(" "); 253 254 if (record.getThrown() != null) { 255 StringWriter sw = new StringWriter(); 256 record.getThrown().printStackTrace(new PrintWriter(sw)); 257 fmt.append(sw.toString()); 258 } else { 259 fmt.append(record.getMessage()); 260 } 261 262 fmt.append("\n"); 263 264 return fmt.toString(); 265 } 266 } 267 } 268 | Popular Tags |