aspose file tools
The moose likes I/O and Streams and the fly likes mark( int marklimit) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "mark( int marklimit)" Watch "mark( int marklimit)" New topic

mark( int marklimit)

Altug Altintas

Joined: Jun 28, 2002
Posts: 3
Can anybody post a small code that uses marklimit parameter properly. I use BufferedInputStream class but it seems that mark() method ignores marklimit parameter.
Any idea ?
Snigdha Solanki
Ranch Hand

Joined: Sep 07, 2000
Posts: 128
I am not sure but it looks like readlimit
parameter is ignored.The output of this program
is same independent of the argument to mark.

class MarkTest{
public static void main(String args[]){
InputStream s = new FileInputStream("D:\\test.txt");
BufferedInputStream t = new BufferedInputStream(s);
byte b[] = new byte[10];
if(t.markSupported() == true)
System.out.println("Mark supported");
System.out.println("Mark not supported");
System.out.println("byte read"+b[0]+" "+b[1]+" "+b[2]);
System.out.println("Reset called");,0,3);
System.out.println("byte read"+b[0]+" "+b[1]+" "+b[2]);

catch(Exception e){
System.out.println("Exception is"+e);

where test.txt contains string - ABCDEF
Output is:
Mark supported
byte read65 66 67
Reset called
byte read65 66 67

Please advise.

Snigdha<br />Sun Certified Programmer for the Java™ 2 Platform
Altug Altintas

Joined: Jun 28, 2002
Posts: 3
In theory it must do like that but i couldn't find any condition like that, it always reads fix size an i think it ignores the marklimit parameter. I investigate the BufferedReader's source code and i found that if markpos=0 then it grows the buf, it means that when u give a mark limit parameter, BufferedReader ignores it until markpos (mark position in inputstream) is 0 but markpos parameter cant be null because when we mark something in the stream then it is obvious that marklimit must be differ than 0 (greater tha zero).
fill() metod in BufferedInputStream class
* Fills the buffer with more data, taking into account
* shuffling and other tricks for dealing with marks.
* Assumes that it is being called by a synchronized method.
* This method also assumes that all data has already been read in,
* hence pos > count.
private void fill() throws IOException {
if (markpos < 0)
pos = 0; /* no mark: throw away the buffer */
else if (pos >= buf.length) /* no room left in buffer */
if (markpos > 0) { /* can throw away early part of the buffer */
int sz = pos - markpos;
System.arraycopy(buf, markpos, buf, 0, sz);
pos = sz;
markpos = 0;
} else if (buf.length >= marklimit) {
markpos = -1; /* buffer got too big, invalidate mark */
pos = 0; /* drop buffer contents */
} else { /* grow buffer */
int nsz = pos * 2;
if (nsz > marklimit)
nsz = marklimit;
byte nbuf[] = new byte[nsz];
System.arraycopy(buf, 0, nbuf, 0, pos);
buf = nbuf;
count = pos;
int n =, pos, buf.length - pos);
if (n > 0)
count = n + pos;
any idea ?
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Please review the JavaRanch Naming Policy then edit your profile so that your publicly displayed name complies with the rules.
Thanks for your cooperation.
Altug Altintas

Joined: Jun 28, 2002
Posts: 3
Sorry, i have changed it .
Jerry Pulley
Ranch Hand

Joined: Sep 19, 2000
Posts: 221
We have a few calls to mark( int ) in a rather large app; they worked fine under 1.2 but reset() started failing when we switched to 1.3.1. The problem was the argument - it needs to be one greater than the maximum number of bytes you expect to read before resetting.
Read the fine Javadoc (carefully). It states "The readlimit arguments tells this input stream to allow that many bytes to be read before the mark position gets invalidated." The implication is that if you call mark( 10 ) then the mark may be discarded anytime after the 10th character is read.
How many bytes may actually be read before reset() starts failing seems to vary with the situation; all that mark()'s contract guarantees is that you can read at least (readlimit - 1) bytes and still reset() the stream. The stream must allow (readlimit - 1); it may allow more. In 1.2, the reset buffer size seemed to be a power of 2, at least for small readlimit. In 1.3.1 and up, the implementations seem more aggressive in sizing the buffer - still, I doubt that

will ever fail. But you can't rely on it.
[ July 12, 2002: Message edited by: Jerry Pulley ]
I agree. Here's the link:
subject: mark( int marklimit)