Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cross-Platform Networking

James Gray
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I just finished Java I/O (the O'Reilly book by Elliotte Rusty Harold) and I have some questions. I need to do some cross-platform networking with Strings and he the book left me with a few outstanding questions.
First, let's talk about the readline() bug and println()'s flaws. The author makes a pretty big deal of these methods not being usable for networking, but the solution seems very obvious to me. readlines()'s problem seems seems solely tied to cariage returns, '\r'. println() suffers similar disadvantages because it's always using platform specific line ends. Knowing this, couldn't we just use the print() method instead, tack on a UNIX style line feed ('\n') and call flush afterwords? I guess this doesn't use println() after all, but is does seem to avoid all the problems, unless I'm missing something. It should be safe to call readline() this way, since it will never even see a '\r', and it won't even return the '\n' character so it shouldn't cause problems on any platform, right? I'm just trying to make sure I understand what's going on here.
Ironically, that's not the strategy I'm leaning towards. My plan is this:
DataOutputStream -> BufferedOutputStream -> OutputStream
DataInputStream <- BufferedInputStream <- InputStream
I do have a few questions though. I plan to use writeUTF() in DataInputStream and pull it back out with readUTF() at the other end. Now when readUTF() see's that initial short declaring x number of bytes and tries to read x bytes, it will just block if they haven't all been delivered over the network yet, right? Put another way, it's only going to throw an exception if it reaches a premature end of stream before reading x bytes, correct?
My last question is about the buffer in the BufferedInputStream. I plan to use a small value (128 or 256 probably), but I will be sending variable length Strings. (I'll flush() after each String.) I want to make sure I understand how that works. It my string is longer than the buffer, it will just be sent in seperate chunks, right? That shouldn't adversly affect my Strings or impose any kind of a length limit on me, if I'm understanding all of this correctly.
Well, I know that's a lot to swallow. Thanks for taking the time to read this. I'm just trying to make sure I'm getting a hold of the ins and outs of Java's I/O system.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic