1 50 51 package com.lowagie.text.pdf; 52 import java.io.IOException ; 53 import java.io.OutputStream ; 54 import java.io.UnsupportedEncodingException ; 55 import java.text.DecimalFormat ; 56 import java.text.DecimalFormatSymbols ; 57 import java.util.Locale ; 58 59 import com.lowagie.text.DocWriter; 60 61 66 67 public class ByteBuffer extends OutputStream { 68 69 protected int count; 70 71 72 protected byte buf[]; 73 74 private static int byteCacheSize = 0; 75 76 private static byte[][] byteCache = new byte[byteCacheSize][]; 77 public static final byte ZERO = (byte)'0'; 78 private static final char[] chars = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; 79 private static final byte[] bytes = new byte[] {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102}; 80 84 public static boolean HIGH_PRECISION = false; 85 private static final DecimalFormatSymbols dfs = new DecimalFormatSymbols (Locale.US); 86 87 88 public ByteBuffer() { 89 this(128); 90 } 91 92 96 public ByteBuffer(int size) { 97 if (size < 1) 98 size = 128; 99 buf = new byte[size]; 100 } 101 102 110 111 public static void setCacheSize(int size) { 112 if (size > 3276700) size = 3276700; 113 if (size <= byteCacheSize) return; 114 byte[][] tmpCache = new byte[size][]; 115 System.arraycopy(byteCache, 0, tmpCache, 0, byteCacheSize); 116 byteCache = tmpCache; 117 byteCacheSize = size; 118 } 119 120 125 126 public static void fillCache(int decimals) { 127 int step = 1; 128 switch(decimals) { 129 case 0: 130 step = 100; 131 break; 132 case 1: 133 step = 10; 134 break; 135 } 136 for (int i = 1; i < byteCacheSize; i += step) { 137 if (byteCache[i] != null) continue; 138 byteCache[i] = convertToBytes(i); 139 } 140 } 141 142 148 149 private static byte[] convertToBytes(int i) { 150 int size = (int)Math.floor(Math.log(i) / Math.log(10)); 151 if (i % 100 != 0) { 152 size += 2; 153 } 154 if (i % 10 != 0) { 155 size++; 156 } 157 if (i < 100) { 158 size++; 159 if (i < 10) { 160 size++; 161 } 162 } 163 size--; 164 byte[] cache = new byte[size]; 165 size --; 166 if (i < 100) { 167 cache[0] = (byte)'0'; 168 } 169 if (i % 10 != 0) { 170 cache[size--] = bytes[i % 10]; 171 } 172 if (i % 100 != 0) { 173 cache[size--] = bytes[(i / 10) % 10]; 174 cache[size--] = (byte)'.'; 175 } 176 size = (int)Math.floor(Math.log(i) / Math.log(10)) - 1; 177 int add = 0; 178 while (add < size) { 179 cache[add] = bytes[(i / (int)Math.pow(10, size - add + 1)) % 10]; 180 add++; 181 } 182 return cache; 183 } 184 185 190 public ByteBuffer append_i(int b) { 191 int newcount = count + 1; 192 if (newcount > buf.length) { 193 byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)]; 194 System.arraycopy(buf, 0, newbuf, 0, count); 195 buf = newbuf; 196 } 197 buf[count] = (byte)b; 198 count = newcount; 199 return this; 200 } 201 202 210 public ByteBuffer append(byte b[], int off, int len) { 211 if ((off < 0) || (off > b.length) || (len < 0) || 212 ((off + len) > b.length) || ((off + len) < 0) || len == 0) 213 return this; 214 int newcount = count + len; 215 if (newcount > buf.length) { 216 byte newbuf[] = new byte[Math.max(buf.length << 1, newcount)]; 217 System.arraycopy(buf, 0, newbuf, 0, count); 218 buf = newbuf; 219 } 220 System.arraycopy(b, off, buf, count, len); 221 count = newcount; 222 return this; 223 } 224 225 230 public ByteBuffer append(byte b[]) { 231 return append(b, 0, b.length); 232 } 233 234 240 public ByteBuffer append(String str) { 241 if (str != null) 242 return append(DocWriter.getISOBytes(str)); 243 return this; 244 } 245 246 252 public ByteBuffer append(char c) { 253 return append_i(c); 254 } 255 256 261 public ByteBuffer append(ByteBuffer buf) { 262 return append(buf.buf, 0, buf.count); 263 } 264 265 270 public ByteBuffer append(int i) { 271 return append((double)i); 272 } 273 274 public ByteBuffer append(byte b) { 275 return append_i(b); 276 } 277 278 public ByteBuffer appendHex(byte b) { 279 append(bytes[(b >> 4) & 0x0f]); 280 return append(bytes[b & 0x0f]); 281 } 282 283 289 public ByteBuffer append(float i) { 290 return append((double)i); 291 } 292 293 299 public ByteBuffer append(double d) { 300 append(formatDouble(d, this)); 301 return this; 302 } 303 304 309 public static String formatDouble(double d) { 310 return formatDouble(d, null); 311 } 312 313 321 public static String formatDouble(double d, ByteBuffer buf) { 322 if (HIGH_PRECISION) { 323 DecimalFormat dn = new DecimalFormat ("0.######", dfs); 324 String sform = dn.format(d); 325 if (buf == null) 326 return sform; 327 else { 328 buf.append(sform); 329 return null; 330 } 331 } 332 boolean negative = false; 333 if (Math.abs(d) < 0.000015) { 334 if (buf != null) { 335 buf.append(ZERO); 336 return null; 337 } else { 338 return "0"; 339 } 340 } 341 if (d < 0) { 342 negative = true; 343 d = -d; 344 } 345 if (d < 1.0) { 346 d += 0.000005; 347 if (d >= 1) { 348 if (negative) { 349 if (buf != null) { 350 buf.append((byte)'-'); 351 buf.append((byte)'1'); 352 return null; 353 } else { 354 return "-1"; 355 } 356 } else { 357 if (buf != null) { 358 buf.append((byte)'1'); 359 return null; 360 } else { 361 return "1"; 362 } 363 } 364 } 365 if (buf != null) { 366 int v = (int) (d * 100000); 367 368 if (negative) buf.append((byte)'-'); 369 buf.append((byte)'0'); 370 buf.append((byte)'.'); 371 372 buf.append( (byte)(v / 10000 + ZERO) ); 373 if (v % 10000 != 0) { 374 buf.append( (byte)((v / 1000) % 10 + ZERO) ); 375 if (v % 1000 != 0) { 376 buf.append( (byte)((v / 100) % 10 + ZERO) ); 377 if (v % 100 != 0) { 378 buf.append((byte)((v / 10) % 10 + ZERO) ); 379 if (v % 10 != 0) { 380 buf.append((byte)((v) % 10 + ZERO) ); 381 } 382 } 383 } 384 } 385 return null; 386 } else { 387 int x = 100000; 388 int v = (int) (d * x); 389 390 StringBuffer res = new StringBuffer (); 391 if (negative) res.append('-'); 392 res.append("0."); 393 394 while( v < x/10 ) { 395 res.append('0'); 396 x /= 10; 397 } 398 res.append(v); 399 int cut = res.length() - 1; 400 while (res.charAt(cut) == '0') { 401 --cut; 402 } 403 res.setLength(cut + 1); 404 return res.toString(); 405 } 406 } else if (d <= 32767) { 407 d += 0.005; 408 int v = (int) (d * 100); 409 410 if (v < byteCacheSize && byteCache[v] != null) { 411 if (buf != null) { 412 if (negative) buf.append((byte)'-'); 413 buf.append(byteCache[v]); 414 return null; 415 } else { 416 String tmp = PdfEncodings.convertToString(byteCache[v], null); 417 if (negative) tmp = "-" + tmp; 418 return tmp; 419 } 420 } 421 if (buf != null) { 422 if (v < byteCacheSize) { 423 byte[] cache; 425 int size = 0; 426 if (v >= 1000000) { 427 size += 5; 429 } else if (v >= 100000) { 430 size += 4; 432 } else if (v >= 10000) { 433 size += 3; 435 } else if (v >= 1000) { 436 size += 2; 438 } else if (v >= 100) { 439 size += 1; 441 } 442 443 if (v % 100 != 0) { 445 size += 2; 447 } 448 if (v % 10 != 0) { 449 size++; 450 } 451 cache = new byte[size]; 452 int add = 0; 453 if (v >= 1000000) { 454 cache[add++] = bytes[(v / 1000000)]; 455 } 456 if (v >= 100000) { 457 cache[add++] = bytes[(v / 100000) % 10]; 458 } 459 if (v >= 10000) { 460 cache[add++] = bytes[(v / 10000) % 10]; 461 } 462 if (v >= 1000) { 463 cache[add++] = bytes[(v / 1000) % 10]; 464 } 465 if (v >= 100) { 466 cache[add++] = bytes[(v / 100) % 10]; 467 } 468 469 if (v % 100 != 0) { 470 cache[add++] = (byte)'.'; 471 cache[add++] = bytes[(v / 10) % 10]; 472 if (v % 10 != 0) { 473 cache[add++] = bytes[v % 10]; 474 } 475 } 476 byteCache[v] = cache; 477 } 478 479 if (negative) buf.append((byte)'-'); 480 if (v >= 1000000) { 481 buf.append( bytes[(v / 1000000)] ); 482 } 483 if (v >= 100000) { 484 buf.append( bytes[(v / 100000) % 10] ); 485 } 486 if (v >= 10000) { 487 buf.append( bytes[(v / 10000) % 10] ); 488 } 489 if (v >= 1000) { 490 buf.append( bytes[(v / 1000) % 10] ); 491 } 492 if (v >= 100) { 493 buf.append( bytes[(v / 100) % 10] ); 494 } 495 496 if (v % 100 != 0) { 497 buf.append((byte)'.'); 498 buf.append( bytes[(v / 10) % 10] ); 499 if (v % 10 != 0) { 500 buf.append( bytes[v % 10] ); 501 } 502 } 503 return null; 504 } else { 505 StringBuffer res = new StringBuffer (); 506 if (negative) res.append('-'); 507 if (v >= 1000000) { 508 res.append( chars[(v / 1000000)] ); 509 } 510 if (v >= 100000) { 511 res.append( chars[(v / 100000) % 10] ); 512 } 513 if (v >= 10000) { 514 res.append( chars[(v / 10000) % 10] ); 515 } 516 if (v >= 1000) { 517 res.append( chars[(v / 1000) % 10] ); 518 } 519 if (v >= 100) { 520 res.append( chars[(v / 100) % 10] ); 521 } 522 523 if (v % 100 != 0) { 524 res.append('.'); 525 res.append( chars[(v / 10) % 10] ); 526 if (v % 10 != 0) { 527 res.append( chars[v % 10] ); 528 } 529 } 530 return res.toString(); 531 } 532 } else { 533 StringBuffer res = new StringBuffer (); 534 if (negative) res.append('-'); 535 d += 0.5; 536 long v = (long) d; 537 return res.append(v).toString(); 538 } 539 } 540 541 544 public void reset() { 545 count = 0; 546 } 547 548 555 public byte[] toByteArray() { 556 byte newbuf[] = new byte[count]; 557 System.arraycopy(buf, 0, newbuf, 0, count); 558 return newbuf; 559 } 560 561 566 public int size() { 567 return count; 568 } 569 570 public void setSize(int size) { 571 if (size > count || size < 0) 572 throw new IndexOutOfBoundsException ("The new size must be positive and <= of the current size"); 573 count = size; 574 } 575 576 582 public String toString() { 583 return new String (buf, 0, count); 584 } 585 586 595 public String toString(String enc) throws UnsupportedEncodingException { 596 return new String (buf, 0, count, enc); 597 } 598 599 607 public void writeTo(OutputStream out) throws IOException { 608 out.write(buf, 0, count); 609 } 610 611 public void write(int b) throws IOException { 612 append((byte)b); 613 } 614 615 public void write(byte[] b, int off, int len) { 616 append(b, off, len); 617 } 618 619 public byte[] getBuffer() { 620 return buf; 621 } 622 } | Popular Tags |