• Post Reply Bookmark Topic Watch Topic
  • New Topic

JDK 1.5_0_5 PrinterWriter.readLine() is splitting data?  RSS feed

 
scotta irwin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would cause readline() to return with split data?

When I write out a short message, it all works fine.

When I write out a boolean, an int, a few longer strings; it splits the message returning after the boolean and int, and then the next readLine returns the strings.

The code is classic stuff. It's all done in 1.5 so I can't try it in 1.4.

Anybody see this one before?

Thanks,

Scott
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused by your question. First of all, there is no class called PrinterWriter. Do you mean PrintWriter instead? If so, your question still does not make sense because PrintWriter does not have a readLine() method. You cannot read with a Writer!

So I must assume you are using a Reader of some sort instead. However, I am unsure what you mean by "split data". What does this mean exactly? Can you post some code to show us what you are trying to do? Also, can you show some sample output and explain how it differs from what you expect? If you can clarify these things, then we will be able to help from there.

Layne
[ October 09, 2005: Message edited by: Layne Lund ]
 
scotta irwin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is really a question about how to write objects over a socket. Turns out my issue is with the way I'm using ObjectOutputStream(ByteArrayOutputStream))

If I use this to write stuff (writeUTF, writeObject, writeBoolean, etc..), then the readLine on the other side chokes on values within the buffer returned. ByteArrayOutputStream.toString(). It works sometimes, just depending on what is written.

I see Datagrams, but I'm not broadcasting.

This method I've been using works great over JMS, just not over a Socket.

Any ideas on how to wrap/format the resulting string from ObjectOutputStream so readLine doesn't see other chars and cut it up?

Thanks,

Scott
[ October 09, 2005: Message edited by: scotta irwin ]
 
scotta irwin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, let me clarify...

I use PrintWriter to issue a println on the client socket out. The value I write is generated using the ObjectInputStream to write values into a byte array.

On the server side, I'm using a BufferedReader to issue a readLine().

Scott
 
scotta irwin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Layne,

one last thing, the "split" is when readLine() returns without the entire buffer I wrote on the client using PrintWriter.println();

If I use ObjectOutputStream to write a boolean, an int, and two strings, into a ByteArrayOutputStream, then println the contents, readLine() returns twice to read that one line.

I get exceptions like EOF in data body and invalid header when I attempt to read the line I get out. Attempted to paste them together (hack) but ObjectInputStream didn't like it.

Scott
 
scotta irwin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/developer/technicalArticles/ALT/sockets/

Looks like I just write the object right to the socket out. No readLine, just read the object right from the socket in.

I'll try this next.....
 
scotta irwin
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That works great. Should have googled that yesterday afternoon.....

Scott
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!