2

I got a problem of how to handle byte data without corrupting them. Here is my code

...
byte[] b = new byte[1000];

// read input stream
BufferedInputStream inData = new BufferedInputStream(socket.getInputStream());
int length = inData.read(b);

String data = new String(b, 0, length);
if (Log4j.log.isEnabledFor(Level.INFO)) {
    Log4j.log.info("Data Length: " + length
            + ", Received data:  " + data);
}

...
// start a new socket to other server
...
BufferedOutputStream out = new BufferedOutputStream(remote.getOutputStream());

out.write(data.getBytes());
out.flush();
...

It seem like nothing problem here. But if I got a hex string like

F8 F0 F0 C2 20 00 00 80 00 00

few data like C2 will be turned into 3F. I could see this in my log & remote server's log too.

At first, I suspect it will be the overflow. But since those data will be treat as Hex String and send to another server, so this suspicion will be crossed.

I got not clue about what is going on about this, so I could really use some help if anyone knows about this issue.

Kaninchen
  • 431
  • 2
  • 5
  • 16

1 Answers1

3

Right now you are converting the bytes into a String with the platform default charset, and then calling getBytes() back later. If the bytes do not represent a valid string in that charset, data will be lost, e.g. the invalid bytes will be replaced with the character '?'.

Stop that. If you have bytes, pass them around as a byte[]. Do not at any point convert them into a String.

Louis Wasserman
  • 172,699
  • 23
  • 307
  • 375