--- src/main/java/net/iharder/Base64.java 2010-03-29 20:54:45.000000000 +0200 +++ src/main/java/net/iharder/Base64.java.elasticsearch 2015-02-18 23:43:01.174030315 +0100 @@ -1,5 +1,8 @@ package net.iharder; +import java.nio.charset.Charset; +import java.util.Locale; + /** *

Encodes and decodes to and from Base64 notation.

*

Homepage: http://iharder.net/base64.

@@ -208,7 +211,7 @@ /** Preferred encoding. */ - private final static String PREFERRED_ENCODING = "US-ASCII"; + public final static Charset PREFERRED_ENCODING = Charset.forName("US-ASCII"); private final static byte WHITE_SPACE_ENC = -5; // Indicates white space in encoding @@ -690,13 +693,7 @@ } // end finally // Return value according to relevant encoding. - try { - return new String( baos.toByteArray(), PREFERRED_ENCODING ); - } // end try - catch (java.io.UnsupportedEncodingException uue){ - // Fall back to some Java default - return new String( baos.toByteArray() ); - } // end catch + return new String(baos.toByteArray(), PREFERRED_ENCODING); } // end encode @@ -831,12 +828,7 @@ byte[] encoded = encodeBytesToBytes( source, off, len, options ); // Return value according to relevant encoding. - try { - return new String( encoded, PREFERRED_ENCODING ); - } // end try - catch (java.io.UnsupportedEncodingException uue) { - return new String( encoded ); - } // end catch + return new String(encoded, PREFERRED_ENCODING); } // end encodeBytes @@ -899,7 +891,7 @@ if( off + len > source.length ){ throw new IllegalArgumentException( - String.format( "Cannot have offset of %d and length of %d with array of length %d", off,len,source.length)); + String.format(Locale.ROOT, "Cannot have offset of %d and length of %d with array of length %d", off, len, source.length)); } // end if: off < 0 @@ -1039,12 +1031,12 @@ throw new NullPointerException( "Destination array was null." ); } // end if if( srcOffset < 0 || srcOffset + 3 >= source.length ){ - throw new IllegalArgumentException( String.format( - "Source array with length %d cannot have offset of %d and still process four bytes.", source.length, srcOffset ) ); + throw new IllegalArgumentException(String.format(Locale.ROOT, + "Source array with length %d cannot have offset of %d and still process four bytes.", source.length, srcOffset)); } // end if if( destOffset < 0 || destOffset +2 >= destination.length ){ - throw new IllegalArgumentException( String.format( - "Destination array with length %d cannot have offset of %d and still store three bytes.", destination.length, destOffset ) ); + throw new IllegalArgumentException(String.format(Locale.ROOT, + "Destination array with length %d cannot have offset of %d and still store three bytes.", destination.length, destOffset)); } // end if @@ -1153,8 +1145,8 @@ throw new NullPointerException( "Cannot decode null source array." ); } // end if if( off < 0 || off + len > source.length ){ - throw new IllegalArgumentException( String.format( - "Source array with length %d cannot have offset of %d and process %d bytes.", source.length, off, len ) ); + throw new IllegalArgumentException(String.format(Locale.ROOT, + "Source array with length %d cannot have offset of %d and process %d bytes.", source.length, off, len)); } // end if if( len == 0 ){ @@ -1191,15 +1183,26 @@ // If that was the equals sign, break out of 'for' loop if( source[i] == EQUALS_SIGN ) { + // check if the equals sign is somewhere in between + if (i + 1 < len + off) { + throw new java.io.IOException(String.format(Locale.ROOT, + "Found equals sign at position %d of the base64 string, not at the end", i)); + } break; } // end if: equals sign } // end if: quartet built + else { + if (source[i] == EQUALS_SIGN && len + off > i && source[i + 1] != EQUALS_SIGN) { + throw new java.io.IOException(String.format(Locale.ROOT, + "Found equals sign at position %d of the base64 string, not at the end", i)); + } // enf if: equals sign and next character not as well + } // end else: } // end if: equals sign or better } // end if: white space, equals sign or better else { // There's a bad input character in the Base64 stream. - throw new java.io.IOException( String.format( - "Bad Base64 input character decimal %d in array position %d", ((int)source[i])&0xFF, i ) ); + throw new java.io.IOException(String.format(Locale.ROOT, + "Bad Base64 input character decimal %d in array position %d", ((int) source[i]) & 0xFF, i)); } // end else: } // each input character @@ -1243,13 +1246,7 @@ throw new NullPointerException( "Input string was null." ); } // end if - byte[] bytes; - try { - bytes = s.getBytes( PREFERRED_ENCODING ); - } // end try - catch( java.io.UnsupportedEncodingException uee ) { - bytes = s.getBytes(); - } // end catch + byte[] bytes = s.getBytes(PREFERRED_ENCODING); // // Decode @@ -1282,7 +1279,7 @@ } // end try catch( java.io.IOException e ) { - e.printStackTrace(); + // e.printStackTrace(); // Just return originally-decoded bytes } // end catch finally { @@ -1359,7 +1356,7 @@ @Override public Class resolveClass(java.io.ObjectStreamClass streamClass) throws java.io.IOException, ClassNotFoundException { - Class c = Class.forName(streamClass.getName(), false, loader); + Class c = Class.forName(streamClass.getName(), false, loader); if( c == null ){ return super.resolveClass(streamClass); } else {