• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DatinputStream : length issue

 
Kristof Camelbeke
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kristof Camelbeke
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic