1 3 23 package org.xsocket; 24 25 26 import java.io.UnsupportedEncodingException ; 27 import java.nio.ByteBuffer ; 28 import java.nio.CharBuffer ; 29 import java.nio.charset.CharacterCodingException ; 30 import java.nio.charset.Charset ; 31 import java.nio.charset.CharsetDecoder ; 32 import java.nio.charset.CharsetEncoder ; 33 import java.text.SimpleDateFormat ; 34 import java.util.ArrayList ; 35 import java.util.Date ; 36 import java.util.HashMap ; 37 import java.util.List ; 38 import java.util.Map ; 39 40 41 42 47 public final class DataConverter { 48 49 private static final Map <String , CharsetEncoder > encoders = new HashMap <String , CharsetEncoder >(); 50 private static final Map <String , CharsetDecoder > decoders = new HashMap <String , CharsetDecoder >(); 51 52 53 59 public static String toFormatedBytesSize(long bytes) { 60 if (bytes > (5 * 1000 * 1000)) { 61 return (bytes/1000000) + " mb"; 62 63 } else if (bytes > (10 * 1000)) { 64 return (bytes/1000) + " kb"; 65 66 } else { 67 return bytes + " bytes"; 68 } 69 } 70 71 72 78 public static String toFormatedDate(long time) { 79 return new SimpleDateFormat ("MMM.dd HH:mm").format(new Date (time)); 80 } 81 82 88 public static String toFormatedDuration(long duration) { 89 90 if (duration < 5 * 1000) { 91 return duration + " millis"; 92 93 } else if (duration < (60 * 1000)) { 94 return ((int) (duration / 1000)) + " sec"; 95 96 } else if (duration < (60 * 60 * 1000)) { 97 return ((int) (duration / (60* 1000))) + " min"; 98 } else { 99 return ((int) (duration / (60 * 60* 1000))) + " h"; 100 } 101 } 102 103 104 111 public static ByteBuffer toByteBuffer(String s, String encoding) { 112 CharsetEncoder encoder = encoders.get(encoding); 113 if (encoder == null) { 114 Charset charset = Charset.forName(encoding); 115 if (charset != null) { 116 encoder = charset.newEncoder(); 117 encoders.put(encoding, encoder); 118 decoders.put(encoding, charset.newDecoder()); 119 } else { 120 return null; 121 } 122 } 123 124 try { 125 return encoder.encode(CharBuffer.wrap(s)); 126 } catch (CharacterCodingException cce) { 127 throw new RuntimeException (cce); 128 } 129 } 130 131 132 139 public static String toString(ByteBuffer buffer) throws UnsupportedEncodingException { 140 return toString(buffer, "UTF-8"); 141 } 142 143 144 151 public static String toString(ByteBuffer [] buffer) throws UnsupportedEncodingException { 152 return toString(buffer, "UTF-8"); 153 } 154 155 156 163 public static String toString(ByteBuffer buffer, String encoding) throws UnsupportedEncodingException { 164 try { 165 CharsetDecoder decoder = decoders.get(encoding); 166 if (decoder == null) { 167 Charset charset = Charset.forName(encoding); 168 if (charset != null) { 169 decoder = charset.newDecoder(); 170 decoders.put(encoding, decoder); 171 encoders.put(encoding, charset.newEncoder()); 172 } else { 173 throw new UnsupportedEncodingException ("charset '" + encoding + "' has not been found"); 174 } 175 } 176 177 return decoder.decode(buffer).toString(); 178 179 } catch (CharacterCodingException cce) { 180 RuntimeException re = new RuntimeException ("coding exception for '" + encoding + "' occured: " + cce.toString(), cce); 181 throw re; 182 } 183 } 184 185 186 187 193 public static String toByteString(ByteBuffer buffer) { 194 StringBuilder sb = new StringBuilder (); 195 196 while (buffer.hasRemaining()) { 197 String hex = Integer.toHexString(0x0100 + (buffer.get() & 0x00FF)).substring(1); 198 sb.append((hex.length() < 2 ? "0" : "") + hex + " "); 199 } 200 201 return sb.toString(); 202 } 203 204 205 206 213 public static String toString(List <ByteBuffer > buffers, String encoding) throws UnsupportedEncodingException { 214 return toString(buffers.toArray(new ByteBuffer [buffers.size()]), encoding); 215 } 216 217 218 225 public static String toString(ByteBuffer [] buffers, String encoding) throws UnsupportedEncodingException { 226 byte[] bytes = toBytes(buffers); 227 if (bytes != null) { 228 return new String (bytes, encoding); 229 } else { 230 return ""; 231 } 232 } 233 234 235 236 245 public static String toString(ByteBuffer [] buffers, String encoding, int maxOutSize) throws UnsupportedEncodingException { 246 String s = toString(buffers, encoding); 247 if (s.length() > maxOutSize) { 248 s = s.substring(0, maxOutSize) + " [output has been cut]"; 249 } 250 return s; 251 } 252 253 254 260 public static ByteBuffer toByteBuffer(ByteBuffer [] buffers) { 261 byte[] bytes = toBytes(buffers); 262 return ByteBuffer.wrap(bytes); 263 } 264 265 266 272 public static byte[] toBytes(List <ByteBuffer > buffers) { 273 return toBytes(buffers.toArray(new ByteBuffer [buffers.size()])); 274 } 275 276 277 278 284 public static byte[] toBytes(ByteBuffer [] buffers) { 285 byte[] result = null; 286 287 if (buffers == null) { 288 return null; 289 } 290 291 int size = 0; 292 for (ByteBuffer buffer : buffers) { 293 size += buffer.remaining(); 294 if (result == null) { 295 byte[] bytes = toBytes(buffer); 296 if (bytes.length > 0) { 297 result = bytes; 298 } 299 } else { 300 byte[] additionalBytes = toBytes(buffer); 301 byte[] newResult = new byte[result.length + additionalBytes.length]; 302 System.arraycopy(result, 0, newResult, 0, result.length); 303 System.arraycopy(additionalBytes, 0, newResult, result.length, additionalBytes.length); 304 result = newResult; 305 } 306 } 307 308 return result; 309 } 310 311 312 318 public static byte[] toBytes(ByteBuffer buffer) { 319 320 int savedPos = buffer.position(); 321 int savedLimit = buffer.limit(); 322 323 try { 324 325 byte[] array = new byte[buffer.limit() - buffer.position()]; 326 327 if (buffer.hasArray()) { 328 int offset = buffer.arrayOffset(); 329 byte[] bufferArray = buffer.array(); 330 System.arraycopy(bufferArray, offset, array, 0, array.length); 331 332 return array; 333 } else { 334 buffer.get(array); 335 return array; 336 } 337 338 } finally { 339 buffer.position(savedPos); 340 buffer.limit(savedLimit); 341 } 342 } 343 344 345 346 354 public static String toHexString(byte[] buffers, int maxOutSize) { 355 return toHexString(new ByteBuffer [] { ByteBuffer.wrap(buffers) }, maxOutSize); 356 } 357 358 359 360 368 public static String toHexString(ByteBuffer [] buffers, int maxOutSize) { 369 370 String postfix = ""; 372 int size = 0; 373 List <ByteBuffer > copies = new ArrayList <ByteBuffer >(); 374 for (ByteBuffer buffer : buffers) { 375 ByteBuffer copy = buffer.duplicate(); 376 if ((size + copy.limit()) > maxOutSize) { 377 copy.limit(maxOutSize - size); 378 copies.add(copy); 379 postfix = " [...output has been cut]"; 380 break; 381 } else { 382 copies.add(copy); 383 } 384 } 385 386 StringBuilder result = new StringBuilder (); 387 388 389 for (ByteBuffer buffer : copies) { 390 result.append(toByteString(buffer)); 391 } 392 393 result.append(postfix); 394 395 return result.toString(); 396 } 397 398 399 407 public static String toTextOrHexString(ByteBuffer buffer, String encoding, int maxOutSize) { 408 return toTextOrHexString(new ByteBuffer [] { buffer }, encoding, maxOutSize); 409 } 410 411 412 420 public static String toTextOrHexString(ByteBuffer [] buffers, String encoding, int maxOutSize) { 421 boolean hasNonPrintableChars = false; 422 423 for (ByteBuffer buffer : buffers) { 424 ByteBuffer copy = buffer.duplicate(); 425 while (copy.hasRemaining()) { 426 int i = copy.get(); 427 if (i < 10) { 428 hasNonPrintableChars = true; 429 } 430 } 431 } 432 433 if (hasNonPrintableChars) { 434 return toHexString(buffers, maxOutSize); 435 } else { 436 try { 437 return toString(buffers, encoding, maxOutSize); 438 } catch (UnsupportedEncodingException use) { 439 return toHexString(buffers, maxOutSize); 440 } 441 } 442 } 443 444 445 446 public static String toTextAndHexString(ByteBuffer [] buffers, String encoding, int maxOutSize) { 447 StringBuilder sb = new StringBuilder (); 448 sb.append(DataConverter.toHexString(buffers, 500)); 449 sb.append("\n"); 450 try { 451 sb.append("[txt:] " + toString(buffers, "US-ASCII", 500)); 452 } catch (Exception ignore) { 453 sb.append("[txt:] ... content not printable ..."); 454 } 455 return sb.toString(); 456 } 457 } 458 | Popular Tags |