1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.net.util; 19 20 import java.math.BigInteger; 21 import java.nio.charset.StandardCharsets; 22 import java.util.Base64.Decoder; 23 import java.util.Base64.Encoder; 24 import java.util.Objects; 25 26 /** 27 * Provides Base64 encoding and decoding as defined by RFC 2045. 28 * 29 * <p> 30 * This class implements section <cite>6.8. Base64 Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose Internet Mail Extensions (MIME) Part One: 31 * Format of Internet Message Bodies</cite> by Freed and Borenstein. 32 * </p> 33 * <p> 34 * The class can be parameterized in the following manner with various constructors: 35 * <ul> 36 * <li>URL-safe mode: Default off.</li> 37 * <li>Line length: Default 76. Line length that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data. 38 * <li>Line separator: Default is CRLF ("\r\n")</li> 39 * </ul> 40 * <p> 41 * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode character encodings which are 42 * compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc). 43 * </p> 44 * 45 * @deprecated Use {@link java.util.Base64}. 46 * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045</a> 47 * @since 2.2 48 */ 49 @Deprecated 50 public class Base64 { 51 52 /** 53 * Chunk size per RFC 2045 section 6.8. 54 * 55 * <p> 56 * The {@value} character limit does not count the trailing CRLF, but counts all other characters, including any equal signs. 57 * </p> 58 * 59 * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 6.8</a> 60 */ 61 static final int CHUNK_SIZE = 76; 62 63 /** 64 * Chunk separator per RFC 2045 section 2.1. 65 * 66 * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045 section 2.1</a> 67 */ 68 static final byte[] CHUNK_SEPARATOR = { '\r', '\n' }; 69 70 /** 71 * Byte used to pad output. 72 */ 73 private static final byte PAD = '='; 74 75 /** 76 * This array is a lookup table that translates Unicode characters drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045) into their 6-bit 77 * positive integer equivalents. Characters that are not in the Base64 alphabet but fall within the bounds of the array are translated to -1. 78 * 79 * Note: '+' and '-' both decode to 62. '/' and '_' both decode to 63. This means decoder seamlessly handles both URL_SAFE and STANDARD base64. (The 80 * encoder, on the other hand, needs to know ahead of time what to emit). 81 * 82 * Thanks to "commons" project in ws.apache.org for <a href="https://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/">this code</a> 83 */ 84 private static final byte[] DECODE_TABLE = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 85 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 86 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, -1, -1, -1, -1, 63, -1, 26, 27, 28, 29, 30, 31, 32, 87 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 }; 88 89 // The static final fields above are used for the original static byte[] methods on Base64. 90 // The private member fields below are used with the new streaming approach, which requires 91 // some state be preserved between calls of encode() and decode(). 92 93 /** 94 * Tests a given byte array to see if it contains any valid character within the Base64 alphabet. 95 * 96 * @param arrayOctet byte array to test 97 * @return {@code true} if any byte is a valid character in the Base64 alphabet; {@code false} otherwise 98 */ 99 private static boolean containsBase64Byte(final byte[] arrayOctet) { 100 for (final byte element : arrayOctet) { 101 if (isBase64(element)) { 102 return true; 103 } 104 } 105 return false; 106 } 107 108 /** 109 * Decodes Base64 data into octets. 110 * 111 * @param base64 Byte array containing Base64 data 112 * @return Array containing decoded data. 113 */ 114 public static byte[] decodeBase64(final byte[] base64) { 115 return isEmpty(base64) ? base64 : getDecoder().decode(base64); 116 } 117 118 /** 119 * Decodes a Base64 String into octets. 120 * 121 * @param base64 String containing Base64 data 122 * @return Array containing decoded data. 123 * @since 1.4 124 */ 125 public static byte[] decodeBase64(final String base64) { 126 return getDecoder().decode(base64); 127 } 128 129 /** 130 * Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signature 131 * 132 * @param source a byte array containing base64 character data 133 * @return A BigInteger 134 * @since 1.4 135 */ 136 public static BigInteger decodeInteger(final byte[] source) { 137 return new BigInteger(1, decodeBase64(source)); 138 } 139 140 private static byte[] encode(final byte[] binaryData, final int lineLength, final byte[] lineSeparator, final boolean urlSafe) { 141 if (isEmpty(binaryData)) { 142 return binaryData; 143 } 144 return lineLength > 0 ? encodeBase64Chunked(binaryData, lineLength, lineSeparator) 145 : urlSafe ? encodeBase64URLSafe(binaryData) : encodeBase64(binaryData); 146 } 147 148 /** 149 * Encodes binary data using the base64 algorithm but does not chunk the output. 150 * 151 * @param source binary data to encode 152 * @return byte[] containing Base64 characters in their UTF-8 representation. 153 */ 154 public static byte[] encodeBase64(final byte[] source) { 155 return isEmpty(source) ? source : getEncoder().encode(source); 156 } 157 158 /** 159 * Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks. 160 * 161 * @param binaryData Array containing binary data to encode. 162 * @param chunked if {@code true} this encoder will chunk the base64 output into 76 character blocks 163 * @return Base64-encoded data. 164 * @throws IllegalArgumentException Thrown when the input array needs an output array bigger than {@link Integer#MAX_VALUE} 165 */ 166 public static byte[] encodeBase64(final byte[] binaryData, final boolean chunked) { 167 return chunked ? encodeBase64Chunked(binaryData) : encodeBase64(binaryData, false, false); 168 } 169 170 /** 171 * Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks. 172 * 173 * @param binaryData Array containing binary data to encode. 174 * @param chunked if {@code true} this encoder will chunk the base64 output into 76 character blocks 175 * @param urlSafe if {@code true} this encoder will emit - and _ instead of the usual + and / characters. 176 * @return Base64-encoded data. 177 * @throws IllegalArgumentException Thrown when the input array needs an output array bigger than {@link Integer#MAX_VALUE} 178 * @since 1.4 179 */ 180 public static byte[] encodeBase64(final byte[] binaryData, final boolean chunked, final boolean urlSafe) { 181 return encodeBase64(binaryData, chunked, urlSafe, Integer.MAX_VALUE); 182 } 183 184 /** 185 * Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks. 186 * 187 * @param binaryData Array containing binary data to encode. 188 * @param chunked if {@code true} this encoder will chunk the base64 output into 76 character blocks 189 * @param urlSafe if {@code true} this encoder will emit - and _ instead of the usual + and / characters. 190 * @param maxResultSize The maximum result size to accept. 191 * @return Base64-encoded data. 192 * @throws IllegalArgumentException Thrown when the input array needs an output array bigger than maxResultSize 193 * @since 1.4 194 */ 195 public static byte[] encodeBase64(final byte[] binaryData, final boolean chunked, final boolean urlSafe, final int maxResultSize) { 196 if (isEmpty(binaryData)) { 197 return binaryData; 198 } 199 final long len = getEncodeLength(binaryData, chunked ? CHUNK_SIZE : 0, chunked ? CHUNK_SEPARATOR : NetConstants.EMPTY_BTYE_ARRAY); 200 if (len > maxResultSize) { 201 throw new IllegalArgumentException( 202 "Input array too big, the output array would be bigger (" + len + ") than the specified maxium size of " + maxResultSize); 203 } 204 return chunked ? encodeBase64Chunked(binaryData) : urlSafe ? encodeBase64URLSafe(binaryData) : encodeBase64(binaryData); 205 } 206 207 /** 208 * Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks separated by CR-LF. 209 * <p> 210 * The return value ends in a CR-LF. 211 * </p> 212 * 213 * @param binaryData binary data to encode 214 * @return Base64 characters chunked in 76 character blocks 215 * @throws ArithmeticException if the {@code binaryData} would overflows a byte[]. 216 */ 217 public static byte[] encodeBase64Chunked(final byte[] binaryData) { 218 return encodeBase64Chunked(binaryData, CHUNK_SIZE, CHUNK_SEPARATOR); 219 } 220 221 private static byte[] encodeBase64Chunked(final byte[] binaryData, final int lineLength, final byte[] lineSeparator) { 222 final long encodeLength = getEncodeLength(binaryData, lineLength, lineSeparator); 223 final byte[] dst = new byte[Math.toIntExact(encodeLength)]; 224 getMimeEncoder(lineLength, lineSeparator).encode(binaryData, dst); 225 // Copy chunk separator at the end 226 System.arraycopy(lineSeparator, 0, dst, dst.length - lineSeparator.length, lineSeparator.length); 227 return dst; 228 } 229 230 /** 231 * Encodes binary data using the base64 algorithm into 76 character blocks separated by CR-LF. 232 * <p> 233 * The return value ends in a CR-LF. 234 * </p> 235 * <p> 236 * For a non-chunking version, see {@link #encodeBase64StringUnChunked(byte[])}. 237 * </p> 238 * 239 * @param binaryData binary data to encode 240 * @return String containing Base64 characters. 241 * @since 1.4 242 */ 243 public static String encodeBase64String(final byte[] binaryData) { 244 return getMimeEncoder().encodeToString(binaryData) + "\r\n"; 245 } 246 247 /** 248 * Encodes binary data using the base64 algorithm. 249 * 250 * @param binaryData binary data to encode 251 * @param chunked whether to split the output into chunks 252 * @return String containing Base64 characters. 253 * @since 3.2 254 */ 255 public static String encodeBase64String(final byte[] binaryData, final boolean chunked) { 256 return newStringUtf8(encodeBase64(binaryData, chunked)); 257 } 258 259 /** 260 * Encodes binary data using the base64 algorithm, without using chunking. 261 * <p> 262 * For a chunking version, see {@link #encodeBase64String(byte[])}. 263 * </p> 264 * 265 * @param binaryData binary data to encode 266 * @return String containing Base64 characters. 267 * @since 3.2 268 */ 269 public static String encodeBase64StringUnChunked(final byte[] binaryData) { 270 return getEncoder().encodeToString(binaryData); 271 } 272 273 /** 274 * Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The url-safe variation emits - and _ instead of + 275 * and / characters. 276 * 277 * @param binaryData binary data to encode 278 * @return byte[] containing Base64 characters in their UTF-8 representation. 279 * @since 1.4 280 */ 281 public static byte[] encodeBase64URLSafe(final byte[] binaryData) { 282 return getUrlEncoder().withoutPadding().encode(binaryData); 283 } 284 285 /** 286 * Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The url-safe variation emits - and _ instead of + 287 * and / characters. 288 * 289 * @param binaryData binary data to encode 290 * @return String containing Base64 characters 291 * @since 1.4 292 */ 293 public static String encodeBase64URLSafeString(final byte[] binaryData) { 294 return getUrlEncoder().withoutPadding().encodeToString(binaryData); 295 } 296 297 /** 298 * Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature 299 * 300 * @param bigInt a BigInteger 301 * @return A byte array containing base64 character data 302 * @throws NullPointerException if null is passed in 303 * @since 1.4 304 */ 305 public static byte[] encodeInteger(final BigInteger bigInt) { 306 return encodeBase64(toIntegerBytes(bigInt), false); 307 } 308 309 private static Decoder getDecoder() { 310 return java.util.Base64.getDecoder(); 311 } 312 313 /** 314 * Pre-calculates the amount of space needed to base64-encode the supplied array. 315 * 316 * @param array byte[] array which will later be encoded 317 * @param lineSize line-length of the output (<= 0 means no chunking) between each chunkSeparator (e.g. CRLF). 318 * @param linkSeparator the sequence of bytes used to separate chunks of output (e.g. CRLF). 319 * 320 * @return amount of space needed to encode the supplied array. Returns a long since a max-len array will require Integer.MAX_VALUE + 33%. 321 */ 322 static long getEncodeLength(final byte[] array, int lineSize, final byte[] linkSeparator) { 323 // base64 always encodes to multiples of 4. 324 lineSize = lineSize / 4 * 4; 325 long len = array.length * 4 / 3; 326 final long mod = len % 4; 327 if (mod != 0) { 328 len += 4 - mod; 329 } 330 if (lineSize > 0) { 331 final boolean lenChunksPerfectly = len % lineSize == 0; 332 len += len / lineSize * linkSeparator.length; 333 if (!lenChunksPerfectly) { 334 len += linkSeparator.length; 335 } 336 } 337 return len; 338 } 339 340 private static Encoder getEncoder() { 341 return java.util.Base64.getEncoder(); 342 } 343 344 private static Encoder getMimeEncoder() { 345 return java.util.Base64.getMimeEncoder(); 346 } 347 348 private static Encoder getMimeEncoder(final int lineLength, final byte[] lineSeparator) { 349 return java.util.Base64.getMimeEncoder(lineLength, lineSeparator); 350 } 351 352 private static Encoder getUrlEncoder() { 353 return java.util.Base64.getUrlEncoder(); 354 } 355 356 /** 357 * Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently, the method treats whitespace as valid. 358 * 359 * @param arrayOctet byte array to test 360 * @return {@code true} if all bytes are valid characters in the Base64 alphabet or if the byte array is empty; false, otherwise 361 */ 362 public static boolean isArrayByteBase64(final byte[] arrayOctet) { 363 for (final byte element : arrayOctet) { 364 if (!isBase64(element) && !isWhiteSpace(element)) { 365 return false; 366 } 367 } 368 return true; 369 } 370 371 /** 372 * Returns whether or not the <code>octet</code> is in the base 64 alphabet. 373 * 374 * @param octet The value to test 375 * @return {@code true} if the value is defined in the base 64 alphabet, {@code false} otherwise. 376 * @since 1.4 377 */ 378 public static boolean isBase64(final byte octet) { 379 return octet == PAD || octet >= 0 && octet < DECODE_TABLE.length && DECODE_TABLE[octet] != -1; 380 } 381 382 private static boolean isEmpty(final byte[] array) { 383 return array == null || array.length == 0; 384 } 385 386 /** 387 * Checks if a byte value is whitespace or not. 388 * 389 * @param byteToCheck the byte to check 390 * @return true if byte is whitespace, false otherwise 391 */ 392 private static boolean isWhiteSpace(final byte byteToCheck) { 393 switch (byteToCheck) { 394 case ' ': 395 case '\n': 396 case '\r': 397 case '\t': 398 return true; 399 default: 400 return false; 401 } 402 } 403 404 private static String newStringUtf8(final byte[] encode) { 405 return new String(encode, StandardCharsets.UTF_8); 406 } 407 408 /** 409 * Returns a byte-array representation of a <code>BigInteger</code> without sign bit. 410 * 411 * @param bigInt <code>BigInteger</code> to be converted 412 * @return a byte array representation of the BigInteger parameter 413 */ 414 private static byte[] toIntegerBytes(final BigInteger bigInt) { 415 Objects.requireNonNull(bigInt, "bigInt"); 416 int bitlen = bigInt.bitLength(); 417 // round bitlen 418 bitlen = bitlen + 7 >> 3 << 3; 419 final byte[] bigBytes = bigInt.toByteArray(); 420 if (bigInt.bitLength() % 8 != 0 && bigInt.bitLength() / 8 + 1 == bitlen / 8) { 421 return bigBytes; 422 } 423 // set up params for copying everything but sign bit 424 int startSrc = 0; 425 int len = bigBytes.length; 426 427 // if bigInt is exactly byte-aligned, just skip signbit in copy 428 if (bigInt.bitLength() % 8 == 0) { 429 startSrc = 1; 430 len--; 431 } 432 final int startDst = bitlen / 8 - len; // to pad w/ nulls as per spec 433 final byte[] resizedBytes = new byte[bitlen / 8]; 434 System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, len); 435 return resizedBytes; 436 } 437 438 /** 439 * Line length for encoding. Not used when decoding. A value of zero or less implies no chunking of the base64 encoded data. 440 */ 441 private final int lineLength; 442 443 /** 444 * Line separator for encoding. Not used when decoding. Only used if lineLength > 0. 445 */ 446 private final byte[] lineSeparator; 447 448 /** 449 * Whether encoding is URL and filename safe, or not. 450 */ 451 private final boolean urlSafe; 452 453 /** 454 * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode. 455 * <p> 456 * When encoding the line length is 76, the line separator is CRLF, and the encoding table is STANDARD_ENCODE_TABLE. 457 * </p> 458 * 459 * <p> 460 * When decoding all variants are supported. 461 * </p> 462 */ 463 public Base64() { 464 this(false); 465 } 466 467 /** 468 * Creates a Base64 codec used for decoding (all modes) and encoding in the given URL-safe mode. 469 * <p> 470 * When encoding the line length is 76, the line separator is CRLF, and the encoding table is STANDARD_ENCODE_TABLE. 471 * </p> 472 * 473 * <p> 474 * When decoding all variants are supported. 475 * </p> 476 * 477 * @param urlSafe if {@code true}, URL-safe encoding is used. In most cases this should be set to {@code false}. 478 * @since 1.4 479 */ 480 public Base64(final boolean urlSafe) { 481 this(CHUNK_SIZE, CHUNK_SEPARATOR, urlSafe); 482 } 483 484 /** 485 * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode. 486 * <p> 487 * When encoding the line length is given in the constructor, the line separator is CRLF, and the encoding table is STANDARD_ENCODE_TABLE. 488 * </p> 489 * <p> 490 * Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data. 491 * </p> 492 * <p> 493 * When decoding all variants are supported. 494 * </p> 495 * 496 * @param lineLength Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 4). 497 * If {@code lineLength <= 0}, then the output will not be divided into lines (chunks). Ignored when decoding. 498 * @since 1.4 499 */ 500 public Base64(final int lineLength) { 501 this(lineLength, CHUNK_SEPARATOR); 502 } 503 504 /** 505 * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode. 506 * <p> 507 * When encoding the line length and line separator are given in the constructor, and the encoding table is STANDARD_ENCODE_TABLE. 508 * </p> 509 * <p> 510 * Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data. 511 * </p> 512 * <p> 513 * When decoding all variants are supported. 514 * </p> 515 * 516 * @param lineLength Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 4). 517 * If {@code lineLength <= 0}, then the output will not be divided into lines (chunks). Ignored when decoding. 518 * @param lineSeparator Each line of encoded data will end with this sequence of bytes. Not used for decoding. 519 * @throws IllegalArgumentException Thrown when the provided lineSeparator included some base64 characters. 520 * @since 1.4 521 */ 522 public Base64(final int lineLength, final byte[] lineSeparator) { 523 this(lineLength, lineSeparator, false); 524 } 525 526 /** 527 * Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode. 528 * <p> 529 * When encoding the line length and line separator are given in the constructor, and the encoding table is STANDARD_ENCODE_TABLE. 530 * </p> 531 * <p> 532 * Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data. 533 * </p> 534 * <p> 535 * When decoding all variants are supported. 536 * </p> 537 * 538 * @param lineLength Each line of encoded data will be at most of the given length (rounded down to the nearest multiple of 4). 539 * If {@code lineLength <= 0}, then the output will not be divided into lines (chunks). Ignored when decoding. 540 * @param lineSeparator Each line of encoded data will end with this sequence of bytes. Not used for decoding. 541 * @param urlSafe Instead of emitting '+' and '/' we emit '-' and '_' respectively. urlSafe is only applied to encode operations. Decoding seamlessly 542 * handles both modes. 543 * @throws IllegalArgumentException The provided lineSeparator included some base64 characters. That's not going to work! 544 * @since 1.4 545 */ 546 public Base64(int lineLength, byte[] lineSeparator, final boolean urlSafe) { 547 if (lineSeparator == null || urlSafe) { 548 lineLength = 0; // disable chunk-separating 549 lineSeparator = NetConstants.EMPTY_BTYE_ARRAY; // this just gets ignored 550 } 551 this.lineLength = lineLength > 0 ? lineLength / 4 * 4 : 0; 552 this.lineSeparator = new byte[lineSeparator.length]; 553 System.arraycopy(lineSeparator, 0, this.lineSeparator, 0, lineSeparator.length); 554 if (containsBase64Byte(lineSeparator)) { 555 final String sep = newStringUtf8(lineSeparator); 556 throw new IllegalArgumentException("lineSeperator must not contain base64 characters: [" + sep + "]"); 557 } 558 this.urlSafe = urlSafe; 559 } 560 561 /** 562 * Decodes a byte array containing characters in the Base64 alphabet. 563 * 564 * @param source A byte array containing Base64 character data 565 * @return a byte array containing binary data; will return {@code null} if provided byte array is {@code null}. 566 */ 567 public byte[] decode(final byte[] source) { 568 return isEmpty(source) ? source : getDecoder().decode(source); 569 } 570 571 /** 572 * Decodes a String containing characters in the Base64 alphabet. 573 * 574 * @param source A String containing Base64 character data, must not be {@code null} 575 * @return a byte array containing binary data 576 * @since 1.4 577 */ 578 public byte[] decode(final String source) { 579 return getDecoder().decode(source); 580 } 581 582 /** 583 * Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 alphabet. 584 * 585 * @param source a byte array containing binary data 586 * @return A byte array containing only Base64 character data 587 */ 588 public byte[] encode(final byte[] source) { 589 return encode(source, lineLength, lineSeparator, isUrlSafe()); 590 } 591 592 /** 593 * Encodes a byte[] containing binary data, into a String containing characters in the Base64 alphabet. 594 * 595 * @param source a byte array containing binary data 596 * @return A String containing only Base64 character data 597 * @since 1.4 598 */ 599 public String encodeToString(final byte[] source) { 600 return newStringUtf8(encode(source)); 601 } 602 603 int getLineLength() { 604 return lineLength; 605 } 606 607 byte[] getLineSeparator() { 608 return lineSeparator.clone(); 609 } 610 611 /** 612 * Tests whether our current encoding mode. True if we're URL-SAFE, false otherwise. 613 * 614 * @return true if we're in URL-SAFE mode, false otherwise. 615 * @since 1.4 616 */ 617 public boolean isUrlSafe() { 618 return urlSafe; 619 } 620 }