@@ -540,7 +540,7 @@ void decode(byte[] in, int inPos, int inAvail) {
540540 }
541541 byte b = in [inPos ++];
542542 if (b == PAD ) {
543- // WE'RE DONE!!!!
543+ // We're done.
544544 eof = true ;
545545 break ;
546546 } else {
@@ -628,25 +628,49 @@ private static boolean containsBase64Byte(byte[] arrayOctet) {
628628 *
629629 * @param binaryData
630630 * binary data to encode
631- * @return Base64 characters
631+ * @return byte[] containing Base64 characters in their UTF-8 representation.
632632 */
633633 public static byte [] encodeBase64 (byte [] binaryData ) {
634634 return encodeBase64 (binaryData , false );
635635 }
636636
637+ /**
638+ * Encodes binary data using the base64 algorithm into 76 character blocks separated by CRLF.
639+ *
640+ * @param binaryData
641+ * binary data to encode
642+ * @return String containing Base64 characters.
643+ */
644+ public static String encodeBase64String (byte [] binaryData ) {
645+ return StringUtils .newStringUtf8 (encodeBase64 (binaryData , true ));
646+ }
647+
637648 /**
638649 * Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The
639650 * url-safe variation emits - and _ instead of + and / characters.
640651 *
641652 * @param binaryData
642653 * binary data to encode
643- * @return Base64 characters
654+ * @return byte[] containing Base64 characters in their UTF-8 representation.
644655 * @since 1.4
645656 */
646657 public static byte [] encodeBase64URLSafe (byte [] binaryData ) {
647658 return encodeBase64 (binaryData , false , true );
648659 }
649660
661+ /**
662+ * Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The
663+ * url-safe variation emits - and _ instead of + and / characters.
664+ *
665+ * @param binaryData
666+ * binary data to encode
667+ * @return String containing Base64 characters
668+ * @since 1.4
669+ */
670+ public static String encodeBase64URLSafeString (byte [] binaryData ) {
671+ return StringUtils .newStringUtf8 (encodeBase64 (binaryData , false , true ));
672+ }
673+
650674 /**
651675 * Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks
652676 *
@@ -660,22 +684,35 @@ public static byte[] encodeBase64Chunked(byte[] binaryData) {
660684
661685 /**
662686 * Decodes an Object using the base64 algorithm. This method is provided in order to satisfy the requirements of the
663- * Decoder interface, and will throw a DecoderException if the supplied object is not of type byte[].
687+ * Decoder interface, and will throw a DecoderException if the supplied object is not of type byte[] or String .
664688 *
665689 * @param pObject
666690 * Object to decode
667- * @return An object (of type byte[]) containing the binary data which corresponds to the byte[] supplied.
691+ * @return An object (of type byte[]) containing the binary data which corresponds to the byte[] or String supplied.
668692 * @throws DecoderException
669693 * if the parameter supplied is not of type byte[]
670694 */
671- public Object decode (Object pObject ) throws DecoderException {
695+ public Object decode (Object pObject ) throws DecoderException {
672696 if (pObject instanceof byte []) {
673697 return decode ((byte []) pObject );
698+ } else if (pObject instanceof String ) {
699+ return decode ((String ) pObject );
674700 } else {
675- throw new DecoderException ("Parameter supplied to Base64 decode is not a byte[]" );
701+ throw new DecoderException ("Parameter supplied to Base64 decode is not a byte[] or a String " );
676702 }
677703 }
678704
705+ /**
706+ * Decodes a String containing containing characters in the Base64 alphabet.
707+ *
708+ * @param pArray
709+ * A String containing Base64 character data
710+ * @return a byte array containing binary data
711+ */
712+ public byte [] decode (String pArray ) {
713+ return decode (StringUtils .getBytesUtf8 (pArray ));
714+ }
715+
679716 /**
680717 * Decodes a byte[] containing containing characters in the Base64 alphabet.
681718 *
@@ -684,6 +721,7 @@ public Object decode(Object pObject) throws DecoderException {
684721 * @return a byte array containing binary data
685722 */
686723 public byte [] decode (byte [] pArray ) {
724+ reset ();
687725 if (pArray == null || pArray .length == 0 ) {
688726 return pArray ;
689727 }
@@ -770,6 +808,17 @@ public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean
770808 return b64 .encode (binaryData );
771809 }
772810
811+ /**
812+ * Decodes a Base64 String into octets
813+ *
814+ * @param base64String
815+ * String containing Base64 data
816+ * @return Array containing decoded data.
817+ */
818+ public static byte [] decodeBase64 (String base64String ) {
819+ return new Base64 ().decode (base64String );
820+ }
821+
773822 /**
774823 * Decodes Base64 data into octets
775824 *
@@ -778,8 +827,7 @@ public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean
778827 * @return Array containing decoded data.
779828 */
780829 public static byte [] decodeBase64 (byte [] base64Data ) {
781- Base64 b64 = new Base64 ();
782- return b64 .decode (base64Data );
830+ return new Base64 ().decode (base64Data );
783831 }
784832
785833 /**
@@ -847,6 +895,17 @@ public Object encode(Object pObject) throws EncoderException {
847895 return encode ((byte []) pObject );
848896 }
849897
898+ /**
899+ * Encodes a byte[] containing binary data, into a String containing characters in the Base64 alphabet.
900+ *
901+ * @param pArray
902+ * a byte array containing binary data
903+ * @return A String containing only Base64 character data
904+ */
905+ public String encodeToString (byte [] pArray ) {
906+ return StringUtils .newStringUtf8 (encode (pArray ));
907+ }
908+
850909 /**
851910 * Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 alphabet.
852911 *
@@ -855,6 +914,10 @@ public Object encode(Object pObject) throws EncoderException {
855914 * @return A byte array containing only Base64 character data
856915 */
857916 public byte [] encode (byte [] pArray ) {
917+ reset ();
918+ if (pArray == null || pArray .length == 0 ) {
919+ return pArray ;
920+ }
858921 long len = getEncodeLength (pArray , lineLength , lineSeparator );
859922 byte [] buf = new byte [(int ) len ];
860923 setInitialBuffer (buf , 0 , buf .length );
@@ -964,4 +1027,17 @@ static byte[] toIntegerBytes(BigInteger bigInt) {
9641027 System .arraycopy (bigBytes , startSrc , resizedBytes , startDst , len );
9651028 return resizedBytes ;
9661029 }
1030+
1031+ /**
1032+ * Resets this Base64 object to its initial newly constructed state.
1033+ */
1034+ private void reset () {
1035+ buffer = null ;
1036+ pos = 0 ;
1037+ readPos = 0 ;
1038+ currentLinePos = 0 ;
1039+ modulus = 0 ;
1040+ eof = false ;
1041+ }
1042+
9671043}
0 commit comments