• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

datainputstream passing as in parameter

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I am trying to read a binary file using DataInputStream but it's not working as I expected it. So I read in the first block then use that information to call the next block(s). The next block(s) are broken into multiple get methods which has DataInputStream as a IN parameter. Example,

DataInputStream in = new DataInpuStream();
.....
switch(key){
case 'a': getABlock(in);

case 'b': getBBlock(in);

case 'c': getCBlock(in);

}

However, when the get methods are called the file pointer seems to "skip" or in the wrong position. But if I do this then it seems to work:

DataInputStream in = new DataInpuStream();
.....
in.mark(MAXINT);
switch(key){
case 'a': getABlock(in);

case 'b': getBBlock(in);

case 'c': getCBlock(in);

}

In the get method I would have to add "in.reset()". Do I need to use "mark" then "reset"? Can someone explain why the file pointer skips?


Thanks
bt



 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

bruce truong wrote:Can someone explain why the file pointer skips?



File pointers don't mysteriously "skip". It seems to me that you are describing what you think is going on, but acknowledging that it really doesn't make sense. Therefore, chances are that in fact something else is happening, but in order to get someone else to diagnose that, you'd need to post the code you are actually using, or a short example which can actually be run and reproduces the problem.

Eg, your switch/case doesn't have any breaks in it. I presume you left that out for brevity, but you've left out so much, you've probably also left out the real cause of your problem. If there is something you've made a mistake about or misunderstood, it is very unlikely you are going to explain, "Well I did this wrong and it had this consequence", however it will be obvious looking at your code.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you read something from any InputStream or Reader, that data is consumed, and the InputStream / Reader continues to the next piece of data. I think that the problem here is that you need the key inside your get methods. However, as you've found out, that key is no longer available.

Your solution works conditionally. The mark / reset pair allows you to mark a specific point inside the InputStream / Reader, and later return to that very same point (essentially "unreading" everything after the mark). I say conditionally because not every InputStream / Reader supports mark / reset. That's why both classes have method markSupported. Note that you only need to mark what you want to reset; the higher the value for mark, the more memory you need.

If you want this code to work unconditionally, and the only data missing is the key, then you can simply pass the key as a parameter to your get methods. That way you don't have to "unread" anything.
 
bruce truong
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My code is pretty straight forward.


Unfortunately, the data is broken into blocks (A/B/C...) and so I want to call each get method based on the key. I can't pass in the key I can only pass in DataInputStream object.

I don't think this is new so someone must have seen it before, or maybe I am not doing it right.



bt
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic