Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Bug in java.io.ByteArrayInputStream

Posted on 1997-08-12
4
Medium Priority
?
317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

688 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