45
45
import static com .oracle .graal .python .runtime .exception .PythonErrorType .UnicodeDecodeError ;
46
46
import static com .oracle .graal .python .runtime .exception .PythonErrorType .UnicodeEncodeError ;
47
47
48
+ import java .nio .BufferUnderflowException ;
48
49
import java .nio .ByteBuffer ;
49
50
import java .nio .CharBuffer ;
50
51
import java .nio .charset .CharacterCodingException ;
@@ -586,16 +587,18 @@ String decodeBytes(ByteBuffer bytes, String errors) {
586
587
CodingErrorAction errorAction = convertCodingErrorAction (errors );
587
588
try {
588
589
ByteBuffer buf = ByteBuffer .allocate (bytes .remaining () * Integer .BYTES );
590
+ byte [] hexString = new byte [8 ];
589
591
while (bytes .hasRemaining ()) {
590
592
int val ;
591
593
byte b = bytes .get ();
592
594
if (b == (byte ) '\\' ) {
593
595
byte b1 = bytes .get ();
594
596
if (b1 == (byte ) 'u' ) {
595
- // read 2 bytes as integer
596
- val = bytes . getShort ( );
597
+ bytes . get ( hexString , 0 , 4 );
598
+ val = Integer . parseInt ( new String ( hexString , 0 , 4 ), 16 );
597
599
} else if (b1 == (byte ) 'U' ) {
598
- val = bytes .getInt ();
600
+ bytes .get (hexString , 0 , 8 );
601
+ val = Integer .parseInt (new String (hexString , 0 , 8 ), 16 );
599
602
} else {
600
603
throw new CharacterCodingException ();
601
604
}
@@ -609,7 +612,7 @@ String decodeBytes(ByteBuffer bytes, String errors) {
609
612
buf .flip ();
610
613
CharBuffer decoded = UTF32 .newDecoder ().onMalformedInput (errorAction ).onUnmappableCharacter (errorAction ).decode (buf );
611
614
return String .valueOf (decoded );
612
- } catch (CharacterCodingException e ) {
615
+ } catch (CharacterCodingException | NumberFormatException | BufferUnderflowException e ) {
613
616
throw raise (UnicodeDecodeError , e );
614
617
}
615
618
}
0 commit comments