Java 8 change in UTF-8 decoding

前端 未结 1 1420
深忆病人
深忆病人 2020-12-10 16:28

We recently migrated our application to JDK 8 from JDK 7. After the change, we ran into a problem with the following snippet of code.

String output = new Str         


        
相关标签:
1条回答
  • 2020-12-10 16:51

    From the pointers provided by @Holger, It was clear that we had to write a custom CharsetDecoder.

    I copied over OpenJDK's version of sun.nio.cs.UTF_8 class, renamed it to CustomUTF_8 and used it to construct a string like so

    String output = new String(bytes, new CustomUTF_8());
    

    I plan to run extensive tests cross verifying the outputs generated on Java 7 and Java 8. This is an interim solution while I am trying to fix the actual problem of passing output from hmac directly to String without Base64 encoding it first to.

     String output = new String(Base64.Encoder.encode(bytes), Charset.forname("UTF-8"));
    
    0 讨论(0)
提交回复
热议问题