String (bytes [] Charset) returns results differently in Java7 and java 8

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class Java87String {

public static void main(String[] args) throws UnsupportedEncodingException {
        // TODO Auto-generated method stub

        //byte[] b = {-101, 53, -51, -26, 24, 60, 20, -31, -6, 45, 50, 103, -66, 28, 114, -39, 92, 23, -47, 32, -5, -122, -28, 79, 22, -76, 116, -122, -54, -122};
        //byte[] b = {-76, -55, 85, -50, 80, -23, 27, 62, -94, -74, 47, -123, -119, 94, 90, 61, -63, 73, 56, -48, -54, -4, 11, 79};

        byte[] b = { -5, -122, -28};

        System.out.println("Input Array :" + Arrays.toString(b));
        System.out.println("Array Length : " + b.length);                       
        String target = new String(b,StandardCharsets.UTF_8);
        System.out.println(Arrays.toString(target.getBytes("UTF-8")));
        System.out.println("Final Key :" + target);

}
}

The above code returns the following result in Java 7

Input Array :[-5, -122, -28]
Array Length : 3
[-17, -65, -67]
Final Key : 

The same code returns the following output in Java 8

Input Array :[-5, -122, -28]
Array Length : 3
[-17, -65, -67, -17, -65, -67, -17, -65, -67]
Final Key :   

It seems that Java8 is making the correct replacement using the default sequence [-17, -65, -67].

Why is there a difference in conclusions and Any Known errors in JDK 1.7 that fixes this problem?

+4
source share
2 answers

In String JavaDoc :

The behavior of this constructor when these bytes are not allowed in this encoding is not specified. The CharsetDecoder class should be used when more control over the decoding process is required.

+1

, (-5, -122, -28) UTF-8, JVM . , , Java .

?

+1

Source: https://habr.com/ru/post/1656195/


All Articles