Java Stripping out <CR><LF> over a socket ??

Hello,

I have a data stream coming in over a socket that appears to be losing a <CR><LF> pair on ingestion. Is Java doing something helpful?? Pls see code fragment below ...

I am using the bufferedReader:
        BufferedWriter bufferedWriter = null;
        bufferedWriter = new BufferedWriter(new FileWriter(fileOutput));  
        ...
        Socket clientConnection = new Socket(server, servPort);
        System.out.println("Connected to server...waiting");
        InputStream in = clientConnection.getInputStream();          
        InputStreamReader isr = new InputStreamReader(in);        
        BufferedReader br = new BufferedReader(isr,400);  
         while ((cnt = br.readLine()) != null)
            {             
               System.out.print( cnt );                    
                bufferedWriter.write(cnt);
            }      
Thank  you
           
dennisk1718Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You're using  a BufferedReader. I t tokenizes on lines so you need to put the linefeeds back in again
dennisk1718Author Commented:
I lose track of where they are initially, as my stream is a bit of a jumble, so how can I put them back in??

Serious question...
CEHJCommented:
Instead of

>>bufferedWriter = new BufferedWriter(new FileWriter(fileOutput));  

do


PrintWriter out = new PrintWriter(new FileWriter(fileOutput));
 
...
 
out.println(cnt); // puts correct linefeeds for your platform back in

Open in new window

HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

dennisk1718Author Commented:
Hello,

Since I am using a buffered Reader on socket read and bufferedWriter on write to a file, are you suggesting reworking both?

Where is the stripping occurring?

Thank You

CEHJCommented:
>>Since I am using a buffered Reader on socket read and bufferedWriter on write to a file, are you suggesting reworking both?

You're not paying close enough attention to my comments. The short answer is - no.

>>Where is the stripping occurring?

>>You're using  a BufferedReader. I t tokenizes on lines

It must do, mustn't it - or otherwise why call its most important method 'readLine' ? When it gives you the lines, it's already removed the linefeeds.

What i *am* suggesting is that if you use a PrintWriter, you can use its method println. That puts the linefeeds back in, as my comment says:

>>// puts correct linefeeds for your platform back in

CEHJCommented:
Furthermore, since you now know something more of how a BufferedReader works, you will not be surprised to learn that copying a stream this way is not the most efficient way of doing things. Yes, at the moment, you're echoing lines to the console as well as doing a copy, but if that echoing is only for debugging purposes, you can do the copy more efficiently
dennisk1718Author Commented:
Hello,

Thank You for your response..
Efficiency is paramount in this particular application as my I/O is bursty.
You mentioned " .. more efficiently". In what manner?

Thank you
CEHJCommented:
You can do

IOUtils.copyStream(clientConnection.getInputStream(), new FileOutputStream(fileOutput));

See

http://technojeeves.com/joomla/index.php/free/51-copying-streams

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dennisk1718Author Commented:
Thank You
CEHJCommented:
:-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.