Hi Stephan,

go through the code below

This the first version of the patch for
CharTerminatedInputStream, this code is just for the
testing

Also note that i have hardcoded certain things, 
following code also contains a bug (if u try to send
mails from telnet, however that can be removed easily)

-----------Start of Patch
public class OptTerminatedStream 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 totalb = 0;
        
    public OptTerminatedStream(InputStream in, char[]
terminator) throws Exception 
        {               
                //Initialize buffers            
        this.in = in;
                readByteStream();
        }
        
        /**
         * Readahed a block of data
         */
        public void readByteStream() throws IOException 
        {
                String line = "";
                int len;
                len=0;
                
                        if (!cont) 
                        {
                                bcount = -1;
                                pos = -1;
                                return;
                        }
                        
                        bcount = in.read(buf);
                        
                        if (bcount == -1) //if end-of-file reached
                                return;         
                        
                        len = bcount;
                        pos = 0;
                        try 
                        {                               
                                if (
                                        buf[len - 4] == term[0] && 
                                        buf[len - 3] == term[1] && 
                                        buf[len - 2] == term[2] && 
                                        buf[len - 1] == term[3]
                                        )
                                {
                                        cont = false;
                                        bcount-=4;
                                }
                        } catch (ArrayIndexOutOfBoundsException aiob){}
        }
        
    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;
        }
}
---------------End of Patch

__________________________________________________
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]>

Reply via email to