@@ -296,27 +296,31 @@ private String encode( String decoded, Set<Integer> exceptions ) {
296
296
String character = new String ( Character .toChars ( codePoint ) );
297
297
CharArrayWriter hexSequence = new CharArrayWriter ();
298
298
byte [] bytes = character .getBytes ( UTF_8 );
299
- for ( int bytesIndex = 0 ; bytesIndex < bytes . length ; bytesIndex ++ ) {
300
- char left = Character .forDigit ( bytes [ bytesIndex ] >> 4 & 0xF , 16 );
301
- char right = Character .forDigit ( bytes [ bytesIndex ] & 0xF , 16 );
299
+ for (byte aByte : bytes ) {
300
+ char left = Character .forDigit (aByte >> 4 & 0xF , 16 );
301
+ char right = Character .forDigit (aByte & 0xF , 16 );
302
302
hexSequence
303
- .append ( '%' )
304
- .append ( left )
305
- .append ( right );
303
+ .append ('%' )
304
+ .append (left )
305
+ .append (right );
306
306
}
307
- String target = character .toString ();
308
307
String sequence = hexSequence .toString ().toUpperCase ();
309
- encoded = encoded .replace ( target , sequence );
308
+ encoded = encoded .replace (character , sequence );
310
309
} catch ( UnsupportedEncodingException e ) {
311
310
e .printStackTrace ();
312
311
}
313
312
}
314
313
return encoded ;
315
314
}
316
315
317
- private String decode ( String encoded ) {
316
+ private String decode (String encoded ) {
317
+ // Decode characters with 3 bytes first, then with 1 byte to fix https://github.com/js-cookie/java-cookie/issues/14
318
+ return decode ( decode ( encoded , 3 ), 1 );
319
+ }
320
+
321
+ private String decode ( String encoded , Integer bytesPerCharacter ) {
318
322
String decoded = encoded ;
319
- Pattern pattern = Pattern .compile ( "(%[0-9A-Z]{2})+ " );
323
+ Pattern pattern = Pattern .compile ( "(%[0-9A-Z]{2}){" + bytesPerCharacter + "} " );
320
324
Matcher matcher = pattern .matcher ( encoded );
321
325
while ( matcher .find () ) {
322
326
String encodedChar = matcher .group ();
@@ -392,14 +396,13 @@ private String decodeValue( String encodedValue, String decodedName ) {
392
396
private Map <String , String > getCookies ( String cookieHeader ) {
393
397
Map <String , String > result = new HashMap <String , String >();
394
398
String [] cookies = cookieHeader .split ( "; " );
395
- for ( int i = 0 ; i < cookies .length ; i ++ ) {
396
- String cookie = cookies [ i ];
397
- String encodedName = cookie .split ( "=" )[ 0 ];
398
- String decodedName = decode ( encodedName );
399
-
400
- String encodedValue = cookie .substring ( cookie .indexOf ( '=' ) + 1 , cookie .length () );
401
- String decodedValue = decodeValue ( encodedValue , decodedName );
402
- result .put ( decodedName , decodedValue );
399
+ for (String cookie : cookies ) {
400
+ String encodedName = cookie .split ("=" )[0 ];
401
+ String decodedName = decode (encodedName );
402
+
403
+ String encodedValue = cookie .substring (cookie .indexOf ('=' ) + 1 );
404
+ String decodedValue = decodeValue (encodedValue , decodedName );
405
+ result .put (decodedName , decodedValue );
403
406
}
404
407
return result ;
405
408
}
0 commit comments