1 2 package org.dbunit.util; 3 4 17 public class Base64 18 { 19 20 21 public final static boolean ENCODE = true; 22 23 24 25 public final static boolean DECODE = false; 26 27 28 29 private final static int MAX_LINE_LENGTH = 76; 30 31 32 33 private final static byte EQUALS_SIGN = (byte)'='; 34 35 36 37 private final static byte NEW_LINE = (byte)'\n'; 38 39 40 41 private final static byte[] ALPHABET = 42 { 43 (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G', 44 (byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N', 45 (byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U', 46 (byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z', 47 (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g', 48 (byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n', 49 (byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u', 50 (byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z', 51 (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', 52 (byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/' 53 }; 54 55 59 private final static byte[] DECODABET = 60 { 61 -9, -9, -9, -9, -9, -9, -9, -9, -9, -5, -5, -9, -9, -5, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -5, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, 62, -9, -9, -9, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -9, -9, -9, -1, -9, -9, -9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -9, -9, -9, -9, -9, -9, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -9, -9, -9, -9 92 }; 93 94 private final static byte BAD_ENCODING = -9; private final static byte WHITE_SPACE_ENC = -5; private final static byte EQUALS_SIGN_ENC = -1; 98 99 100 private Base64() 101 { 102 } 103 104 105 106 public static void main(String [] args) 107 { 108 String s = "Hello, world"; 109 s = "abcd"; 110 113 byte[] b = encodeString(s).getBytes(); 114 byte[] c = decode(b, 0, b.length); 115 116 System.out.println("\n\n" + s + ":" + new String (b) + ":" + new String (c)); 117 118 try 119 { 120 java.io.FileInputStream fis = new java.io.FileInputStream ("c:\\abcd.txt"); 121 InputStream b64is = new InputStream(fis, DECODE); 122 int ib = 0; 123 while ((ib = b64is.read()) > 0) 124 { } 126 } catch (Exception e) 128 { 129 e.printStackTrace(); 130 } 131 } 132 133 134 135 136 137 145 private static byte[] encode3to4(byte[] threeBytes) 146 { 147 return encode3to4(threeBytes, 3); 148 } 150 151 164 private static byte[] encode3to4(byte[] threeBytes, int numSigBytes) 165 { 166 byte[] dest = new byte[4]; 167 encode3to4(threeBytes, 0, numSigBytes, dest, 0); 168 return dest; 169 } 170 171 172 193 private static byte[] encode3to4( 194 byte[] source, int srcOffset, int numSigBytes, 195 byte[] destination, int destOffset) 196 { 197 204 int inBuff = (numSigBytes > 0 ? ((source[srcOffset] << 24) >>> 8) : 0) 209 | (numSigBytes > 1 ? ((source[srcOffset + 1] << 24) >>> 16) : 0) 210 | (numSigBytes > 2 ? ((source[srcOffset + 2] << 24) >>> 24) : 0); 211 212 switch (numSigBytes) 213 { 214 case 3: 215 destination[destOffset] = ALPHABET[(inBuff >>> 18)]; 216 destination[destOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3f]; 217 destination[destOffset + 2] = ALPHABET[(inBuff >>> 6) & 0x3f]; 218 destination[destOffset + 3] = ALPHABET[(inBuff) & 0x3f]; 219 return destination; 220 221 case 2: 222 destination[destOffset] = ALPHABET[(inBuff >>> 18)]; 223 destination[destOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3f]; 224 destination[destOffset + 2] = ALPHABET[(inBuff >>> 6) & 0x3f]; 225 destination[destOffset + 3] = EQUALS_SIGN; 226 return destination; 227 228 case 1: 229 destination[destOffset] = ALPHABET[(inBuff >>> 18)]; 230 destination[destOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3f]; 231 destination[destOffset + 2] = EQUALS_SIGN; 232 destination[destOffset + 3] = EQUALS_SIGN; 233 return destination; 234 235 default: 236 return destination; 237 } } 240 241 251 public static String encodeObject(java.io.Serializable serializableObject) 252 { 253 java.io.ByteArrayOutputStream baos = null; 254 java.io.OutputStream b64os = null; 255 java.io.ObjectOutputStream oos = null; 256 257 try 258 { 259 baos = new java.io.ByteArrayOutputStream (); 260 b64os = new OutputStream(baos, Base64.ENCODE); 261 oos = new java.io.ObjectOutputStream (b64os); 262 263 oos.writeObject(serializableObject); 264 } catch (java.io.IOException e) 266 { 267 e.printStackTrace(); 268 return null; 269 } finally 271 { 272 try 273 { 274 oos.close(); 275 } 276 catch (Exception e) 277 { 278 } 279 try 280 { 281 b64os.close(); 282 } 283 catch (Exception e) 284 { 285 } 286 try 287 { 288 baos.close(); 289 } 290 catch (Exception e) 291 { 292 } 293 } 295 return new String (baos.toByteArray()); 296 } 298 299 307 public static String encodeBytes(byte[] source) 308 { 309 return encodeBytes(source, 0, source.length); 310 } 312 313 321 public static String encodeBytes(byte[] source, int off, int len) 322 { 323 int len43 = len * 4 / 3; 324 byte[] outBuff = new byte[(len43) + ((len % 3) > 0 ? 4 : 0) + (len43 / MAX_LINE_LENGTH)]; int d = 0; 328 int e = 0; 329 int len2 = len - 2; 330 int lineLength = 0; 331 for (; d < len2; d += 3, e += 4) 332 { 333 encode3to4(source, d, 3, outBuff, e); 334 335 lineLength += 4; 336 if (lineLength == MAX_LINE_LENGTH) 337 { 338 outBuff[e + 4] = NEW_LINE; 339 e++; 340 lineLength = 0; 341 } } 344 if (d < len) 345 { 346 encode3to4(source, d, len - d, outBuff, e); 347 e += 4; 348 } 350 return new String (outBuff, 0, e); 351 } 353 354 362 public static String encodeString(String s) 363 { 364 return encodeBytes(s.getBytes()); 365 } 367 368 369 370 371 372 373 382 private static byte[] decode4to3(byte[] fourBytes) 383 { 384 byte[] outBuff1 = new byte[3]; 385 int count = decode4to3(fourBytes, 0, outBuff1, 0); 386 byte[] outBuff2 = new byte[count]; 387 388 for (int i = 0; i < count; i++) 389 outBuff2[i] = outBuff1[i]; 390 391 return outBuff2; 392 } 393 394 395 417 private static int decode4to3(byte[] source, int srcOffset, byte[] destination, int destOffset) 418 { 419 if (source[srcOffset + 2] == EQUALS_SIGN) 421 { 422 int outBuff = ((DECODABET[source[srcOffset]] << 24) >>> 6) 423 | ((DECODABET[source[srcOffset + 1]] << 24) >>> 12); 424 425 destination[destOffset] = (byte)(outBuff >>> 16); 426 return 1; 427 } 428 429 else if (source[srcOffset + 3] == EQUALS_SIGN) 431 { 432 int outBuff = ((DECODABET[source[srcOffset]] << 24) >>> 6) 433 | ((DECODABET[source[srcOffset + 1]] << 24) >>> 12) 434 | ((DECODABET[source[srcOffset + 2]] << 24) >>> 18); 435 436 destination[destOffset] = (byte)(outBuff >>> 16); 437 destination[destOffset + 1] = (byte)(outBuff >>> 8); 438 return 2; 439 } 440 441 else 443 { 444 int outBuff = ((DECODABET[source[srcOffset]] << 24) >>> 6) 445 | ((DECODABET[source[srcOffset + 1]] << 24) >>> 12) 446 | ((DECODABET[source[srcOffset + 2]] << 24) >>> 18) 447 | ((DECODABET[source[srcOffset + 3]] << 24) >>> 24); 448 449 destination[destOffset] = (byte)(outBuff >> 16); 450 destination[destOffset + 1] = (byte)(outBuff >> 8); 451 destination[destOffset + 2] = (byte)(outBuff); 452 return 3; 453 } 454 } 456 457 464 public static byte[] decode(String s) 465 { 466 byte[] bytes = s.getBytes(); 467 return decode(bytes, 0, bytes.length); 468 } 470 471 481 public static String decodeToString(String s) 482 { 483 return new String (decode(s)); 484 } 486 487 495 public static Object decodeToObject(String encodedObject) 496 { 497 byte[] objBytes = decode(encodedObject); 498 499 java.io.ByteArrayInputStream bais = null; 500 java.io.ObjectInputStream ois = null; 501 502 try 503 { 504 bais = new java.io.ByteArrayInputStream (objBytes); 505 ois = new java.io.ObjectInputStream (bais); 506 507 return ois.readObject(); 508 } catch (java.io.IOException e) 510 { 511 e.printStackTrace(); 512 return null; 513 } catch (ClassNotFoundException e) 515 { 516 e.printStackTrace(); 517 return null; 518 } finally 520 { 521 try 522 { 523 bais.close(); 524 } 525 catch (Exception e) 526 { 527 } 528 try 529 { 530 ois.close(); 531 } 532 catch (Exception e) 533 { 534 } 535 } } 538 539 549 public static byte[] decode(byte[] source, int off, int len) 550 { 551 int len34 = len * 3 / 4; 552 byte[] outBuff = new byte[len34]; int outBuffPosn = 0; 554 555 byte[] b4 = new byte[4]; 556 int b4Posn = 0; 557 int i = 0; 558 byte sbiCrop = 0; 559 byte sbiDecode = 0; 560 for (i = 0; i < len; i++) 561 { 562 sbiCrop = (byte)(source[i] & 0x7f); sbiDecode = DECODABET[sbiCrop]; 564 565 if (sbiDecode >= WHITE_SPACE_ENC) { 567 if (sbiDecode >= EQUALS_SIGN_ENC) 568 { 569 b4[b4Posn++] = sbiCrop; 570 if (b4Posn > 3) 571 { 572 outBuffPosn += decode4to3(b4, 0, outBuff, outBuffPosn); 573 b4Posn = 0; 574 575 if (sbiCrop == EQUALS_SIGN) 577 break; 578 } 580 } 582 } else 584 { 585 System.err.println("Bad Base64 input character at " + i + ": " + source[i] + "(decimal)"); 586 return null; 587 } } 590 byte[] out = new byte[outBuffPosn]; 591 System.arraycopy(outBuff, 0, out, 0, outBuffPosn); 592 return out; 593 } 595 596 597 598 599 600 601 602 611 public static class InputStream extends java.io.FilterInputStream 612 { 613 private boolean encode; private int position; private byte[] buffer; private int bufferLength; private int numSigBytes; 619 620 626 public InputStream(java.io.InputStream in) 627 { 628 this(in, Base64.DECODE); 629 } 631 632 642 public InputStream(java.io.InputStream in, boolean encode) 643 { 644 super(in); 645 this.encode = encode; 646 this.bufferLength = encode ? 4 : 3; 647 this.buffer = new byte[bufferLength]; 648 this.position = -1; 649 } 651 658 public int read() throws java.io.IOException 659 { 660 if (position < 0) 662 { 663 if (encode) 664 { 665 byte[] b3 = new byte[3]; 666 numSigBytes = 0; 667 for (int i = 0; i < 3; i++) 668 { 669 try 670 { 671 int b = in.read(); 672 673 if (b >= 0) 675 { 676 b3[i] = (byte)b; 677 numSigBytes++; 678 } 680 } catch (java.io.IOException e) 682 { 683 if (i == 0) 685 throw e; 686 687 } } 690 if (numSigBytes > 0) 691 { 692 encode3to4(b3, 0, numSigBytes, buffer, 0); 693 position = 0; 694 } } 697 else 699 { 700 byte[] b4 = new byte[4]; 701 int i = 0; 702 for (i = 0; i < 4; i++) 703 { 704 int b = 0; 705 do 706 { 707 b = in.read(); 708 } 709 while (b >= 0 && DECODABET[b & 0x7f] < WHITE_SPACE_ENC); 710 711 if (b < 0) 712 break; 714 b4[i] = (byte)b; 715 } 717 if (i == 4) 718 { 719 numSigBytes = decode4to3(b4, 0, buffer, 0); 720 position = 0; 721 } 723 } } 726 if (position >= 0) 728 { 729 if (position >= numSigBytes) 731 return -1; 732 733 int b = buffer[position++]; 734 735 if (position >= bufferLength) 736 position = -1; 737 738 return b; 739 } 741 else 743 return -1; 744 } 746 747 759 public int read(byte[] dest, int off, int len) throws java.io.IOException 760 { 761 int i; 762 int b; 763 for (i = 0; i < len; i++) 764 { 765 b = read(); 766 767 if (b < 0) 768 return -1; 769 770 dest[off + i] = (byte)b; 771 } return i; 773 } 775 } 777 778 779 780 781 782 783 784 785 786 795 public static class OutputStream extends java.io.FilterOutputStream 796 { 797 private boolean encode; 798 private int position; 799 private byte[] buffer; 800 private int bufferLength; 801 private int lineLength; 802 803 804 810 public OutputStream(java.io.OutputStream out) 811 { 812 this(out, Base64.ENCODE); 813 } 815 816 826 public OutputStream(java.io.OutputStream out, boolean encode) 827 { 828 super(out); 829 this.encode = encode; 830 this.bufferLength = encode ? 3 : 4; 831 this.buffer = new byte[bufferLength]; 832 this.position = 0; 833 this.lineLength = 0; 834 } 836 837 849 public void write(int theByte) throws java.io.IOException 850 { 851 buffer[position++] = (byte)theByte; 852 if (position >= bufferLength) 853 { 854 if (encode) 855 { 856 out.write(Base64.encode3to4(buffer, bufferLength)); 857 858 lineLength += 4; 859 if (lineLength >= MAX_LINE_LENGTH) 860 { 861 out.write(NEW_LINE); 862 lineLength = 0; 863 } } else 866 out.write(Base64.decode4to3(buffer)); 867 868 position = 0; 869 } } 872 873 882 public void write(byte[] theBytes, int off, int len) throws java.io.IOException 883 { 884 for (int i = 0; i < len; i++) 885 { 886 write(theBytes[off + i]); 887 } 889 } 891 892 899 public void flush() throws java.io.IOException 900 { 901 if (position > 0) 902 { 903 if (encode) 904 { 905 out.write(Base64.encode3to4(buffer, position)); 906 } else 908 { 909 throw new java.io.IOException ("Base64 input not properly padded."); 910 } } 913 super.flush(); 914 out.flush(); 915 } 917 918 923 public void close() throws java.io.IOException 924 { 925 this.flush(); 926 927 super.close(); 928 out.close(); 929 930 buffer = null; 931 out = null; 932 } 934 } 936 937 } 939 940 941 | Popular Tags |