• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mark() -- reset()

 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same as "Whose run() to use"
 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry naveen I couldn't get you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic