Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

string decoding backslash literally in byte[] from datagram packet

807580Jul 22 2010 — edited Jul 25 2010
In my application a client sends a string in a UDP packet. The string can contain backslashes (\) as filename separators on windows systems.

I want to turn the byte[] into a string again but want to prevent interpretation of the backslash as an escape character. The string will only contain ascii printable characters. How can I manage this?
My code reads as follows:
       @Override
        public void run (){
            while ( true ){
                try{
                    packet = new DatagramPacket(new byte[ MAX_COMMAND_LENGTH_BYTES ],MAX_COMMAND_LENGTH_BYTES);
                    datagramSocket.receive(packet);
                    InputStream is = new ByteArrayInputStream(packet.getData(),0,packet.getLength());
                    byte[] b=new byte[packet.getLength()];
                    is.read(b);
                    String line=new String(b); // turns \n into newline, don't want this!

   // other tries below
//                    char[] c=new char[b.length];
//                    for(int i=0;i<b.length;i++){
//                        c=(char)b[i];
// }

// String line=new String(c);


// StringBuilder sb=new StringBuilder();
// for(int i=0;i<b.length;i++){
// sb.append((char)b[i]);
// }
// String line = sb.toString();

log.info("received line \""+line+"\""); // debug
parseAndDispatchCommand(line);

} catch ( SocketException e ){
log.info("closed " + this);
break;
} catch ( IOException ex ){
log.warning(ex.toString());
break;
}

}
}
The client sends a string that reads as follows:
startlogging C:\Users\tobi\Documents\misc\telluride\2010\Recording\test9June30\nine of hearts.dat
It is interpreted on the java side as shown by the logging output:
INFO: received line "startlogging C:\Users\tobi\Documents\misc\telluride\2010\Recording\test9June30
ine of hearts.dat
"
You can see that '\n' in the byte[] is decoded as a newline.  I guess other combinations like \r would have the same problem.

Is there any way to prevent this on the client side?   I've tried various charsets with no success.
Thanks for your help.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 22 2010
Added on Jul 22 2010
12 comments
1,102 views