Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id CD5E3200D70 for ; Thu, 21 Dec 2017 16:19:48 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id CC012160C1A; Thu, 21 Dec 2017 15:19:48 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 676D1160C34 for ; Thu, 21 Dec 2017 16:19:46 +0100 (CET) Received: (qmail 47139 invoked by uid 500); 21 Dec 2017 15:19:41 -0000 Mailing-List: contact commits-help@hbase.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@hbase.apache.org Delivered-To: mailing list commits@hbase.apache.org Received: (qmail 45447 invoked by uid 99); 21 Dec 2017 15:19:39 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 21 Dec 2017 15:19:39 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id BB6FAE0779; Thu, 21 Dec 2017 15:19:38 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: git-site-role@apache.org To: commits@hbase.apache.org Date: Thu, 21 Dec 2017 15:20:02 -0000 Message-Id: <9e17a29e8ec749c49b17ff7c9566663c@git.apache.org> In-Reply-To: <1359deaf8338404688f941344e4cae94@git.apache.org> References: <1359deaf8338404688f941344e4cae94@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [25/51] [partial] hbase-site git commit: Published site at . archived-at: Thu, 21 Dec 2017 15:19:49 -0000 http://git-wip-us.apache.org/repos/asf/hbase-site/blob/505bbb2e/apidocs/src-html/org/apache/hadoop/hbase/util/Base64.Base64OutputStream.html ---------------------------------------------------------------------- diff --git a/apidocs/src-html/org/apache/hadoop/hbase/util/Base64.Base64OutputStream.html b/apidocs/src-html/org/apache/hadoop/hbase/util/Base64.Base64OutputStream.html index 70481ce..b9f6622 100644 --- a/apidocs/src-html/org/apache/hadoop/hbase/util/Base64.Base64OutputStream.html +++ b/apidocs/src-html/org/apache/hadoop/hbase/util/Base64.Base64OutputStream.html @@ -47,1648 +47,1649 @@ 039import java.util.zip.GZIPInputStream; 040import java.util.zip.GZIPOutputStream; 041 -042import org.apache.commons.logging.Log; -043import org.apache.commons.logging.LogFactory; -044import org.apache.yetus.audience.InterfaceAudience; -045 -046/** -047 * Encodes and decodes to and from Base64 notation. -048 * -049 * <p> -050 * Homepage: <a href="http://iharder.net/base64">http://iharder.net/base64</a>. -051 * </p> -052 * -053 * <p> -054 * Change Log: -055 * </p> -056 * <ul> -057 * <li>v2.2.1 - Fixed bug using URL_SAFE and ORDERED encodings. Fixed bug -058 * when using very small files (~&lt; 40 bytes).</li> -059 * <li>v2.2 - Added some helper methods for encoding/decoding directly from -060 * one file to the next. Also added a main() method to support command -061 * line encoding/decoding from one file to the next. Also added these -062 * Base64 dialects: -063 * <ol> -064 * <li>The default is RFC3548 format.</li> -065 * <li>Using Base64.URLSAFE generates URL and file name friendly format as -066 * described in Section 4 of RFC3548. -067 * http://www.faqs.org/rfcs/rfc3548.html</li> -068 * <li>Using Base64.ORDERED generates URL and file name friendly format -069 * that preserves lexical ordering as described in -070 * http://www.faqs.org/qa/rfcc-1940.html</li> -071 * </ol> -072 * <p> -073 * Special thanks to Jim Kellerman at <a href="http://www.powerset.com/"> -074 * http://www.powerset.com/</a> for contributing the new Base64 dialects. -075 * </li> -076 * -077 * <li>v2.1 - Cleaned up javadoc comments and unused variables and methods. -078 * Added some convenience methods for reading and writing to and from files. -079 * </li> -080 * <li>v2.0.2 - Now specifies UTF-8 encoding in places where the code fails on -081 * systems with other encodings (like EBCDIC).</li> -082 * <li>v2.0.1 - Fixed an error when decoding a single byte, that is, when the -083 * encoded data was a single byte.</li> -084 * <li>v2.0 - I got rid of methods that used booleans to set options. Now -085 * everything is more consolidated and cleaner. The code now detects when -086 * data that's being decoded is gzip-compressed and will decompress it -087 * automatically. Generally things are cleaner. You'll probably have to -088 * change some method calls that you were making to support the new options -089 * format (<tt>int</tt>s that you "OR" together).</li> -090 * <li>v1.5.1 - Fixed bug when decompressing and decoding to a byte[] using -091 * <tt>decode( String s, boolean gzipCompressed )</tt>. Added the ability to -092 * "suspend" encoding in the Output Stream so you can turn on and off the -093 * encoding if you need to embed base64 data in an otherwise "normal" stream -094 * (like an XML file).</li> -095 * <li>v1.5 - Output stream pases on flush() command but doesn't do anything -096 * itself. This helps when using GZIP streams. Added the ability to -097 * GZip-compress objects before encoding them.</li> -098 * <li>v1.4 - Added helper methods to read/write files.</li> -099 * <li>v1.3.6 - Fixed OutputStream.flush() so that 'position' is reset.</li> -100 * <li>v1.3.5 - Added flag to turn on and off line breaks. Fixed bug in input -101 * stream where last buffer being read, if not completely full, was not -102 * returned.</li> -103 * <li>v1.3.4 - Fixed when "improperly padded stream" error was thrown at the -104 * wrong time.</li> -105 * <li>v1.3.3 - Fixed I/O streams which were totally messed up.</li> -106 * </ul> -107 * -108 * <p> -109 * I am placing this code in the Public Domain. Do with it as you will. This -110 * software comes with no guarantees or warranties but with plenty of -111 * well-wishing instead! -112 * <p> -113 * Please visit <a href="http://iharder.net/base64">http://iharder.net/base64</a> -114 * periodically to check for updates or to contribute improvements. -115 * <p> -116 * author: Robert Harder, rob@iharder.net -117 * <br> -118 * version: 2.2.1 -119 */ -120@InterfaceAudience.Public -121public class Base64 { -122 -123 /* ******** P U B L I C F I E L D S ******** */ -124 -125 /** No options specified. Value is zero. */ -126 public final static int NO_OPTIONS = 0; -127 -128 /** Specify encoding. */ -129 public final static int ENCODE = 1; -130 -131 /** Specify decoding. */ -132 public final static int DECODE = 0; -133 -134 /** Specify that data should be gzip-compressed. */ -135 public final static int GZIP = 2; -136 -137 /** Don't break lines when encoding (violates strict Base64 specification) */ -138 public final static int DONT_BREAK_LINES = 8; -139 -140 /** -141 * Encode using Base64-like encoding that is URL and Filename safe as -142 * described in Section 4 of RFC3548: -143 * <a href="http://www.faqs.org/rfcs/rfc3548.html"> -144 * http://www.faqs.org/rfcs/rfc3548.html</a>. -145 * It is important to note that data encoded this way is <em>not</em> -146 * officially valid Base64, or at the very least should not be called Base64 -147 * without also specifying that is was encoded using the URL and -148 * Filename safe dialect. -149 */ -150 public final static int URL_SAFE = 16; -151 -152 /** -153 * Encode using the special "ordered" dialect of Base64 described here: -154 * <a href="http://www.faqs.org/qa/rfcc-1940.html"> -155 * http://www.faqs.org/qa/rfcc-1940.html</a>. -156 */ -157 public final static int ORDERED = 32; -158 -159 /* ******** P R I V A T E F I E L D S ******** */ -160 -161 private static final Log LOG = LogFactory.getLog(Base64.class); -162 -163 /** Maximum line length (76) of Base64 output. */ -164 private final static int MAX_LINE_LENGTH = 76; -165 -166 /** The equals sign (=) as a byte. */ -167 private final static byte EQUALS_SIGN = (byte) '='; -168 -169 /** The new line character (\n) as a byte. */ -170 private final static byte NEW_LINE = (byte) '\n'; -171 -172 /** Preferred encoding. */ -173 private final static String PREFERRED_ENCODING = "UTF-8"; -174 -175 private final static byte WHITE_SPACE_ENC = -5; // Indicates white space -176 private final static byte EQUALS_SIGN_ENC = -1; // Indicates equals sign -177 -178 /* ******** S T A N D A R D B A S E 6 4 A L P H A B E T ******** */ -179 -180 /** The 64 valid Base64 values. */ -181 -182 /* -183 * Host platform may be something funny like EBCDIC, so we hardcode these -184 * values. -185 */ -186 private final static byte[] _STANDARD_ALPHABET = { (byte) 'A', (byte) 'B', -187 (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', -188 (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', -189 (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', -190 (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', -191 (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f', -192 (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l', -193 (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', -194 (byte) 's', (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x', -195 (byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2', (byte) '3', -196 (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', -197 (byte) '+', (byte) '/' -198 }; -199 -200 /** -201 * Translates a Base64 value to either its 6-bit reconstruction value or a -202 * negative number indicating some other meaning. -203 */ -204 private final static byte[] _STANDARD_DECODABET = { -205 -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 0 - 8 -206 -5, -5, // Whitespace: Tab, Newline -207 -9, -9, // Decimal 11 - 12 -208 -5, // Whitespace: Return -209 -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 14 - 26 -210 -9, -9, -9, -9, -9, // Decimal 27 - 31 -211 -5, // Whitespace: Space -212 -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 33 - 42 -213 62, // Plus sign at decimal 43 -214 -9, -9, -9, // Decimal 44 - 46 -215 63, // Slash at decimal 47 -216 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // Numbers zero - nine -217 -9, -9, -9, // Decimal 58 - 60 -218 -1, // Equals sign at decimal 61 -219 -9, -9, -9, // Decimal 62 - 64 -220 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // Letters 'A' - 'N' -221 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // Letters 'O' - 'Z' -222 -9, -9, -9, -9, -9, -9, // Decimal 91 - 96 -223 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // Letters 'a' - 'm' -224 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // Letters 'n' -'z' -225 -9, -9, -9, -9 // Decimal 123 - 126 -226 }; -227 -228 /* ******** U R L S A F E B A S E 6 4 A L P H A B E T ******** */ -229 -230 /** -231 * Used in the URL and Filename safe dialect described in Section 4 of RFC3548 -232 * <a href="http://www.faqs.org/rfcs/rfc3548.html"> -233 * http://www.faqs.org/rfcs/rfc3548.html</a>. -234 * Notice that the last two bytes become "hyphen" and "underscore" instead of -235 * "plus" and "slash." -236 */ -237 private final static byte[] _URL_SAFE_ALPHABET = { (byte) 'A', (byte) 'B', -238 (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', -239 (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', -240 (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', -241 (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', -242 (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f', -243 (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l', -244 (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', -245 (byte) 's', (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x', -246 (byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2', (byte) '3', -247 (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', -248 (byte) '-', (byte) '_' -249 }; -250 -251 /** -252 * Used in decoding URL and Filename safe dialects of Base64. -253 */ -254 private final static byte[] _URL_SAFE_DECODABET = { -255 -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 0 - 8 -256 -5, -5, // Whitespace: Tab, Newline -257 -9, -9, // Decimal 11 - 12 -258 -5, // Whitespace: Return -259 -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 14 - 26 -260 -9, -9, -9, -9, -9, // Decimal 27 - 31 -261 -5, // Whitespace: Space -262 -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 33 - 42 -263 -9, // Plus sign at 43 -264 -9, // Decimal 44 -265 62, // Minus sign at 45 -266 -9, // Decimal 46 -267 -9, // Slash at 47 -268 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // Numbers 0 - 9 -269 -9, -9, -9, // Decimal 58 - 60 -270 -1, // Equals sign at 61 -271 -9, -9, -9, // Decimal 62 - 64 -272 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, // Letters 'A' - 'N' -273 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // Letters 'O' - 'Z' -274 -9, -9, -9, -9, // Decimal 91 - 94 -275 63, // Underscore at 95 -276 -9, // Decimal 96 -277 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, // Letters 'a' - 'm' -278 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // Letters 'n' - 'z' -279 -9, -9, -9, -9 // Decimal 123 - 126 -280 }; -281 -282 /* ******** O R D E R E D B A S E 6 4 A L P H A B E T ******** */ -283 -284 /** -285 * In addition to being URL and file name friendly, this encoding preserves -286 * the sort order of encoded values. Whatever is input, be it string or -287 * just an array of bytes, when you use this encoding, the encoded value sorts -288 * exactly the same as the input value. It is described in the RFC change -289 * request: <a href="http://www.faqs.org/qa/rfcc-1940.html"> -290 * http://www.faqs.org/qa/rfcc-1940.html</a>. -291 * -292 * It replaces "plus" and "slash" with "hyphen" and "underscore" and -293 * rearranges the alphabet so that the characters are in their natural sort -294 * order. -295 */ -296 private final static byte[] _ORDERED_ALPHABET = { (byte) '-', (byte) '0', -297 (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6', -298 (byte) '7', (byte) '8', (byte) '9', (byte) 'A', (byte) 'B', (byte) 'C', -299 (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', -300 (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O', -301 (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', -302 (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) '_', -303 (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f', -304 (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l', -305 (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', -306 (byte) 's', (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x', -307 (byte) 'y', (byte) 'z' -308 }; -309 -310 /** -311 * Used in decoding the "ordered" dialect of Base64. -312 */ -313 private final static byte[] _ORDERED_DECODABET = { -314 -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 0 - 8 -315 -5, -5, // Whitespace: Tab, Newline -316 -9, -9, // Decimal 11 - 12 -317 -5, // Whitespace: Return -318 -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 14 - 26 -319 -9, -9, -9, -9, -9, // Decimal 27 - 31 -320 -5, // Whitespace: Space -321 -9, -9, -9, -9, -9, -9, -9, -9, -9, -9, // Decimal 33 - 42 -322 -9, // Plus sign at 43 -323 -9, // Decimal 44 -324 0, // Minus sign at 45 -325 -9, // Decimal 46 -326 -9, // Slash at decimal 47 -327 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, // Numbers 0 - 9 -328 -9, -9, -9, // Decimal 58 - 60 -329 -1, // Equals sign at 61 -330 -9, -9, -9, // Decimal 62 - 64 -331 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, // Letters 'A' - 'M' -332 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, // Letters 'N' - 'Z' -333 -9, -9, -9, -9, // Decimal 91 - 94 -334 37, // Underscore at 95 -335 -9, // Decimal 96 -336 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, // Letters 'a' - 'm' -337 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, // Letters 'n' - 'z' -338 -9, -9, -9, -9 // Decimal 123 - 126 -339 }; -340 -341 /* ******** D E T E R M I N E W H I C H A L H A B E T ******** */ -342 -343 /** -344 * Returns one of the _SOMETHING_ALPHABET byte arrays depending on the options -345 * specified. It's possible, though silly, to specify ORDERED and URLSAFE in -346 * which case one of them will be picked, though there is no guarantee as to -347 * which one will be picked. -348 * -349 * @param options URL_SAFE or ORDERED -350 * @return alphabet array to use -351 */ -352 protected static byte[] getAlphabet(int options) { -353 if ((options & URL_SAFE) == URL_SAFE) { -354 return _URL_SAFE_ALPHABET; -355 -356 } else if ((options & ORDERED) == ORDERED) { -357 return _ORDERED_ALPHABET; -358 -359 } else { -360 return _STANDARD_ALPHABET; -361 } -362 } // end getAlphabet -363 -364 /** -365 * Returns one of the _SOMETHING_DECODABET byte arrays depending on the -366 * options specified. It's possible, though silly, to specify ORDERED and -367 * URL_SAFE in which case one of them will be picked, though there is no -368 * guarantee as to which one will be picked. -369 * @param options URL_SAFE or ORDERED -370 * @return alphabet array to use -371 */ -372 protected static byte[] getDecodabet(int options) { -373 if ((options & URL_SAFE) == URL_SAFE) { -374 return _URL_SAFE_DECODABET; -375 -376 } else if ((options & ORDERED) == ORDERED) { -377 return _ORDERED_DECODABET; -378 -379 } else { -380 return _STANDARD_DECODABET; -381 } -382 } // end getDecodabet -383 -384 /** Defeats instantiation. */ -385 private Base64() {} -386 -387 /** -388 * Main program. Used for testing. -389 * -390 * Encodes or decodes two files from the command line -391 * -392 * @param args command arguments -393 */ -394 public static void main(String[] args) { -395 if (args.length < 3) { -396 usage("Not enough arguments."); -397 -398 } else { -399 String flag = args[0]; -400 String infile = args[1]; -401 String outfile = args[2]; -402 if (flag.equals("-e")) { // encode -403 encodeFileToFile(infile, outfile); -404 -405 } else if (flag.equals("-d")) { // decode -406 decodeFileToFile(infile, outfile); -407 -408 } else { -409 usage("Unknown flag: " + flag); -410 } -411 } -412 } // end main -413 -414 /** -415 * Prints command line usage. -416 * -417 * @param msg A message to include with usage info. -418 */ -419 private static void usage(String msg) { -420 System.err.println(msg); -421 System.err.println("Usage: java Base64 -e|-d inputfile outputfile"); -422 } // end usage -423 -424 /* ******** E N C O D I N G M E T H O D S ******** */ -425 -426 /** -427 * Encodes up to the first three bytes of array <var>threeBytes</var> and -428 * returns a four-byte array in Base64 notation. The actual number of -429 * significant bytes in your array is given by <var>numSigBytes</var>. The -430 * array <var>threeBytes</var> needs only be as big as <var>numSigBytes</var>. -431 * Code can reuse a byte array by passing a four-byte array as <var>b4</var>. -432 * -433 * @param b4 A reusable byte array to reduce array instantiation -434 * @param threeBytes the array to convert -435 * @param numSigBytes the number of significant bytes in your array -436 * @param options options for get alphabet -437 * @return four byte array in Base64 notation. -438 * @since 1.5.1 -439 */ -440 protected static byte[] encode3to4(byte[] b4, byte[] threeBytes, -441 int numSigBytes, int options) { -442 encode3to4(threeBytes, 0, numSigBytes, b4, 0, options); -443 return b4; -444 } // end encode3to4 -445 -446 /** -447 * Encodes up to three bytes of the array <var>source</var> and writes the -448 * resulting four Base64 bytes to <var>destination</var>. The source and -449 * destination arrays can be manipulated anywhere along their length by -450 * specifying <var>srcOffset</var> and <var>destOffset</var>. This method -451 * does not check to make sure your arrays are large enough to accomodate -452 * <var>srcOffset</var> + 3 for the <var>source</var> array or -453 * <var>destOffset</var> + 4 for the <var>destination</var> array. The -454 * actual number of significant bytes in your array is given by -455 * <var>numSigBytes</var>. -456 * <p> -457 * This is the lowest level of the encoding methods with all possible -458 * parameters. -459 * -460 * @param source the array to convert -461 * @param srcOffset the index where conversion begins -462 * @param numSigBytes the number of significant bytes in your array -463 * @param destination the array to hold the conversion -464 * @param destOffset the index where output will be put -465 * @param options options for get alphabet -466 * @return the <var>destination</var> array -467 * @since 1.3 -468 */ -469 protected static byte[] encode3to4(byte[] source, int srcOffset, -470 int numSigBytes, byte[] destination, int destOffset, int options) { -471 byte[] ALPHABET = getAlphabet(options); -472 -473 // 1 2 3 -474 // 01234567890123456789012345678901 Bit position -475 // --------000000001111111122222222 Array position from threeBytes -476 // --------| || || || | Six bit groups to index ALPHABET -477 // >>18 >>12 >> 6 >> 0 Right shift necessary -478 // 0x3f 0x3f 0x3f Additional AND -479 -480 // Create buffer with zero-padding if there are only one or two -481 // significant bytes passed in the array. -482 // We have to shift left 24 in order to flush out the 1's that appear -483 // when Java treats a value as negative that is cast from a byte to an int. -484 int inBuff = -485 (numSigBytes > 0 ? ((source[srcOffset] << 24) >>> 8) : 0) -486 | (numSigBytes > 1 ? ((source[srcOffset + 1] << 24) >>> 16) : 0) -487 | (numSigBytes > 2 ? ((source[srcOffset + 2] << 24) >>> 24) : 0); -488 -489 switch (numSigBytes) { -490 case 3: -491 destination[destOffset] = ALPHABET[(inBuff >>> 18)]; -492 destination[destOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3f]; -493 destination[destOffset + 2] = ALPHABET[(inBuff >>> 6) & 0x3f]; -494 destination[destOffset + 3] = ALPHABET[(inBuff) & 0x3f]; -495 return destination; -496 -497 case 2: -498 destination[destOffset] = ALPHABET[(inBuff >>> 18)]; -499 destination[destOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3f]; -500 destination[destOffset + 2] = ALPHABET[(inBuff >>> 6) & 0x3f]; -501 destination[destOffset + 3] = EQUALS_SIGN; -502 return destination; -503 -504 case 1: -505 destination[destOffset] = ALPHABET[(inBuff >>> 18)]; -506 destination[destOffset + 1] = ALPHABET[(inBuff >>> 12) & 0x3f]; -507 destination[destOffset + 2] = EQUALS_SIGN; -508 destination[destOffset + 3] = EQUALS_SIGN; -509 return destination; -510 -511 default: -512 return destination; -513 } // end switch -514 } // end encode3to4 -515 -516 /** -517 * Serializes an object and returns the Base64-encoded version of that -518 * serialized object. If the object cannot be serialized or there is another -519 * error, the method will return <tt>null</tt>. The object is not -520 * GZip-compressed before being encoded. -521 * -522 * @param serializableObject The object to encode -523 * @return The Base64-encoded object -524 * @since 1.4 -525 */ -526 public static String encodeObject(Serializable serializableObject) { -527 return encodeObject(serializableObject, NO_OPTIONS); -528 } // end encodeObject -529 -530 /** -531 * Serializes an object and returns the Base64-encoded version of that -532 * serialized object. If the object cannot be serialized or there is another -533 * error, the method will return <tt>null</tt>. -534 * <p> -535 * Valid options: -536 * <ul> -537 * <li>GZIP: gzip-compresses object before encoding it.</li> -538 * <li>DONT_BREAK_LINES: don't break lines at 76 characters. <i>Note: -539 * Technically, this makes your encoding non-compliant.</i></li> -540 * </ul> -541 * <p> -542 * Example: <code>encodeObject( myObj, Base64.GZIP )</code> or -543 * <p> -544 * Example: -545 * <code>encodeObject( myObj, Base64.GZIP | Base64.DONT_BREAK_LINES )</code> -546 * -547 * @param serializableObject The object to encode -548 * @param options Specified options -549 * @see Base64#GZIP -550 * @see Base64#DONT_BREAK_LINES -551 * @return The Base64-encoded object -552 * @since 2.0 -553 */ -554 @SuppressWarnings({"ConstantConditions"}) -555 public static String encodeObject(Serializable serializableObject, -556 int options) { -557 -558 ByteArrayOutputStream baos = new ByteArrayOutputStream(); -559 OutputStream b64os = null; -560 ObjectOutputStream oos = null; -561 try { -562 // ObjectOutputStream -> (GZIP) -> Base64 -> ByteArrayOutputStream -563 b64os = new Base64OutputStream(baos, ENCODE | options); -564 -565 oos = ((options & GZIP) == GZIP) ? -566 new ObjectOutputStream(new GZIPOutputStream(b64os)) : -567 new ObjectOutputStream(b64os); -568 -569 oos.writeObject(serializableObject); -570 return new String(baos.toByteArray(), PREFERRED_ENCODING); -571 -572 } catch (UnsupportedEncodingException uue) { -573 return new String(baos.toByteArray(), StandardCharsets.UTF_8); -574 -575 } catch (IOException e) { -576 LOG.error("error encoding object", e); -577 return null; -578 -579 } finally { -580 if (oos != null) { -581 try { -582 oos.close(); -583 } catch (Exception e) { -584 LOG.error("error closing ObjectOutputStream", e); -585 } -586 } -587 if (b64os != null) { -588 try { -589 b64os.close(); -590 } catch (Exception e) { -591 LOG.error("error closing Base64OutputStream", e); -592 } -593 } -594 try { -595 baos.close(); -596 } catch (Exception e) { -597 LOG.error("error closing ByteArrayOutputStream", e); -598 } -599 } // end finally -600 } // end encode -601 -602 /** -603 * Encodes a byte array into Base64 notation. Does not GZip-compress data. -604 * -605 * @param source The data to convert -606 * @return encoded byte array -607 * @since 1.4 -608 */ -609 public static String encodeBytes(byte[] source) { -610 return encodeBytes(source, 0, source.length, NO_OPTIONS); -611 } // end encodeBytes -612 -613 /** -614 * Encodes a byte array into Base64 notation. -615 * <p> -616 * Valid options: -617 * <ul> -618 * <li>GZIP: gzip-compresses object before encoding it.</li> -619 * <li>DONT_BREAK_LINES: don't break lines at 76 characters. <i>Note: -620 * Technically, this makes your encoding non-compliant.</i></li> -621 * </ul> -622 * -623 * <p> -624 * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or -625 * <p> -626 * Example: -627 * <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code> -628 * -629 * @param source The data to convert -630 * @param options Specified options -631 * @see Base64#GZIP -632 * @see Base64#DONT_BREAK_LINES -633 * @see Base64#URL_SAFE -634 * @see Base64#ORDERED -635 * @return encoded byte array -636 * @since 2.0 -637 */ -638 public static String encodeBytes(byte[] source, int options) { -639 return encodeBytes(source, 0, source.length, options); -640 } // end encodeBytes -641 -642 /** -643 * Encodes a byte array into Base64 notation. Does not GZip-compress data. -644 * -645 * @param source The data to convert -646 * @param off Offset in array where conversion should begin -647 * @param len Length of data to convert -648 * @return encoded byte array -649 * @since 1.4 -650 */ -651 public static String encodeBytes(byte[] source, int off, int len) { -652 return encodeBytes(source, off, len, NO_OPTIONS); -653 } // end encodeBytes -654 -655 /** -656 * Encodes a byte array into Base64 notation. -657 * <p> -658 * Valid options: -659 * <ul> -660 * <li>GZIP: gzip-compresses object before encoding it.</li> -661 * <li>DONT_BREAK_LINES: don't break lines at 76 characters. <i>Note: -662 * Technically, this makes your encoding non-compliant.</i></li> -663 * </ul> -664 * -665 * <p> -666 * Example: <code>encodeBytes( myData, Base64.GZIP )</code> or -667 * <p> -668 * Example: -669 * <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code> -670 * -671 * @param source The data to convert -672 * @param off Offset in array where conversion should begin -673 * @param len Length of data to convert -674 * @param options Specified options -675 * @see Base64#GZIP -676 * @see Base64#DONT_BREAK_LINES -677 * @see Base64#URL_SAFE -678 * @see Base64#ORDERED -679 * @return encoded byte array -680 * @since 2.0 -681 */ -682 public static String encodeBytes(byte[] source, int off, int len, int options) { -683 if ((options & GZIP) == GZIP) { // Compress? -684 // GZip -> Base64 -> ByteArray -685 ByteArrayOutputStream baos = new ByteArrayOutputStream(); -686 GZIPOutputStream gzos = null; -687 -688 try { -689 gzos = -690 new GZIPOutputStream(new Base64OutputStream(baos, ENCODE | options)); -691 -692 gzos.write(source, off, len); -693 gzos.close(); -694 gzos = null; -695 return new String(baos.toByteArray(), PREFERRED_ENCODING); -696 -697 } catch (UnsupportedEncodingException uue) { -698 return new String(baos.toByteArray(), StandardCharsets.UTF_8); -699 -700 } catch (IOException e) { -701 LOG.error("error encoding byte array", e); -702 return null; -703 -704 } finally { -705 if (gzos != null) { -706 try { -707 gzos.close(); -708 } catch (Exception e) { -709 LOG.error("error closing GZIPOutputStream", e); -710 } -711 } -712 try { -713 baos.close(); -714 } catch (Exception e) { -715 LOG.error("error closing ByteArrayOutputStream", e); -716 } -717 } // end finally -718 -719 } // end Compress -720 -721 // Don't compress. Better not to use streams at all then. -722 -723 boolean breakLines = ((options & DONT_BREAK_LINES) == 0); -724 -725 int len43 = len * 4 / 3; -726 byte[] outBuff = -727 new byte[(len43) // Main 4:3 -728 + ((len % 3) > 0 ? 4 : 0) // padding -729 + (breakLines ? (len43 / MAX_LINE_LENGTH) : 0)]; // New lines -730 int d = 0; -731 int e = 0; -732 int len2 = len - 2; -733 int lineLength = 0; -734 for (; d < len2; d += 3, e += 4) { -735 encode3to4(source, d + off, 3, outBuff, e, options); -736 -737 lineLength += 4; -738 if (breakLines && lineLength == MAX_LINE_LENGTH) { -739 outBuff[e + 4] = NEW_LINE; -740 e++; -741 lineLength = 0; -742 } // end if: end of line -743 } // end for: each piece of array -744 -745 if (d < len) { -746 encode3to4(source, d + off, len - d, outBuff, e, options); -747 e += 4; -748 } // end if: some padding needed -749 -750 // Return value according to relevant encoding. -751 try { -752 return new String(outBuff, 0, e, PREFERRED_ENCODING); -753 -754 } catch (UnsupportedEncodingException uue) { -755 return new String(outBuff, 0, e, StandardCharsets.UTF_8); -756 } -757 } // end encodeBytes -758 -759 /* ******** D E C O D I N G M E T H O D S ******** */ -760 -761 /** -762 * Decodes four bytes from array <var>source</var> and writes the resulting -763 * bytes (up to three of them) to <var>destination</var>. The source and -764 * destination arrays can be manipulated anywhere along their length by -765 * specifying <var>srcOffset</var> and <var>destOffset</var>. This method -766 * does not check to make sure your arrays are large enough to accomodate -767 * <var>srcOffset</var> + 4 for the <var>source</var> array or -768 * <var>destOffset</var> + 3 for the <var>destination</var> array. This -769 * method returns the actual number of bytes that were converted from the -770 * Base64 encoding. -771 * <p> -772 * This is the lowest level of the decoding methods with all possible -773 * parameters. -774 * </p> -775 * -776 * @param source the array to convert -777 * @param srcOffset the index where conversion begins -778 * @param destination the array to hold the conversion -779 * @param destOffset the index where output will be put -780 * @param options options for getDecoabet -781 * @see Base64#URL_SAFE -782 * @see Base64#ORDERED -783 * @return the number of decoded bytes converted -784 * @since 1.3 -785 */ -786 @SuppressWarnings({"ConstantConditions"}) -787 protected static int decode4to3(byte[] source, int srcOffset, -788 byte[] destination, int destOffset, int options) { -789 byte[] DECODABET = getDecodabet(options); -790 -791 if (source[srcOffset + 2] == EQUALS_SIGN) { // Example: Dk== -792 // Two ways to do the same thing. Don't know which way I like best. -793 // int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 ) -794 // | ( ( DECODABET[ source[ srcOffset + 1] ] << 24 ) >>> 12 ); -795 int outBuff = -796 ((DECODABET[source[srcOffset]] & 0xFF) << 18) -797 | ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12); -798 -799 destination[destOffset] = (byte) (outBuff >>> 16); -800 return 1; -801 -802 } else if (source[srcOffset + 3] == EQUALS_SIGN) { // Example: DkL= -803 // Two ways to do the same thing. Don't know which way I like best. -804 // int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 ) -805 // | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 ) -806 // | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 ); -807 int outBuff = -808 ((DECODABET[source[srcOffset]] & 0xFF) << 18) -809 | ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12) -810 | ((DECODABET[source[srcOffset + 2]] & 0xFF) << 6); -811 -812 destination[destOffset] = (byte) (outBuff >>> 16); -813 destination[destOffset + 1] = (byte) (outBuff >>> 8); -814 return 2; -815 -816 } else { // Example: DkLE -817 try { -818 // Two ways to do the same thing. Don't know which way I like best. -819 // int outBuff = ( ( DECODABET[ source[ srcOffset ] ] << 24 ) >>> 6 ) -820 // | ( ( DECODABET[ source[ srcOffset + 1 ] ] << 24 ) >>> 12 ) -821 // | ( ( DECODABET[ source[ srcOffset + 2 ] ] << 24 ) >>> 18 ) -822 // | ( ( DECODABET[ source[ srcOffset + 3 ] ] << 24 ) >>> 24 ); -823 int outBuff = -824 ((DECODABET[source[srcOffset]] & 0xFF) << 18) -825 | ((DECODABET[source[srcOffset + 1]] & 0xFF) << 12) -826 | ((DECODABET[source[srcOffset + 2]] & 0xFF) << 6) -827 | ((DECODABET[source[srcOffset + 3]] & 0xFF)); -828 -829 destination[destOffset] = (byte) (outBuff >> 16); -830 destination[destOffset + 1] = (byte) (outBuff >> 8); -831 destination[destOffset + 2] = (byte) (outBuff); -832 -833 return 3; -834 -835 } catch (Exception e) { -836 LOG.error("error decoding bytes at " + source[srcOffset] + ": " + -837 (DECODABET[source[srcOffset]]) + ", " + source[srcOffset + 1] + -838 ": " + (DECODABET[source[srcOffset + 1]]) + ", " + -839 source[srcOffset + 2] + ": " + (DECODABET[source[srcOffset + 2]]) + -840 ", " + source[srcOffset + 3] + ": " + -841 (DECODABET[source[srcOffset + 3]]), e); -842 return -1; -843 } // end catch -844 } -845 } // end decodeToBytes -846 -847 /** -848 * Very low-level access to decoding ASCII characters in the form of a byte -849 * array. Does not support automatically gunzipping or any other "fancy" -850 * features. -851 * -852 * @param source The Base64 encoded data -853 * @param off The offset of where to begin decoding -854 * @param len The length of characters to decode -855 * @param options options for getDecodabet -856 * @see Base64#URL_SAFE -857 * @see Base64#ORDERED -858 * @return decoded data -859 * @since 1.3 -860 */ -861 public static byte[] decode(byte[] source, int off, int len, int options) { -862 byte[] DECODABET = getDecodabet(options); -863 -864 int len34 = len * 3 / 4; -865 byte[] outBuff = new byte[len34]; // Upper limit on size of output -866 int outBuffPosn = 0; -867 -868 byte[] b4 = new byte[4]; -869 int b4Posn = 0; -870 int i; -871 byte sbiCrop; -872 byte sbiDecode; -873 for (i = off; i < off + len; i++) { -874 sbiCrop = (byte) (source[i] & 0x7f); // Only the low seven bits -875 sbiDecode = DECODABET[sbiCrop]; -876 -877 if (sbiDecode >= WHITE_SPACE_ENC) { // Whitespace, Equals or better -878 if (sbiDecode >= EQUALS_SIGN_ENC) { // Equals or better -879 b4[b4Posn++] = sbiCrop; -880 if (b4Posn > 3) { -881 outBuffPosn += decode4to3(b4, 0, outBuff, outBuffPosn, options); -882 b4Posn = 0; -883 -884 // If that was the equals sign, break out of 'for' loop -885 if (sbiCrop == EQUALS_SIGN) -886 break; -887 } // end if: quartet built -888 } // end if: equals sign or better -889 } else { -890 LOG.error("Bad Base64 input character at " + i + ": " + source[i] + -891 "(decimal)"); -892 return null; -893 } // end else: -894 } // each input character -