File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes New Line Character Marshalling issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "New Line Character Marshalling issue" Watch "New Line Character Marshalling issue" New topic
Author

New Line Character Marshalling issue

Udhaya Uthama
Greenhorn

Joined: Sep 02, 2009
Posts: 4
Hi,

We have a new line character (0x0D) in our Java objects. While performing marsahhling on Java objects, New line character is discarded.

Please let me know what needs to be done

Regards

Udhaya

rajesh sherla
Greenhorn

Joined: Apr 04, 2010
Posts: 11
Hi Udhaya,

Can you be little more specific to your issue.

Because i created one String object with newline characters "\n" and also with "\r" as given below

String s = new String("string1 \n string2");
String s1 = new String("string1 \r string2");

During deserialization, both the output is proper as given below

string1
string2

How you are inserting new line character in object.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37955
    
  22
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 char[]s 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.
Udhaya Uthama
Greenhorn

Joined: Sep 02, 2009
Posts: 4
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?
rajesh sherla
Greenhorn

Joined: Apr 04, 2010
Posts: 11
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 "...) ???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37955
    
  22
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”.
    Udhaya Uthama
    Greenhorn

    Joined: Sep 02, 2009
    Posts: 4
    Hi,

    So what is the solution for my problem

    Regards
    Udhaya
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37955
        
      22
    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.
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18541
        
        8

    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.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: New Line Character Marshalling issue
     
    Similar Threads
    Why java is unknown
    Extracting integers from a string.
    Can java read character without newline
    Why IE can not open a page, but Firefox can?
    doubt on code