| Author |
datainputstream passing as in parameter
|
bruce truong
Greenhorn
Joined: Mar 14, 2011
Posts: 11
|
|
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
|
 |
Marcus Kelvin
Ranch Hand
Joined: Jan 04, 2012
Posts: 44
|
|
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.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
bruce truong
Greenhorn
Joined: Mar 14, 2011
Posts: 11
|
|
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
|
 |
 |
|
|
subject: datainputstream passing as in parameter
|
|
|