Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 618
  • Last Modified:

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
           
0
dennisk1718
Asked:
dennisk1718
  • 6
  • 4
1 Solution
 
CEHJCommented:
You're using  a BufferedReader. I t tokenizes on lines so you need to put the linefeeds back in again
0
 
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...
0
 
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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

0
 
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

0
 
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
0
 
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
0
 
CEHJCommented:
You can do

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

See

http://technojeeves.com/joomla/index.php/free/51-copying-streams
0
 
dennisk1718Author Commented:
Thank You
0
 
CEHJCommented:
:-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now