This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Udhaya Uthama wrote:We have a new line character (0x0D) in our Java objects.
No you haven’t. 0x0d is carriage return.
Rajesh sherla, it might be worth converting your deserialised Strings to chars with the toCharArray() method, and printing each char with System.out.printf("0x%02x ", (int)c); I don’t know whether the (int) cast is actually necessary. Then you will have definite confirmation the characters are coming back correctly when you get 0x0a for \n and 0x0d for \r. If you print your second String on a *nix console, you would only read “string2” because \r moves the cursor to the start of the same line. I agree with you, we need more explanation of what the problem is.
I also think this question is too difficult for “beginning”, so I shall move it.
Joined: Sep 02, 2009
Thanks for your response. Following is the real time scenario:
Client will submit a file from Linux machine (Through browser) and that will stored in the server's file system. While client wants to retrieve the file, we read the content from the file system and placing that string in the JAXB objects. We have printed the content read from the file system and we could able to see the 0x0D character. (Event it is displaying properly in the out file). We are marshalling the java object before sending to client . After marshalling 0x0D character is discarded. Is there something we need to take care?
Joined: Apr 04, 2010
Hi Campbell Ritchie,
You are correct. Thanks for this.
I tested my program in *nix console, it moves cursor to the start of the same line.
But the same progarm in *dow console, shows proper output.
But just in general question, you said "System.out.printf("0x%02x ", (int)c);
Then you will have definite confirmation the characters are coming back correctly when you get 0x0a for \n and 0x0d for \r"
I checked on *nix console, it is showing me "0x0d" during serialization/deserialization process.
So it means everything is fine..
And for confirmation every time we have to use System.out.printf("0x%02x "...) ???
Joined: Oct 13, 2005
That is one way of confirming the presence of a particular char in a String. It was simply the first thing I thought of. I am sure you can think of other ways to do it, eg contains("\n"); or contains("\r"); On a *nix console, moving to the left end of the line and overwriting is the correct behaviour.
It is worthwhile checking the different kind of line ends.
When I first programmed, I was using a Teletype. I had to push the CR = Campbell Ritchie key at the end of every line.
Then I had to push the LF = line-feed = \n key to move the paper up.
Older Macs used \r as a line-end sequence.
DOS and Windows® still use CR-LF (\r\n) at the end of a line.
Unix-like systems always use LF alone (\n)
.That means, if you are working on a Linux system, there will be no \r characters in a normal text file. You are going to have to do something to allow for that difference if you transfer the file to a Windows® system which uses \r\n.
I have realised: I have spent 40 years thinking the key marked CR meant “Campbell Ritchie”, when it really means “Carriage Return”.
Joined: Sep 02, 2009
So what is the solution for my problem
Joined: Oct 13, 2005
I don’t know what the solution is. You need to work that out, then please tell us. Decide why you want \r characters, and what you are doing with them.
You say you are "marshalling" Java objects. That suggests to me that there might be some XML involved. Is that correct?
And then you say that carriage-return characters are disappearing. This suggests to me that attribute value normalization (you could look that up) might be taking place. In other words if you use a carriage-return character in an XML attribute, you should expect it to be replaced by a new-line character.