Hi Stephan Schiessling, Below is the second version of the code in which i have taken care of some of the issues raised by u
Bye
import java.io.*;
/**
* @author: Sandiep. U. Sharma
*/
public class NewOptTerminatedStream extends
InputStream
{
private InputStream in;
static final byte term[] = {(byte) '\n', (byte)
'.',(byte) '\r',(byte) '\n'};
byte buf[] = new byte[2048]; //allocate buffer
int bcount = -1;
int pos = -1;
boolean cont = true;
int len;
int j=0;
public NewOptTerminatedStream(InputStream in,
char[] terminator) throws Exception
{
//Initialize buffers
this.in = in;
readByteStream();
}
/**
* Readahed a block of data
*/
public void readByteStream() throws IOException
{
len=0;
if (!cont)
{
bcount = -1; pos = -1;
return;
}
bcount = in.read(buf);
int c=0;
if (j>0){c=j;} //If term char is pending for next
block
for (int i = 0;i<bcount;i++)
{
if (term[c] == buf[i])
{
for (j = c;(j<4) && (i < bcount);j++)
{
if (term[j] != buf[i++])
{
j = -1;
break;
}
}
if (j == 4) //End of file found
{
len = i-4;
pos = 0;
if((bcount > len) && (in instanceof
PushbackInputStream))
{
//If in is a PushBackInputStream the
unread
extra bytes readed
((PushbackInputStream)in).unread(buf,len,(bcount-len));
}
bcount=len;
cont = false;
System.out.println("End of file reached");
break;
}
} //End of If block
} // End of for loop
if (cont)
{
len = bcount;
pos = 0;
}
}
public int read() throws IOException
{
if ((bcount >0) && (pos < bcount))
{
return buf[pos++];
} else if (!cont || (bcount == -1)) {
return -1;
} else {
this.readByteStream();
return buf[pos++];
}
}
public int read(byte[] b,int off,int len) throws
IOException
{
if (b.length == 0)
return 0;
if ((bcount >0) && (pos < bcount))
{
int curlen = bcount-pos;
if (curlen <= len)
{
System.arraycopy(buf,pos,b,off,curlen);
this.readByteStream();
return curlen;
} else if (curlen > len)
{
curlen = len;
System.arraycopy(buf,pos,b,off,curlen);
pos = pos + curlen-1;
return curlen;
}
} else if (!cont || (bcount == -1)) {
return -1;
}
return -1;
}
}
__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
