@@ -529,7 +529,7 @@ public static boolean matchesContentType(String contentType, String matchesConte
529
529
}
530
530
531
531
public static String sanitizeVarName (String name ){
532
- return name .replaceAll ("_" ,"" ).toLowerCase ();
532
+ return name .replaceAll ("_" , "" ).toLowerCase ();
533
533
}
534
534
535
535
public static ResponseStatus createResponseStatus (Object obj ) {
@@ -671,4 +671,115 @@ public static <K,V> HashMap<K,ArrayList<V>> createMap(ArrayList<V> xs, Function<
671
671
672
672
return to ;
673
673
}
674
+
675
+ //From: http://iharder.sourceforge.net/current/java/base64/ (Public Domain)
676
+ public static String toBase64String (String source ) {
677
+ return toBase64String (toUtf8Bytes (source ));
678
+ }
679
+
680
+ public static String toBase64String (byte [] source ) {
681
+ byte [] encoded = toBase64Bytes (source );
682
+ try {
683
+ return new String (encoded , "US-ASCII" );
684
+ } catch (UnsupportedEncodingException e ) {
685
+ return new String (encoded );
686
+ }
687
+ }
688
+
689
+ public static byte [] toBase64Bytes (byte [] source ) {
690
+ return toBase64Bytes (source , 0 , source .length );
691
+ }
692
+
693
+ public static byte [] toBase64Bytes (byte [] source , int off , int len ) {
694
+ if (source == null )
695
+ throw new NullPointerException ("Cannot serialize a null array." );
696
+
697
+ if (off < 0 )
698
+ throw new IllegalArgumentException ("Cannot have negative offset: " + off );
699
+
700
+ if (len < 0 )
701
+ throw new IllegalArgumentException ("Cannot have length offset: " + len );
702
+
703
+ if (off + len > source .length )
704
+ throw new IllegalArgumentException (String .format (
705
+ "Cannot have offset of %d and length of %d with array of length %d" , off , len , source .length ));
706
+
707
+ int encLen = (len / 3 ) * 4 + (len % 3 > 0 ? 4 : 0 ); // Bytes needed for actual encoding
708
+ byte [] outBuff = new byte [encLen ];
709
+
710
+ int d = 0 ;
711
+ int e = 0 ;
712
+ int len2 = len - 2 ;
713
+ for (; d < len2 ; d += 3 , e += 4 ) {
714
+ encode3to4 (source , d + off , 3 , outBuff , e );
715
+ }
716
+
717
+ if (d < len ) {
718
+ encode3to4 (source , d + off , len - d , outBuff , e );
719
+ e += 4 ;
720
+ }
721
+
722
+ // Only resize array if we didn't guess it right.
723
+ if (e <= outBuff .length - 1 ) {
724
+ byte [] finalOut = new byte [e ];
725
+ System .arraycopy (outBuff , 0 , finalOut , 0 , e );
726
+ return finalOut ;
727
+ } else {
728
+ return outBuff ;
729
+ }
730
+ }
731
+
732
+ private final static byte EQUALS_SIGN = (byte )'=' ;
733
+
734
+ private final static byte [] _STANDARD_ALPHABET = {
735
+ (byte )'A' , (byte )'B' , (byte )'C' , (byte )'D' , (byte )'E' , (byte )'F' , (byte )'G' ,
736
+ (byte )'H' , (byte )'I' , (byte )'J' , (byte )'K' , (byte )'L' , (byte )'M' , (byte )'N' ,
737
+ (byte )'O' , (byte )'P' , (byte )'Q' , (byte )'R' , (byte )'S' , (byte )'T' , (byte )'U' ,
738
+ (byte )'V' , (byte )'W' , (byte )'X' , (byte )'Y' , (byte )'Z' ,
739
+ (byte )'a' , (byte )'b' , (byte )'c' , (byte )'d' , (byte )'e' , (byte )'f' , (byte )'g' ,
740
+ (byte )'h' , (byte )'i' , (byte )'j' , (byte )'k' , (byte )'l' , (byte )'m' , (byte )'n' ,
741
+ (byte )'o' , (byte )'p' , (byte )'q' , (byte )'r' , (byte )'s' , (byte )'t' , (byte )'u' ,
742
+ (byte )'v' , (byte )'w' , (byte )'x' , (byte )'y' , (byte )'z' ,
743
+ (byte )'0' , (byte )'1' , (byte )'2' , (byte )'3' , (byte )'4' , (byte )'5' ,
744
+ (byte )'6' , (byte )'7' , (byte )'8' , (byte )'9' , (byte )'+' , (byte )'/'
745
+ };
746
+
747
+ private static byte [] encode3to4 (
748
+ byte [] source , int srcOffset , int numSigBytes ,
749
+ byte [] destination , int destOffset ) {
750
+
751
+ byte [] ALPHABET = _STANDARD_ALPHABET ;
752
+
753
+ int inBuff = ( numSigBytes > 0 ? ((source [ srcOffset ] << 24 ) >>> 8 ) : 0 )
754
+ | ( numSigBytes > 1 ? ((source [ srcOffset + 1 ] << 24 ) >>> 16 ) : 0 )
755
+ | ( numSigBytes > 2 ? ((source [ srcOffset + 2 ] << 24 ) >>> 24 ) : 0 );
756
+
757
+ switch (numSigBytes )
758
+ {
759
+ case 3 :
760
+ destination [ destOffset ] = ALPHABET [ (inBuff >>> 18 ) ];
761
+ destination [ destOffset + 1 ] = ALPHABET [ (inBuff >>> 12 ) & 0x3f ];
762
+ destination [ destOffset + 2 ] = ALPHABET [ (inBuff >>> 6 ) & 0x3f ];
763
+ destination [ destOffset + 3 ] = ALPHABET [ (inBuff ) & 0x3f ];
764
+ return destination ;
765
+
766
+ case 2 :
767
+ destination [ destOffset ] = ALPHABET [ (inBuff >>> 18 ) ];
768
+ destination [ destOffset + 1 ] = ALPHABET [ (inBuff >>> 12 ) & 0x3f ];
769
+ destination [ destOffset + 2 ] = ALPHABET [ (inBuff >>> 6 ) & 0x3f ];
770
+ destination [ destOffset + 3 ] = EQUALS_SIGN ;
771
+ return destination ;
772
+
773
+ case 1 :
774
+ destination [ destOffset ] = ALPHABET [ (inBuff >>> 18 ) ];
775
+ destination [ destOffset + 1 ] = ALPHABET [ (inBuff >>> 12 ) & 0x3f ];
776
+ destination [ destOffset + 2 ] = EQUALS_SIGN ;
777
+ destination [ destOffset + 3 ] = EQUALS_SIGN ;
778
+ return destination ;
779
+
780
+ default :
781
+ return destination ;
782
+ }
783
+ }
784
+
674
785
}
0 commit comments