views:

34

answers:

1

Hello everyone! For some reason, this code is changing any '\n' characters from the input and replacing it with '\n\r' in the new outputed file. I reference a couple websites, and still haven't figured it out.. Anyone have an idea? Thanks a lot!

Socket connectionSocket = sData.accept();
InputStream inputStream = connectionSocket.getInputStream();
BufferedInputStream inputBufferedStream = new BufferedInputStream(inputStream);
FileOutputStream outputStream = new FileOutputStream("/home/greg/1");


     byte[] buffer = new byte[1024];
     long count = 0;
     int n = 0;
     while ((n = inputBufferedStream.read(buffer))>=0) {
         outputStream.write(buffer, 0, n);
         count += n;
     }
    outputStream.close();
  }
+1  A: 

The particular code isn't doing that. Likely those \r\n were simply already in the input source.

It can only happen when you're reading it using for example BufferedReader#readLine() which eats the newlines and writing it using PrintWriter#println() which appends the platform default newlines. Probably the other side is doing that? After all, a Reader/Writer shouldn't be used for binary data. It may malform it. Use InputStream/OutputStream for it.

BalusC
Thank you so much! The sending data is changing the \n to \r\n
Greg
You're welcome. Don't forget to mark the answer accepted. See also http://stackoverflow.com/faq.
BalusC