2
0

Base64.java 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package net.sf.l2j;
  16. import java.io.BufferedReader;
  17. import java.io.IOException;
  18. import java.io.InputStreamReader;
  19. /**
  20. * Encodes and decodes to and from Base64 notation.
  21. *
  22. * The source is based on the work of Robert Harder
  23. *
  24. * <p>
  25. * I am placing this code in the Public Domain. Do with it as you will.
  26. * This software comes with no guarantees or warranties but with
  27. * plenty of well-wishing instead!
  28. * Please visit <a href="http://iharder.net/xmlizable">http://iharder.net/base64</a>
  29. * periodically to check for updates or to contribute improvements.
  30. * </p>
  31. *
  32. * @author Robert Harder
  33. * @author rob@iharder.net
  34. * @version 2.0
  35. */
  36. public class Base64
  37. {
  38. /* ******** P U B L I C F I E L D S ******** */
  39. /** No options specified. Value is zero. */
  40. public final static int NO_OPTIONS = 0;
  41. /** Specify encoding. */
  42. public final static int ENCODE = 1;
  43. /** Specify decoding. */
  44. public final static int DECODE = 0;
  45. /** Specify that data should be gzip-compressed. */
  46. public final static int GZIP = 2;
  47. /** Don't break lines when encoding (violates strict Base64 specification) */
  48. public final static int DONT_BREAK_LINES = 8;
  49. /* ******** P R I V A T E F I E L D S ******** */
  50. /** Maximum line length (76) of Base64 output. */
  51. private final static int MAX_LINE_LENGTH = 76;
  52. /** The equals sign (=) as a byte. */
  53. private final static byte EQUALS_SIGN = (byte) '=';
  54. /** The new line character (\n) as a byte. */
  55. private final static byte NEW_LINE = (byte) '\n';
  56. /** Preferred encoding. */
  57. private final static String PREFERRED_ENCODING = "UTF-8";
  58. /** The 64 valid Base64 values. */
  59. private final static byte[] ALPHABET;
  60. private final static byte[] _NATIVE_ALPHABET = /* May be something funny like EBCDIC */
  61. {(byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H',
  62. (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P',
  63. (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X',
  64. (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f',
  65. (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n',
  66. (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u', (byte) 'v',
  67. (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2', (byte) '3',
  68. (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) '+', (byte) '/'};
  69. public static void main(String[] args) throws IOException
  70. {
  71. BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  72. System.out.print("Enter String to encode: ");
  73. System.out.println(Base64.encodeBytes(bf.readLine().getBytes()));
  74. }
  75. /** Determine which ALPHABET to use. */
  76. static
  77. {
  78. byte[] __bytes;
  79. try
  80. {
  81. __bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes(PREFERRED_ENCODING);
  82. } // end try
  83. catch (java.io.UnsupportedEncodingException use)
  84. {
  85. __bytes = _NATIVE_ALPHABET; // Fall back to native encoding
  86. } // end catch
  87. ALPHABET = __bytes;
  88. } // end static
  89. /**
  90. * Translates a Base64 value to either its 6-bit reconstruction value
  91. * or a negative number indicating some other meaning.
  92. **/
  93. final static byte[] DECODABET = {-9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 0 - 8
  94. -5, -5, // Whitespace: Tab and Linefeed
  95. -9, -9, // Decimal 11 - 12
  96. -5, // Whitespace: Carriage Return
  97. -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 14 - 26
  98. -9, -9, -9, -9, -9, // Decimal 27 - 31
  99. -5, // Whitespace: Space
  100. -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 33 - 42
  101. 62, // Plus sign at decimal 43
  102. -9, -9, -9, // Decimal 44 - 46
  103. 63, // Slash at decimal 47
  104. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // Numbers zero through nine
  105. -9, -9, -9, // Decimal 58 - 60
  106. -1, // Equals sign at decimal 61
  107. -9, -9, -9, // Decimal 62 - 64
  108. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // Letters 'A' through 'N'
  109. 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // Letters 'O' through 'Z'
  110. -9, -9, -9, -9, -9, -9, // Decimal 91 - 96
  111. 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // Letters 'a' through 'm'
  112. 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // Letters 'n' through 'z'
  113. -9, -9, -9, -9 // Decimal 123 - 126
  114. /*,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 127 - 139
  115. -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 140 - 152
  116. -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 153 - 165
  117. -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 166 - 178
  118. -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 179 - 191
  119. -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 192 - 204
  120. -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 205 - 217
  121. -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 218 - 230
  122. -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9, // Decimal 231 - 243
  123. -9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9,-9 // Decimal 244 - 255 */
  124. };
  125. //private final static byte BAD_ENCODING = -9; // Indicates error in encoding
  126. private final static byte WHITE_SPACE_ENC = -5; // Indicates white space in encoding
  127. private final static byte EQUALS_SIGN_ENC = -1; // Indicates equals sign in encoding
  128. /** Defeats instantiation. */
  129. private Base64()
  130. {
  131. }
  132. /* ******** E N C O D I N G M E T H O D S ******** */
  133. // /**
  134. // * Encodes the first three bytes of array <var>threeBytes</var>
  135. // * and returns a four-byte array in Base64 notation.
  136. // *
  137. // * @param threeBytes the array to convert
  138. // * @return four byte array in Base64 notation.
  139. // * @since 1.3
  140. // */
  141. // private static byte[] encode3to4( byte[] threeBytes )
  142. // {
  143. // return encode3to4( threeBytes, 3 );
  144. // } // end encodeToBytes
  145. // /**
  146. // * Encodes up to the first three bytes of array <var>threeBytes</var>
  147. // * and returns a four-byte array in Base64 notation.
  148. // * The actual number of significant bytes in your array is
  149. // * given by <var>numSigBytes</var>.
  150. // * The array <var>threeBytes</var> needs only be as big as
  151. // * <var>numSigBytes</var>.
  152. // *
  153. // * @param threeBytes the array to convert
  154. // * @param numSigBytes the number of significant bytes in your array
  155. // * @return four byte array in Base64 notation.
  156. // * @since 1.3
  157. // */
  158. // private static byte[] encode3to4( byte[] threeBytes, int numSigBytes )
  159. // {
  160. // byte[] dest = new byte[4];
  161. // encode3to4( threeBytes, 0, numSigBytes, dest, 0 );
  162. // return dest;
  163. // }
  164. /**
  165. * Encodes up to the first three bytes of array <var>threeBytes</var>
  166. * and returns a four-byte array in Base64 notation.
  167. * The actual number of significant bytes in your array is
  168. * given by <var>numSigBytes</var>.
  169. * The array <var>threeBytes</var> needs only be as big as
  170. * <var>numSigBytes</var>.
  171. * Code can reuse a byte array by passing a four-byte array as <var>b4</var>.
  172. *
  173. * @param b4 A reusable byte array to reduce array instantiation
  174. * @param threeBytes the array to convert
  175. * @param numSigBytes the number of significant bytes in your array
  176. * @return four byte array in Base64 notation.
  177. * @since 1.5.1
  178. */
  179. static byte[] encode3to4(byte[] b4, byte[] threeBytes, int numSigBytes)
  180. {
  181. encode3to4(threeBytes, 0, numSigBytes, b4, 0);
  182. return b4;
  183. } // end encode3to4
  184. /**
  185. * Encodes up to three bytes of the array <var>source</var>
  186. * and writes the resulting four Base64 bytes to <var>destination</var>.
  187. * The source and destination arrays can be manipulated
  188. * anywhere along their length by specifying
  189. * <var>srcOffset</var> and <var>destOffset</var>.
  190. * This method does not check to make sure your arrays
  191. * are large enough to accomodate <var>srcOffset</var> + 3 for
  192. * the <var>source</var> array or <var>destOffset</var> + 4 for
  193. * the <var>destination</var> array.
  194. * The actual number of significant bytes in your array is
  195. * given by <var>numSigBytes</var>.
  196. *
  197. * @param source the array to convert
  198. * @param srcOffset the index where conversion begins
  199. * @param numSigBytes the number of significant bytes in your array
  200. * @param destination the array to hold the conversion
  201. * @param destOffset the index where output will be put
  202. * @return the <var>destination</var> array
  203. * @since 1.3
  204. */
  205. static byte[] encode3to4(byte[] source, int srcOffset, int numSigBytes, byte[] destination,
  206. int destOffset)
  207. {
  208. // 1 2 3
  209. // 01234567890123456789012345678901 Bit position
  210. // --------000000001111111122222222 Array position from threeBytes
  211. // --------| || || || | Six bit groups to index ALPHABET
  212. // >>18 >>12 >> 6 >> 0 Right shift necessary
  213. // 0x3f 0x3f 0x3f Additional AND
  214. // Create buffer with zero-padding if there are only one or two
  215. // significant bytes passed in the array.
  216. // We have to shift left 24 in order to flush out the 1's that appear
  217. // when Java treats a value as negative that is cast from a byte to an int.
  218. int inBuff = (numSigBytes > 0 ? ((source[srcOffset] << 24) >>> 8) : 0)
  219. | (numSigBytes > 1 ? ((source[srcOffset + 1] << 24) >>> 16) : 0)
  220. | (numSigBytes > 2 ? ((source[srcOffset + 2] << 24) >>> 24) : 0);
  221. switch (numSigBytes)
  222. {
  223. case 3:
  224. destination[destOffset] = ALPHABET[(inBuff >>> 18)];
  225. destination[destOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3f];
  226. destination[destOffset + 2] = ALPHABET[(inBuff >>> 6) & 0x3f];
  227. destination[destOffset + 3] = ALPHABET[(inBuff) & 0x3f];
  228. return destination;
  229. case 2:
  230. destination[destOffset] = ALPHABET[(inBuff >>> 18)];
  231. destination[destOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3f];
  232. destination[destOffset + 2] = ALPHABET[(inBuff >>> 6) & 0x3f];
  233. destination[destOffset + 3] = EQUALS_SIGN;
  234. return destination;
  235. case 1:
  236. destination[destOffset] = ALPHABET[(inBuff >>> 18)];
  237. destination[destOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3f];
  238. destination[destOffset + 2] = EQUALS_SIGN;
  239. destination[destOffset + 3] = EQUALS_SIGN;
  240. return destination;
  241. default:
  242. return destination;
  243. } // end switch
  244. } // end encode3to4
  245. /**
  246. * Serializes an object and returns the Base64-encoded
  247. * version of that serialized object. If the object
  248. * cannot be serialized or there is another error,
  249. * the method will return <tt>null</tt>.
  250. * The object is not GZip-compressed before being encoded.
  251. *
  252. * @param serializableObject The object to encode
  253. * @return The Base64-encoded object
  254. * @since 1.4
  255. */
  256. public static String encodeObject(java.io.Serializable serializableObject)
  257. {
  258. return encodeObject(serializableObject, NO_OPTIONS);
  259. } // end encodeObject
  260. /**
  261. * Serializes an object and returns the Base64-encoded
  262. * version of that serialized object. If the object
  263. * cannot be serialized or there is another error,
  264. * the method will return <tt>null</tt>.
  265. * <p>
  266. * Valid options:<pre>
  267. * GZIP: gzip-compresses object before encoding it.
  268. * DONT_BREAK_LINES: don't break lines at 76 characters
  269. * <i>Note: Technically, this makes your encoding non-compliant.</i>
  270. * </pre>
  271. * <p>
  272. * Example: <code>encodeObject( myObj, Base64.GZIP )</code> or
  273. * <p>
  274. * Example: <code>encodeObject( myObj, Base64.GZIP | Base64.DONT_BREAK_LINES )</code>
  275. *
  276. * @param serializableObject The object to encode
  277. * @options Specified options
  278. * @return The Base64-encoded object
  279. * @see Base64#GZIP
  280. * @see Base64#DONT_BREAK_LINES
  281. * @since 2.0
  282. */
  283. public static String encodeObject(java.io.Serializable serializableObject, int options)
  284. {
  285. // Streams
  286. java.io.ByteArrayOutputStream baos = null;
  287. java.io.OutputStream b64os = null;
  288. java.io.ObjectOutputStream oos = null;
  289. java.util.zip.GZIPOutputStream gzos = null;
  290. // Isolate options
  291. int gzip = (options & GZIP);
  292. int dontBreakLines = (options & DONT_BREAK_LINES);
  293. try
  294. {
  295. // ObjectOutputStream -> (GZIP) -> Base64 -> ByteArrayOutputStream
  296. baos = new java.io.ByteArrayOutputStream();
  297. b64os = new Base64.OutputStream(baos, ENCODE | dontBreakLines);
  298. // GZip?
  299. if (gzip == GZIP)
  300. {
  301. gzos = new java.util.zip.GZIPOutputStream(b64os);
  302. oos = new java.io.ObjectOutputStream(gzos);
  303. } // end if: gzip
  304. else oos = new java.io.ObjectOutputStream(b64os);
  305. oos.writeObject(serializableObject);
  306. } // end try
  307. catch (java.io.IOException e)
  308. {
  309. e.printStackTrace();
  310. return null;
  311. } // end catch
  312. finally
  313. {
  314. try
  315. {
  316. oos.close();
  317. }
  318. catch (Exception e)
  319. {
  320. }
  321. try
  322. {
  323. gzos.close();
  324. }
  325. catch (Exception e)
  326. {
  327. }
  328. try
  329. {
  330. b64os.close();
  331. }
  332. catch (Exception e)
  333. {
  334. }
  335. try
  336. {
  337. baos.close();
  338. }
  339. catch (Exception e)
  340. {
  341. }
  342. } // end finally
  343. // Return value according to relevant encoding.
  344. try
  345. {
  346. return new String(baos.toByteArray(), PREFERRED_ENCODING);
  347. } // end try
  348. catch (java.io.UnsupportedEncodingException uue)
  349. {
  350. return new String(baos.toByteArray());
  351. } // end catch
  352. } // end encode
  353. /**
  354. * Encodes a byte array into Base64 notation.
  355. * Does not GZip-compress data.
  356. *
  357. * @param source The data to convert
  358. * @since 1.4
  359. */
  360. public static String encodeBytes(byte[] source)
  361. {
  362. return encodeBytes(source, 0, source.length, NO_OPTIONS);
  363. } // end encodeBytes
  364. /**
  365. * Encodes a byte array into Base64 notation.
  366. * <p>
  367. * Valid options:<pre>
  368. * GZIP: gzip-compresses object before encoding it.
  369. * DONT_BREAK_LINES: don't break lines at 76 characters
  370. * <i>Note: Technically, this makes your encoding non-compliant.</i>
  371. * </pre>
  372. * <p>
  373. * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or
  374. * <p>
  375. * Example: <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code>
  376. *
  377. *
  378. * @param source The data to convert
  379. * @param options Specified options
  380. * @see Base64#GZIP
  381. * @see Base64#DONT_BREAK_LINES
  382. * @since 2.0
  383. */
  384. public static String encodeBytes(byte[] source, int options)
  385. {
  386. return encodeBytes(source, 0, source.length, options);
  387. } // end encodeBytes
  388. /**
  389. * Encodes a byte array into Base64 notation.
  390. * Does not GZip-compress data.
  391. *
  392. * @param source The data to convert
  393. * @param off Offset in array where conversion should begin
  394. * @param len Length of data to convert
  395. * @since 1.4
  396. */
  397. public static String encodeBytes(byte[] source, int off, int len)
  398. {
  399. return encodeBytes(source, off, len, NO_OPTIONS);
  400. } // end encodeBytes
  401. /**
  402. * Encodes a byte array into Base64 notation.
  403. * <p>
  404. * Valid options:<pre>
  405. * GZIP: gzip-compresses object before encoding it.
  406. * DONT_BREAK_LINES: don't break lines at 76 characters
  407. * <i>Note: Technically, this makes your encoding non-compliant.</i>
  408. * </pre>
  409. * <p>
  410. * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or
  411. * <p>
  412. * Example: <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code>
  413. *
  414. *
  415. * @param source The data to convert
  416. * @param off Offset in array where conversion should begin
  417. * @param len Length of data to convert
  418. * @param breakLines Break lines at 80 characters or less.
  419. * @param options Specified options
  420. * @see Base64#GZIP
  421. * @see Base64#DONT_BREAK_LINES
  422. * @since 2.0
  423. */
  424. public static String encodeBytes(byte[] source, int off, int len, int options)
  425. {
  426. // Isolate options
  427. int dontBreakLines = (options & DONT_BREAK_LINES);
  428. int gzip = (options & GZIP);
  429. // Compress?
  430. if (gzip == GZIP)
  431. {
  432. java.io.ByteArrayOutputStream baos = null;
  433. java.util.zip.GZIPOutputStream gzos = null;
  434. Base64.OutputStream b64os = null;
  435. try
  436. {
  437. // GZip -> Base64 -> ByteArray
  438. baos = new java.io.ByteArrayOutputStream();
  439. b64os = new Base64.OutputStream(baos, ENCODE | dontBreakLines);
  440. gzos = new java.util.zip.GZIPOutputStream(b64os);
  441. gzos.write(source, off, len);
  442. gzos.close();
  443. } // end try
  444. catch (java.io.IOException e)
  445. {
  446. e.printStackTrace();
  447. return null;
  448. } // end catch
  449. finally
  450. {
  451. try
  452. {
  453. gzos.close();
  454. }
  455. catch (Exception e)
  456. {
  457. }
  458. try
  459. {
  460. b64os.close();
  461. }
  462. catch (Exception e)
  463. {
  464. }
  465. try
  466. {
  467. baos.close();
  468. }
  469. catch (Exception e)
  470. {
  471. }
  472. } // end finally
  473. // Return value according to relevant encoding.
  474. try
  475. {
  476. return new String(baos.toByteArray(), PREFERRED_ENCODING);
  477. } // end try
  478. catch (java.io.UnsupportedEncodingException uue)
  479. {
  480. return new String(baos.toByteArray());
  481. } // end catch
  482. } // end if: compress
  483. // Convert option to boolean in way that code likes it.
  484. boolean breakLines = dontBreakLines == 0;
  485. int len43 = len * 4 / 3;
  486. byte[] outBuff = new byte[(len43) // Main 4:3
  487. + ((len % 3) > 0 ? 4 : 0) // Account for padding
  488. + (breakLines ? (len43 / MAX_LINE_LENGTH) : 0)]; // New lines
  489. int d = 0;
  490. int e = 0;
  491. int len2 = len - 2;
  492. int lineLength = 0;
  493. for (; d < len2; d += 3, e += 4)
  494. {
  495. encode3to4(source, d + off, 3, outBuff, e);
  496. lineLength += 4;
  497. if (breakLines && lineLength == MAX_LINE_LENGTH)
  498. {
  499. outBuff[e + 4] = NEW_LINE;
  500. e++;
  501. lineLength = 0;
  502. } // end if: end of line
  503. } // en dfor: each piece of array
  504. if (d < len)
  505. {
  506. encode3to4(source, d + off, len - d, outBuff, e);
  507. e += 4;
  508. } // end if: some padding needed
  509. // Return value according to relevant encoding.
  510. try
  511. {
  512. return new String(outBuff, 0, e, PREFERRED_ENCODING);
  513. } // end try
  514. catch (java.io.UnsupportedEncodingException uue)
  515. {
  516. return new String(outBuff, 0, e);
  517. } // end catch
  518. // end else: don't compress
  519. } // end encodeBytes
  520. /* ******** D E C O D I N G M E T H O D S ******** */
  521. // /**
  522. // * Decodes the first four bytes of array <var>fourBytes</var>
  523. // * and returns an array up to three bytes long with the
  524. // * decoded values.
  525. // *
  526. // * @param fourBytes the array with Base64 content
  527. // * @return array with decoded values
  528. // * @since 1.3
  529. // */
  530. // private static byte[] decode4to3( byte[] fourBytes )
  531. // {
  532. // byte[] outBuff1 = new byte[3];
  533. // int count = decode4to3( fourBytes, 0, outBuff1, 0 );
  534. // byte[] outBuff2 = new byte[ count ];
  535. //
  536. // for( int i = 0; i < count; i++ )
  537. // outBuff2[i] = outBuff1[i];
  538. //
  539. // return outBuff2;
  540. // }
  541. /**
  542. * Decodes four bytes from array <var>source</var>
  543. * and writes the resulting bytes (up to three of them)
  544. * to <var>destination</var>.
  545. * The source and destination arrays can be manipulated
  546. * anywhere along their length by specifying
  547. * <var>srcOffset</var> and <var>destOffset</var>.
  548. * This method does not check to make sure your arrays
  549. * are large enough to accomodate <var>srcOffset</var> + 4 for
  550. * the <var>source</var> array or <var>destOffset</var> + 3 for
  551. * the <var>destination</var> array.
  552. * This method returns the actual number of bytes that
  553. * were converted from the Base64 encoding.
  554. *
  555. *
  556. * @param source the array to convert
  557. * @param srcOffset the index where conversion begins
  558. * @param destination the array to hold the conversion
  559. * @param destOffset the index where output will be put
  560. * @return the number of decoded bytes converted
  561. * @since 1.3
  562. */
  563. static int decode4to3(byte[] source, int srcOffset, byte[] destination, int destOffset)
  564. {
  565. // Example: Dk==
  566. if (source[srcOffset + 2] == EQUALS_SIGN)
  567. {
  568. // Two ways to do the same thing. Don't know which way I like best.
  569. //int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
  570. // | ( ( DECODABET[ source[ srcOffset + 1] ] << 24 ) >>> 12 );
  571. int outBuff = ((DECODABET[source[srcOffset]] & 0xFF) << 18)
  572. | ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12);
  573. destination[destOffset] = (byte) (outBuff >>> 16);
  574. return 1;
  575. }
  576. // Example: DkL=
  577. else if (source[srcOffset + 3] == EQUALS_SIGN)
  578. {
  579. // Two ways to do the same thing. Don't know which way I like best.
  580. //int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
  581. // | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 )
  582. // | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 );
  583. int outBuff = ((DECODABET[source[srcOffset]] & 0xFF) << 18)
  584. | ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12)
  585. | ((DECODABET[source[srcOffset + 2]] & 0xFF) << 6);
  586. destination[destOffset] = (byte) (outBuff >>> 16);
  587. destination[destOffset + 1] = (byte) (outBuff >>> 8);
  588. return 2;
  589. }
  590. // Example: DkLE
  591. else
  592. {
  593. try
  594. {
  595. // Two ways to do the same thing. Don't know which way I like best.
  596. //int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 )
  597. // | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 )
  598. // | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 )
  599. // | ( ( DECODABET[ source[ srcOffset + 3 ] ] << 24 ) >>> 24 );
  600. int outBuff = ((DECODABET[source[srcOffset]] & 0xFF) << 18)
  601. | ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12)
  602. | ((DECODABET[source[srcOffset + 2]] & 0xFF) << 6)
  603. | ((DECODABET[source[srcOffset + 3]] & 0xFF));
  604. destination[destOffset] = (byte) (outBuff >> 16);
  605. destination[destOffset + 1] = (byte) (outBuff >> 8);
  606. destination[destOffset + 2] = (byte) (outBuff);
  607. return 3;
  608. }
  609. catch (Exception e)
  610. {
  611. System.out.println("" + source[srcOffset] + ": " + (DECODABET[source[srcOffset]]));
  612. System.out.println("" + source[srcOffset + 1] + ": "
  613. + (DECODABET[source[srcOffset + 1]]));
  614. System.out.println("" + source[srcOffset + 2] + ": "
  615. + (DECODABET[source[srcOffset + 2]]));
  616. System.out.println("" + source[srcOffset + 3] + ": "
  617. + (DECODABET[source[srcOffset + 3]]));
  618. return -1;
  619. } //e nd catch
  620. }
  621. } // end decodeToBytes
  622. /**
  623. * Very low-level access to decoding ASCII characters in
  624. * the form of a byte array. Does not support automatically
  625. * gunzipping or any other "fancy" features.
  626. *
  627. * @param source The Base64 encoded data
  628. * @param off The offset of where to begin decoding
  629. * @param len The length of characters to decode
  630. * @return decoded data
  631. * @since 1.3
  632. */
  633. public static byte[] decode(byte[] source, int off, int len)
  634. {
  635. int len34 = len * 3 / 4;
  636. byte[] outBuff = new byte[len34]; // Upper limit on size of output
  637. int outBuffPosn = 0;
  638. byte[] b4 = new byte[4];
  639. int b4Posn = 0;
  640. int i = 0;
  641. byte sbiCrop = 0;
  642. byte sbiDecode = 0;
  643. for (i = off; i < off + len; i++)
  644. {
  645. sbiCrop = (byte) (source[i] & 0x7f); // Only the low seven bits
  646. sbiDecode = DECODABET[sbiCrop];
  647. if (sbiDecode >= WHITE_SPACE_ENC) // White space, Equals sign or better
  648. {
  649. if (sbiDecode >= EQUALS_SIGN_ENC)
  650. {
  651. b4[b4Posn++] = sbiCrop;
  652. if (b4Posn > 3)
  653. {
  654. outBuffPosn += decode4to3(b4, 0, outBuff, outBuffPosn);
  655. b4Posn = 0;
  656. // If that was the equals sign, break out of 'for' loop
  657. if (sbiCrop == EQUALS_SIGN) break;
  658. } // end if: quartet built
  659. } // end if: equals sign or better
  660. } // end if: white space, equals sign or better
  661. else
  662. {
  663. System.err.println("Bad Base64 input character at " + i + ": " + source[i] + "(decimal)");
  664. return null;
  665. } // end else:
  666. } // each input character
  667. byte[] out = new byte[outBuffPosn];
  668. System.arraycopy(outBuff, 0, out, 0, outBuffPosn);
  669. return out;
  670. } // end decode
  671. /**
  672. * Decodes data from Base64 notation, automatically
  673. * detecting gzip-compressed data and decompressing it.
  674. *
  675. * @param s the string to decode
  676. * @return the decoded data
  677. * @since 1.4
  678. */
  679. public static byte[] decode(String s)
  680. {
  681. byte[] bytes;
  682. try
  683. {
  684. bytes = s.getBytes(PREFERRED_ENCODING);
  685. } // end try
  686. catch (java.io.UnsupportedEncodingException uee)
  687. {
  688. bytes = s.getBytes();
  689. } // end catch
  690. //</change>
  691. // Decode
  692. bytes = decode(bytes, 0, bytes.length);
  693. // Check to see if it's gzip-compressed
  694. // GZIP Magic Two-Byte Number: 0x8b1f (35615)
  695. if (bytes.length >= 2)
  696. {
  697. int head = (bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
  698. if (bytes != null && // In case decoding returned null
  699. bytes.length >= 4 && // Don't want to get ArrayIndexOutOfBounds exception
  700. java.util.zip.GZIPInputStream.GZIP_MAGIC == head)
  701. {
  702. java.io.ByteArrayInputStream bais = null;
  703. java.util.zip.GZIPInputStream gzis = null;
  704. java.io.ByteArrayOutputStream baos = null;
  705. byte[] buffer = new byte[2048];
  706. int length = 0;
  707. try
  708. {
  709. baos = new java.io.ByteArrayOutputStream();
  710. bais = new java.io.ByteArrayInputStream(bytes);
  711. gzis = new java.util.zip.GZIPInputStream(bais);
  712. while ((length = gzis.read(buffer)) >= 0)
  713. {
  714. baos.write(buffer, 0, length);
  715. } // end while: reading input
  716. // No error? Get new bytes.
  717. bytes = baos.toByteArray();
  718. } // end try
  719. catch (java.io.IOException e)
  720. {
  721. // Just return originally-decoded bytes
  722. } // end catch
  723. finally
  724. {
  725. try
  726. {
  727. baos.close();
  728. }
  729. catch (Exception e)
  730. {
  731. }
  732. try
  733. {
  734. gzis.close();
  735. }
  736. catch (Exception e)
  737. {
  738. }
  739. try
  740. {
  741. bais.close();
  742. }
  743. catch (Exception e)
  744. {
  745. }
  746. } // end finally
  747. } // end if: gzipped
  748. } // end if: bytes.length >= 2
  749. return bytes;
  750. } // end decode
  751. /**
  752. * Attempts to decode Base64 data and deserialize a Java
  753. * Object within. Returns <tt>null</tt> if there was an error.
  754. *
  755. * @param encodedObject The Base64 data to decode
  756. * @return The decoded and deserialized object
  757. * @since 1.5
  758. */
  759. public static Object decodeToObject(String encodedObject)
  760. {
  761. // Decode and gunzip if necessary
  762. byte[] objBytes = decode(encodedObject);
  763. java.io.ByteArrayInputStream bais = null;
  764. java.io.ObjectInputStream ois = null;
  765. Object obj = null;
  766. try
  767. {
  768. bais = new java.io.ByteArrayInputStream(objBytes);
  769. ois = new java.io.ObjectInputStream(bais);
  770. obj = ois.readObject();
  771. } // end try
  772. catch (java.io.IOException e)
  773. {
  774. e.printStackTrace();
  775. obj = null;
  776. } // end catch
  777. catch (java.lang.ClassNotFoundException e)
  778. {
  779. e.printStackTrace();
  780. obj = null;
  781. } // end catch
  782. finally
  783. {
  784. try
  785. {
  786. bais.close();
  787. }
  788. catch (Exception e)
  789. {
  790. }
  791. try
  792. {
  793. ois.close();
  794. }
  795. catch (Exception e)
  796. {
  797. }
  798. } // end finally
  799. return obj;
  800. } // end decodeObject
  801. /* ******** I N N E R C L A S S I N P U T S T R E A M ******** */
  802. /**
  803. * A {@link Base64#InputStream} will read data from another
  804. * {@link java.io.InputStream}, given in the constructor,
  805. * and encode/decode to/from Base64 notation on the fly.
  806. *
  807. * @see Base64
  808. * @see java.io.FilterInputStream
  809. * @since 1.3
  810. */
  811. public static class InputStream extends java.io.FilterInputStream
  812. {
  813. //private int options; // Options specified
  814. private boolean encode; // Encoding or decoding
  815. private int position; // Current position in the buffer
  816. private byte[] buffer; // Small buffer holding converted data
  817. private int bufferLength; // Length of buffer (3 or 4)
  818. private int numSigBytes; // Number of meaningful bytes in the buffer
  819. private int lineLength;
  820. private boolean breakLines; // Break lines at less than 80 characters
  821. /**
  822. * Constructs a {@link Base64#InputStream} in DECODE mode.
  823. *
  824. * @param in the {@link java.io.InputStream} from which to read data.
  825. * @since 1.3
  826. */
  827. public InputStream(java.io.InputStream pIn)
  828. {
  829. this(pIn, DECODE);
  830. } // end constructor
  831. /**
  832. * Constructs a {@link Base64#InputStream} in
  833. * either ENCODE or DECODE mode.
  834. * <p>
  835. * Valid options:<pre>
  836. * ENCODE or DECODE: Encode or Decode as data is read.
  837. * DONT_BREAK_LINES: don't break lines at 76 characters
  838. * (only meaningful when encoding)
  839. * <i>Note: Technically, this makes your encoding non-compliant.</i>
  840. * </pre>
  841. * <p>
  842. * Example: <code>new Base64.InputStream( in, Base64.DECODE )</code>
  843. *
  844. *
  845. * @param in the {@link java.io.InputStream} from which to read data.
  846. * @param options Specified options
  847. * @see Base64#ENCODE
  848. * @see Base64#DECODE
  849. * @see Base64#DONT_BREAK_LINES
  850. * @since 2.0
  851. */
  852. public InputStream(java.io.InputStream pIn, int options)
  853. {
  854. super(pIn);
  855. //this.options = options;
  856. breakLines = (options & DONT_BREAK_LINES) != DONT_BREAK_LINES;
  857. encode = (options & ENCODE) == ENCODE;
  858. bufferLength = encode ? 4 : 3;
  859. buffer = new byte[bufferLength];
  860. position = -1;
  861. lineLength = 0;
  862. } // end constructor
  863. /**
  864. * Reads enough of the input stream to convert
  865. * to/from Base64 and returns the next byte.
  866. *
  867. * @return next byte
  868. * @since 1.3
  869. */
  870. @Override
  871. public int read() throws java.io.IOException
  872. {
  873. // Do we need to get data?
  874. if (position < 0)
  875. {
  876. if (encode)
  877. {
  878. byte[] b3 = new byte[3];
  879. int numBinaryBytes = 0;
  880. for (int i = 0; i < 3; i++)
  881. {
  882. try
  883. {
  884. int b = in.read();
  885. // If end of stream, b is -1.
  886. if (b >= 0)
  887. {
  888. b3[i] = (byte) b;
  889. numBinaryBytes++;
  890. } // end if: not end of stream
  891. } // end try: read
  892. catch (java.io.IOException e)
  893. {
  894. // Only a problem if we got no data at all.
  895. if (i == 0) throw e;
  896. } // end catch
  897. } // end for: each needed input byte
  898. if (numBinaryBytes > 0)
  899. {
  900. encode3to4(b3, 0, numBinaryBytes, buffer, 0);
  901. position = 0;
  902. numSigBytes = 4;
  903. } // end if: got data
  904. else
  905. {
  906. return -1;
  907. } // end else
  908. } // end if: encoding
  909. // Else decoding
  910. else
  911. {
  912. byte[] b4 = new byte[4];
  913. int i = 0;
  914. for (i = 0; i < 4; i++)
  915. {
  916. // Read four "meaningful" bytes:
  917. int b = 0;
  918. do
  919. {
  920. b = in.read();
  921. }
  922. while (b >= 0 && DECODABET[b & 0x7f] <= WHITE_SPACE_ENC);
  923. if (b < 0) break; // Reads a -1 if end of stream
  924. b4[i] = (byte) b;
  925. } // end for: each needed input byte
  926. if (i == 4)
  927. {
  928. numSigBytes = decode4to3(b4, 0, buffer, 0);
  929. position = 0;
  930. } // end if: got four characters
  931. else if (i == 0)
  932. {
  933. return -1;
  934. } // end else if: also padded correctly
  935. else
  936. {
  937. // Must have broken out from above.
  938. throw new java.io.IOException("Improperly padded Base64 input.");
  939. } // end
  940. } // end else: decode
  941. } // end else: get data
  942. // Got data?
  943. if (position >= 0)
  944. {
  945. // End of relevant data?
  946. if ( /*!encode &&*/position >= numSigBytes) return -1;
  947. if (encode && breakLines && lineLength >= MAX_LINE_LENGTH)
  948. {
  949. lineLength = 0;
  950. return '\n';
  951. } // end if
  952. lineLength++; // This isn't important when decoding
  953. // but throwing an extra "if" seems
  954. // just as wasteful.
  955. int b = buffer[position++];
  956. if (position >= bufferLength) position = -1;
  957. return b & 0xFF; // This is how you "cast" a byte that's
  958. // intended to be unsigned.
  959. // end else
  960. } // end if: position >= 0
  961. // When JDK1.4 is more accepted, use an assertion here.
  962. throw new java.io.IOException("Error in Base64 code reading stream.");
  963. // end else
  964. } // end read
  965. /**
  966. * Calls {@link #read} repeatedly until the end of stream
  967. * is reached or <var>len</var> bytes are read.
  968. * Returns number of bytes read into array or -1 if
  969. * end of stream is encountered.
  970. *
  971. * @param dest array to hold values
  972. * @param off offset for array
  973. * @param len max number of bytes to read into array
  974. * @return bytes read into array or -1 if end of stream is encountered.
  975. * @since 1.3
  976. */
  977. @Override
  978. public int read(byte[] dest, int off, int len) throws java.io.IOException
  979. {
  980. int i;
  981. int b;
  982. for (i = 0; i < len; i++)
  983. {
  984. b = read();
  985. //if( b < 0 && i == 0 )
  986. // return -1;
  987. if (b >= 0) dest[off + i] = (byte) b;
  988. else if (i == 0) return -1;
  989. else break; // Out of 'for' loop
  990. } // end for: each byte read
  991. return i;
  992. } // end read
  993. } // end inner class InputStream
  994. /* ******** I N N E R C L A S S O U T P U T S T R E A M ******** */
  995. /**
  996. * A {@link Base64#OutputStream} will write data to another
  997. * {@link java.io.OutputStream}, given in the constructor,
  998. * and encode/decode to/from Base64 notation on the fly.
  999. *
  1000. * @see Base64
  1001. * @see java.io.FilterOutputStream
  1002. * @since 1.3
  1003. */
  1004. public static class OutputStream extends java.io.FilterOutputStream
  1005. {
  1006. //private int options;
  1007. private boolean encode;
  1008. private int position;
  1009. private byte[] buffer;
  1010. private int bufferLength;
  1011. private int lineLength;
  1012. private boolean breakLines;
  1013. private byte[] b4; // Scratch used in a few places
  1014. private boolean suspendEncoding;
  1015. /**
  1016. * Constructs a {@link Base64#OutputStream} in ENCODE mode.
  1017. *
  1018. * @param out the {@link java.io.OutputStream} to which data will be written.
  1019. * @since 1.3
  1020. */
  1021. public OutputStream(java.io.OutputStream pOut)
  1022. {
  1023. this(pOut, ENCODE);
  1024. } // end constructor
  1025. /**
  1026. * Constructs a {@link Base64#OutputStream} in
  1027. * either ENCODE or DECODE mode.
  1028. * <p>
  1029. * Valid options:<pre>
  1030. * ENCODE or DECODE: Encode or Decode as data is read.
  1031. * DONT_BREAK_LINES: don't break lines at 76 characters
  1032. * (only meaningful when encoding)
  1033. * <i>Note: Technically, this makes your encoding non-compliant.</i>
  1034. * </pre>
  1035. * <p>
  1036. * Example: <code>new Base64.OutputStream( out, Base64.ENCODE )</code>
  1037. *
  1038. * @param out the {@link java.io.OutputStream} to which data will be written.
  1039. * @param options Specified options.
  1040. * @see Base64#ENCODE
  1041. * @see Base64#DECODE
  1042. * @see Base64#DONT_BREAK_LINES
  1043. * @since 1.3
  1044. */
  1045. public OutputStream(java.io.OutputStream pOut, int options)
  1046. {
  1047. super(pOut);
  1048. //this.options = options;
  1049. breakLines = (options & DONT_BREAK_LINES) != DONT_BREAK_LINES;
  1050. encode = (options & ENCODE) == ENCODE;
  1051. bufferLength = encode ? 3 : 4;
  1052. buffer = new byte[bufferLength];
  1053. position = 0;
  1054. lineLength = 0;
  1055. suspendEncoding = false;
  1056. b4 = new byte[4];
  1057. } // end constructor
  1058. /**
  1059. * Writes the byte to the output stream after
  1060. * converting to/from Base64 notation.
  1061. * When encoding, bytes are buffered three
  1062. * at a time before the output stream actually
  1063. * gets a write() call.
  1064. * When decoding, bytes are buffered four
  1065. * at a time.
  1066. *
  1067. * @param theByte the byte to write
  1068. * @since 1.3
  1069. */
  1070. @Override
  1071. public void write(int theByte) throws java.io.IOException
  1072. {
  1073. // Encoding suspended?
  1074. if (suspendEncoding)
  1075. {
  1076. super.out.write(theByte);
  1077. return;
  1078. } // end if: supsended
  1079. // Encode?
  1080. if (encode)
  1081. {
  1082. buffer[position++] = (byte) theByte;
  1083. if (position >= bufferLength) // Enough to encode.
  1084. {
  1085. out.write(encode3to4(b4, buffer, bufferLength));
  1086. lineLength += 4;
  1087. if (breakLines && lineLength >= MAX_LINE_LENGTH)
  1088. {
  1089. out.write(NEW_LINE);
  1090. lineLength = 0;
  1091. } // end if: end of line
  1092. position = 0;
  1093. } // end if: enough to output
  1094. } // end if: encoding
  1095. // Else, Decoding
  1096. else
  1097. {
  1098. // Meaningful Base64 character?
  1099. if (DECODABET[theByte & 0x7f] > WHITE_SPACE_ENC)
  1100. {
  1101. buffer[position++] = (byte) theByte;
  1102. if (position >= bufferLength) // Enough to output.
  1103. {
  1104. int len = Base64.decode4to3(buffer, 0, b4, 0);
  1105. out.write(b4, 0, len);
  1106. //out.write( Base64.decode4to3( buffer ) );
  1107. position = 0;
  1108. } // end if: enough to output
  1109. } // end if: meaningful base64 character
  1110. else if (DECODABET[theByte & 0x7f] != WHITE_SPACE_ENC)
  1111. {
  1112. throw new java.io.IOException("Invalid character in Base64 data.");
  1113. } // end else: not white space either
  1114. } // end else: decoding
  1115. } // end write
  1116. /**
  1117. * Calls {@link #write} repeatedly until <var>len</var>
  1118. * bytes are written.
  1119. *
  1120. * @param theBytes array from which to read bytes
  1121. * @param off offset for array
  1122. * @param len max number of bytes to read into array
  1123. * @since 1.3
  1124. */
  1125. @Override
  1126. public void write(byte[] theBytes, int off, int len) throws java.io.IOException
  1127. {
  1128. // Encoding suspended?
  1129. if (suspendEncoding)
  1130. {
  1131. super.out.write(theBytes, off, len);
  1132. return;
  1133. } // end if: supsended
  1134. for (int i = 0; i < len; i++)
  1135. {
  1136. write(theBytes[off + i]);
  1137. } // end for: each byte written
  1138. } // end write
  1139. /**
  1140. * Method added by PHIL. [Thanks, PHIL. -Rob]
  1141. * This pads the buffer without closing the stream.
  1142. */
  1143. public void flushBase64() throws java.io.IOException
  1144. {
  1145. if (position > 0)
  1146. {
  1147. if (encode)
  1148. {
  1149. out.write(encode3to4(b4, buffer, position));
  1150. position = 0;
  1151. } // end if: encoding
  1152. else
  1153. {
  1154. throw new java.io.IOException("Base64 input not properly padded.");
  1155. } // end else: decoding
  1156. } // end if: buffer partially full
  1157. } // end flush
  1158. /**
  1159. * Flushes and closes (I think, in the superclass) the stream.
  1160. *
  1161. * @since 1.3
  1162. */
  1163. @Override
  1164. public void close() throws java.io.IOException
  1165. {
  1166. // 1. Ensure that pending characters are written
  1167. flushBase64();
  1168. // 2. Actually close the stream
  1169. // Base class both flushes and closes.
  1170. super.close();
  1171. buffer = null;
  1172. out = null;
  1173. } // end close
  1174. /**
  1175. * Suspends encoding of the stream.
  1176. * May be helpful if you need to embed a piece of
  1177. * base640-encoded data in a stream.
  1178. *
  1179. * @since 1.5.1
  1180. */
  1181. public void suspendEncoding() throws java.io.IOException
  1182. {
  1183. flushBase64();
  1184. suspendEncoding = true;
  1185. } // end suspendEncoding
  1186. /**
  1187. * Resumes encoding of the stream.
  1188. * May be helpful if you need to embed a piece of
  1189. * base640-encoded data in a stream.
  1190. *
  1191. * @since 1.5.1
  1192. */
  1193. public void resumeEncoding()
  1194. {
  1195. suspendEncoding = false;
  1196. } // end resumeEncoding
  1197. } // end inner class OutputStream
  1198. } // end class Base64