Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Bug in java.io.ByteArrayInputStream

Posted on 1997-08-12
4
Medium Priority
?
323 Views
Last Modified: 2008-03-06
Hello,

I am using Sun's JDK 1.1.3. on Win95.   I have come across a bug in
java.io.ByteArrayInputStream class. The mark() and reset() methods do
not work correctly. If a mark is set using mark() and then reset()
called, the mark of the ByteArrayInputStream still remains at the
original value. (By default, the mark is set to the first byte., ie.,
mark = 0). For example, if mark(5) is called, followed by reset(), the
mark still remains at 0.  

This bug has been implicitly reported as a  bug for class
java.io.LineNumberInputStream (Bug ID : 4017159, Synopsis:
java.io.LineNumberInputStream mark/reset methods work wrong. : Submit
Date: Dec 1, 1996).  Since, its been 8 months this bug has been
reported,  I wonder why Sun's Java team has not fixed this bug.  This
bug exists even in JDK 1.1.

Does anybody share any thoughts on this or know what is happening to
this end ?


-Ganesh.
0
Comment
Question by:bkganesh
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:msmolyak
ID: 1224796
Could you give a code sample? I checked the source code of ByteArrayInputStream, the implementation of mark() and reset() methods is rather straightforward. One simply retains the marked position , the other sets the current position to the marked one.

Usually if SUN does not respond they don't consider it a bug.
0
 

Author Comment

by:bkganesh
ID: 1224797
Hello,

Thanks for such a quick reply. From the source code of ByteArrayInputStream, what I understand about mark() and reset() methods is this..
If a ByteArrayInputStream object is instantiated and some 5 bytes are read from it, then the value of "pos" variable will be 5.  If the mark() is called with some argument, then "mark" variable will take the value of "pos" which will be 5. Then, if more bytes are read and a reset() called, the next available byte in the stream will be the 5th one. By this, we can only go back to a previous position in the stream. Incidently, the argument to mark() is not used in the code.  If I would like to set a mark much ahead in the stream, I won't be able to do it. (Is this method really intended to do such things ?) If this is not what the method is supposed to do, please let me know, My understanding of the api documentation might be wrong.

code sample...

class SomeClass
{
    public static void main(String[] argv)
    {
        //create a byte array.
        byte[] byteArray = new byte[10];

        ByteArrayInputStream bais = new         ByteArrayInputStream(byteArray);
        System.out.println("Available is" + bais.available());
        //Available is 10;
        bais.mark(5);
        bais.reset();
        System.out.println("Available is" + bais.available());
        //Available is 10;

        //According to my theory, it should be 5.
    }
}
       
Thanks,
-Ganesh.
       

0
 
LVL 5

Accepted Solution

by:
msmolyak earned 300 total points
ID: 1224798
What the mark() method does, it remembers the current position (rather than the one you pass). The method description is correct (if you read the javadoc), the parameter serves totally different purpose thn you think it does.
here is the description:

 mark

 public synchronized void mark(int readlimit)

     Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at
     the last marked position so that subsequent reads re-read the same bytes.

     The readlimit arguments tells this input stream to allow that many bytes to be read before the mark position
     gets invalidated.

     The mark method of InputStream does nothing.

     Parameters:
          readlimit - the maximum limit of bytes that can be read before the mark position becomes invalid.
     See Also:
          reset

 Thus you code beahves the way it's supposed to, that is when you call mark before reading any data, it stores the position 0. 5 means that the stored position is invalidated after you read 5 bytes.
0
 

Author Comment

by:bkganesh
ID: 1224799
Hi,

Thanks for the answer.  I now get the purpose of mark() and reset() in ByteArrayInputStream. The argument for mark() is probably only used to completely override mark() of InputStream.
The "dummy" argument and it being not used in code left me confused.
Thanks once again,

Bye,
Ganesh.
India.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question