Is it possible to create an empty java string from a non-empty UTF-8 byte array?

I'm trying to debug something, and I wonder if the following code can return true

public boolean impossible(byte[] myBytes) {
  if (myBytes.length == 0)
    return false;
  String string = new String(myBytes, "UTF-8");
  return string.length() == 0;
}

Is there any value that I can pass that will return true? I staggered by skipping only the first byte of a sequence of 2 bytes, but it still creates a single character string.

To explain this, it happened on a PowerPC chip in Java 1.4 compiled via GCJ into its own executable file. This basically means that most bets are disabled. I basically wonder if the behavior of Java was “normal” or the Java specification of any promises.

+3
source share
4 answers

javadoc java.util.String String (byte [], "UTF-8" ) , bytearray . , http://java.sun.com/j2se/1.5.0/docs/api/java/nio/charset/CharsetDecoder.html.

+5

.

API Java 5 API " , , ".

, : java JVM (Sun, HP, IBM, ..)

"", .

: Trey CharsetDecoder

+1

Java BOM mark ( , ), BOM (U + FEFF, UTF-8, EF BB BF) .


Update:

1-3 . Java 1.6. , :

public static void main(String[] args) throws UnsupportedEncodingException {
    byte[] test = new byte[3];
    byte[] end = new byte[test.length];

    if (impossible(test)) {
        System.out.println(Arrays.toString(test));
    }
    do {
        increment(test, 0);
        if (impossible(test)) {
            System.out.println(Arrays.toString(test));
        }
    } while (!Arrays.equals(test, end));

}

private static void increment(byte[] arr, int i) {
    arr[i]++;
    if (arr[i] == 0 && i + 1 < arr.length) {
        increment(arr, i + 1);
    }
}

public static boolean impossible(byte[] myBytes) throws UnsupportedEncodingException {
    if (myBytes.length == 0) {
        return false;
    }
    String string = new String(myBytes, "UTF-8");
    return string.length() == 0;
}
+1

UTF-8 , "" . [] String, .

If you want to reproduce this, write unit test, which iterates over each possible byte value, passing an array of this value to one value and asserting that the string is not empty.

0
source

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


All Articles