0

I'm in front of a curious problem. Some code is better than long story:

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
buffer.write(...); // I write byte[] data
// In debugger I can see that buffer's count = 449597
String szData = buffer.toString();
int iSizeData = buffer.size();
// But here, szData's count = 240368
// & iSizeData = 449597

So my question is: why szData doesn't contain all the buffer's data? (only one Thread run this code) because after that kind of operation, I don't want szData.charAt(iSizeData - 1) crashes!

EDIT: szData.getBytes().length = 450566. There is encoding problems I think. Better use a byte[] instead of a String finally?

N0un
  • 868
  • 8
  • 31
  • Multibyte characters perhaps? – Shark Jul 27 '16 at 08:20
  • 1
    @Shark Not perhaps, definitely: http://stackoverflow.com/questions/16270994/difference-between-string-length-and-string-getbytes-length – Jozef Chocholacek Jul 27 '16 at 08:23
  • @JozefChocholacek oh i'm certain it's *definitely* but without the OP posting the bytes/characters, I can only guess. – Shark Jul 27 '16 at 08:24
  • @JozefChocholacek So there is a solution to get the entire String value or I should use a byte[] buffer? – N0un Jul 27 '16 at 08:25
  • @N0un perhaps you should be doing `iSizeData = buffer.size().length()` instead? that will avoid `szData.charAt(iSizeData-1)` crashes. – Shark Jul 27 '16 at 08:34
  • @N0un Depends what you want to do. In Java, `char` != `byte`, depending on the default character coding of the platform, `char` can occupy up to 4 bytes in memory. You work either with bytes (binary data), or with characters (strings), you cannot (easily) switch between them. – Jozef Chocholacek Jul 27 '16 at 08:44
  • @JozefChocholacek This is the problem, I have byte[] and I want make some operations like (C-strncasecmp), so I convert it in String to do that. If I convert byte[] in String at the beginning and only deal with String after that (using StringBuilder to append,...), it's a good solution ? – N0un Jul 27 '16 at 08:48
  • @N0un Yes, convert the buffer content to String and work with String. Btw. `strncasecmp` seems to me the same operation as [String.compareToIgnoreCase(String str)](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#compareToIgnoreCase-java.lang.String-) in Java. – Jozef Chocholacek Jul 27 '16 at 08:53
  • @JozefChocholacek Tks ! You can post an answer explaining what was my problem if you want! – N0un Jul 27 '16 at 09:30

1 Answers1

1

In Java, charbyte, depending on the default character coding of the platform, char can occupy up to 4 bytes in memory. You work either with bytes (binary data), or with characters (strings), you cannot (easily) switch between them.

For String operations like strncasecmp in C, use the methods of the String class, e.g. String.compareToIgnoreCase(String str). Also have a look at the StringUtils class from the Apache Commons Lang library.

Jozef Chocholacek
  • 2,874
  • 2
  • 20
  • 25