aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes mark()  --  reset() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "mark()  --  reset()" Watch "mark()  --  reset()" New topic
Author

mark() -- reset()

Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
Hi friends
Consider the code
import java.io.*;
class instream{
public static void main(String []a){
try{ String st="abcdefghijklmnop";
byte[] b=st.getBytes();
ByteArrayInputStream s=new ByteArrayInputStream(b);
int i=s.available();
for ( int z=0;z<=i-1 ;z++ ){ System.out.println((char)s.read());
if(z==3) s.mark(2);// Line1
}
s.reset();
System.out.println(" reseting done "+(char)s.read());
}
catch(Exception e){System.out.println("cant");}
}
}
The problem is that on line 1 I'm giving a parameter of value 2 to mark method it means validity of marking position should
expire (as given by Api doc ) but still I'm getting 'e' at the end.
please help!

[This message has been edited by Nasir Khan (edited December 05, 2000).]
[This message has been edited by Nasir Khan (edited December 05, 2000).]
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
In InputStream, mark(int readAheadLimit) will invalidate the mark after the number of bytes read exceed the readAheadLimit. ByteInputStream overrides this and doen't do the invalidation that InputStream does, your mark will remain until you change it.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
Thanks Carl
I've been snooping the source code of InputStream classes and found out that readAheadLimit doesn't work with
ByteInputStream but It should work with BufferedInputStream object .so I tried with this
import java.io.*;
class instream{
public static void main(String []a){
try{ String st="abcdefghijklmnop";
byte[] b=st.getBytes();
ByteArrayInputStream s=new ByteArrayInputStream(b);
BufferedInputStream d=new BufferedInputStream(s);
int i=d.available();
for ( int z=0;z<=i-1 ;z++ ){ System.out.println((char)d.read());
if(z==3) d.mark(2);// Line1
}
d.reset();
System.out.println(" reseting done "+(char)d.read());
}
catch(Exception e){System.out.println("cant");}
}
}
But still the same ... mark position doesn't get invlidated after reading 2 no of bytes.
It means we can't make a use of it here as well..
Is that right?or i'm making some mistake ..
There's no way using mark reset method with FileInputStream , ObjectInputStream,PushbackInputStream,
SequenceInputStream and DataInputStream .Am I right?
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
It still won't work because the underlaying stream is a ByteArrayInputStream..... The other InputStreams you mention, all inherate direct from InputStream and do not override mark() or rest() so... As long as your not creating them on a ByteArrayInputStream, mark and rest will work as documented in InptStream.
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
But BufferedInputStream is behaving as a wrapper around ByteArrayInputStream and I think all functioality will be
provided by BufferedIputStream not by ByteArrayInputStream.
naveen sahu
Ranch Hand

Joined: Nov 15, 2000
Posts: 48
Same as "Whose run() to use"
Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
Sorry naveen I couldn't get you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: mark() -- reset()