• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

mark( int marklimit)

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
Regards.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
import java.io.*;

class MarkTest{
public static void main(String args[]){
try{
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");
else
System.out.println("Mark not supported");
t.mark(1);
t.read(b,0,3);
System.out.println("byte read"+b[0]+" "+b[1]+" "+b[2]);
t.reset();
System.out.println("Reset called");
t.read(b,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.
 
Altug Altintas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 = in.read(buf, pos, buf.length - pos);
if (n > 0)
count = n + pos;
}
************************************************
any idea ?
Regards
Altug.
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"upux"
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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, i have changed it .
Regards
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
ok,
jp
[ July 12, 2002: Message edited by: Jerry Pulley ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic