简体   繁体   中英

Java StringBuilder / String is '<Unreadable>'

I have a byte array bytes of UTF-8 encoded strings which I want to convert to a String. bytes.length is about 130000

String str = new String(bytes, StandardCharsets.UTF_8); should do the job. However str gets the value '<Unreadable>'

Converting bytes line by line and printing it out works nicely. However appending the lines in a StringBuilder fails as well. Again the content of the StringBuilder r will be '<Unreadable>'. So I thought there might be an unreadable byte in the array. But r.substring(60000, r.count) works well, and r.substring(1,60000) , too. Is there any problem with the size of the byte array?? Maximum size of String/StringBuilder is 2^32 - 1 so there should be no problem.

      ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
      InputStreamReader reader = new InputStreamReader(bais);
      BufferedReader in = new BufferedReader(reader);
      // String readBuf = in.lines().collect(Collectors.joining()); gives '<Unreadable>'           
      String readed;
      StringBuilder r = new StringBuilder();
      while ((readed = in. readLine()) != null) {
          System.out.println(readed); // works fine
          r=r.append(readed);
      }

After the loop r.toString() is '<Unreadable>' Any ideas why I cannot convert the byte array to a String/StringBuilder?

I had exactly the same. The String was "<Unreadable>" if it was made from file bigger then 50000 bytes. But String.lenght() showed 50000+ bytes! I found that the text "<Unreadable"> returned the IDE (Netbeans 11) in debug tools. So, the String was OK, but Netbeans didn't show the right content. It showed "<Unreadable>" instead.

I tried to reproduce this behavior, and I have not been able to. We need a proper minimal reproducible example to make any real progress on this. And full details of the Java version and vendor, and any other tools that may be implicated.

However I do have one definite thing to report. I have copies of the OpenJDK source code for Java 6, 7, 8, 11 and 17 in a searchable form. When I search the source code for Unreadable , NONE of the hits I get are relevant. (Indeed, they are all in the respective test trees!) This is very odd.

My tentative conclusion is that this <Unreadable> string you are seeing is NOT coming from OpenJDK / Oracle Java. Either you are using a different vendor's Java, or it is coming from a tool such as your IDE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM