Solved

Bug in java.io.ByteArrayInputStream

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 46
Certificat to iSeries KeyStore 1 30
Selenium docs api java index 3 97
jsp error 6 71
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
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…
Suggested Courses

739 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