Solved

Bug in java.io.ByteArrayInputStream

Posted on 1997-08-12
4
281 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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
what is the difference between "sudo su" and "su - root" 6 106
get weblogic logged in user in java 2 58
servlet  URL Rewriting 1 27
java stored proc example 9 23
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now