Solved

Bug in java.io.ByteArrayInputStream

Posted on 1997-08-12
4
276 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

772 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

11 Experts available now in Live!

Get 1:1 Help Now