wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes DatinputStream : length issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "DatinputStream : length issue" Watch "DatinputStream : length issue" New topic
Author

DatinputStream : length issue

Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
I have this webApp which uses a Tuxedo server to obtain data from a Mainframe. Everything works correct but when I have a stream of more than 1460 bytes the characters after byte 1460 are replaced by a square 0 !?! Does anybody have a clue ?
Here is my code :
Thanks for any input !
Kristof
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Check the API for read(byte[], int, int) - there's no guarantee it will actualy read all the bytes you request. Annoying but true. Try the readFully() methods instead.


"I'm not back." - Bill Harding, Twister
Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
Very strange.... When I post my message the first time I get the problem I mentionned above, but when I immediately post a second time it works !!! So the first time it never works... But now I wonder how this is possible !? Anybody an idea ?
Thanks,
Kristof
Kristof Camelbeke
Ranch Hand

Joined: Nov 28, 2001
Posts: 97
Thanks Jim it works.... The readFully method reads everything in contradiction with the read method... Don't think I would ever solve this problem...
Thanks a lot !
Kristof
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
But now I wonder how this is possible !? Anybody an idea ?
It can depend on a lot of things, e.g. various buffer sizes used by the system when reading sockets - and also it depends on whether the socket has been completely written to yet. If data is being sent from another computer (or another process on this computer, or even another thread in this JVM) then you probably don't have any control over how fast it sends data, or over how quickly the network transfers the data. By default read(byte[]) just grabs whatever data is immediately available; if you want to makesure you have all the data, you need to tell it to wait. If you're not using DataInput and thus don't have a readFully() method, you can do something like:

This assumes your array is already exactly the right size for the data. There are many other ways to set this up, depending on what you want to do - but the general principle is that when using read(byte[]), always check the return value, and do something with it to ensure that you can handle the possibility that not everything is returned at once.
Aside from DataInput's readFully(), the other popular choice is to use a BefferedReader, which has a readLine() method which always waits until it has a full line of text data - which is often exactly what you want, and so it's nice that readLine() makes it easy for you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DatinputStream : length issue
 
Similar Threads
InputStream blocking?
Java client socket to talk to C server socket
PC Connected to Internet and Internet Connectivity?
IP address!
Not getting the bytes sent