PageRenderTime 72ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/utils/text/Base64Encoder.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 184 lines | 97 code | 29 blank | 58 comment | 18 complexity | ab857ee2614109bebd9250e0afa2ae11 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. package mpv5.utils.text;
  2. /* BASE.java --
  3. Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. /**
  33. * Encodes and decodes text according to the BASE64 encoding.
  34. *
  35. * @author Chris Burdess (dog@gnu.org)
  36. */
  37. public final class Base64Encoder {
  38. private static final byte[] src = {
  39. 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52,
  40. 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a,
  41. 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x30, 0x31,
  42. 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2b, 0x2f
  43. };
  44. private static final byte[] dst;
  45. static {
  46. dst = new byte[0x100];
  47. for (int i = 0x0; i < 0xff; i++) {
  48. dst[i] = -1;
  49. }
  50. for (int i = 0; i < src.length; i++) {
  51. dst[src[i]] = (byte) i;
  52. }
  53. }
  54. private Base64Encoder() {}
  55. /**
  56. * Encode the specified byte array using the BASE64 algorithm.
  57. *
  58. * @param bs the source byte array
  59. */
  60. public static byte[] encode(byte[] bs) {
  61. int si = 0,
  62. ti = 0; // source/target array indices
  63. byte[] bt = new byte[((bs.length + 2) * 4) / 3]; // target byte array
  64. for (; si < bs.length; si += 3) {
  65. int buflen = bs.length - si;
  66. if (buflen == 1) {
  67. byte b = bs[si];
  68. int i = 0;
  69. bt[ti++] = src[b >>> 2 & 0x3f];
  70. bt[ti++] = src[(b << 4 & 0x30) + (i >>> 4 & 0xf)];
  71. } else if (buflen == 2) {
  72. byte b1 = bs[si],
  73. b2 = bs[si + 1];
  74. int i = 0;
  75. bt[ti++] = src[b1 >>> 2 & 0x3f];
  76. bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)];
  77. bt[ti++] = src[(b2 << 2 & 0x3c) + (i >>> 6 & 0x3)];
  78. } else {
  79. byte b1 = bs[si],
  80. b2 = bs[si + 1],
  81. b3 = bs[si + 2];
  82. bt[ti++] = src[b1 >>> 2 & 0x3f];
  83. bt[ti++] = src[(b1 << 4 & 0x30) + (b2 >>> 4 & 0xf)];
  84. bt[ti++] = src[(b2 << 2 & 0x3c) + (b3 >>> 6 & 0x3)];
  85. bt[ti++] = src[b3 & 0x3f];
  86. }
  87. }
  88. /*
  89. * while (ti < bt.length)
  90. * {
  91. * bt[ti++] = 0x3d;
  92. * }
  93. */
  94. return bt;
  95. }
  96. /**
  97. * Decode the specified byte array using the BASE64 algorithm.
  98. *
  99. * @param bs the source byte array
  100. */
  101. public static byte[] decode(byte[] bs) {
  102. int srclen = bs.length;
  103. while ((srclen > 0) && (bs[srclen - 1] == 0x3d)) {
  104. srclen--; /* strip padding character */
  105. }
  106. byte[] buffer = new byte[srclen];
  107. int buflen = 0;
  108. int si = 0;
  109. int len = srclen - si;
  110. while (len > 0) {
  111. byte b0 = dst[bs[si++] & 0xff];
  112. byte b2 = dst[bs[si++] & 0xff];
  113. buffer[buflen++] = (byte) (b0 << 2 & 0xfc | b2 >>> 4 & 0x3);
  114. if (len > 2) {
  115. b0 = b2;
  116. b2 = dst[bs[si++] & 0xff];
  117. buffer[buflen++] = (byte) (b0 << 4 & 0xf0 | b2 >>> 2 & 0xf);
  118. if (len > 3) {
  119. b0 = b2;
  120. b2 = dst[bs[si++] & 0xff];
  121. buffer[buflen++] = (byte) (b0 << 6 & 0xc0 | b2 & 0x3f);
  122. }
  123. }
  124. len = srclen - si;
  125. }
  126. byte[] bt = new byte[buflen];
  127. System.arraycopy(buffer, 0, bt, 0, buflen);
  128. return bt;
  129. }
  130. public static void main(String[] args) {
  131. boolean decode = false;
  132. for (int i = 0; i < args.length; i++) {
  133. if (args[i].equals("-d")) {
  134. decode = true;
  135. } else {
  136. try {
  137. byte[] in = args[i].getBytes("US-ASCII");
  138. byte[] out = decode
  139. ? decode(in)
  140. : encode(in);
  141. System.out.println(args[i] + " = " + new String(out, "US-ASCII"));
  142. } catch (java.io.UnsupportedEncodingException e) {
  143. e.printStackTrace(System.err);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. //~ Formatted by Jindent --- http://www.jindent.com