Hi Sandeep Sharma,

Your code has some problems:
1) In the readBlockStream method, you read a whole
    block (2048) bytes, but you forgot a loop to test
    all occurences of the terminating characters (actually
    it is only tested once).
2) No sign of code to test the terminating characters ("\n.\r\n")
    between two blocks.
3) OptTerminatedInputStream reads too much bytes from the underlaying InputStream 
(given
    in the constructor). You cannot expect, that the underlaying InputStream
    ends with the terminating characters, so that OptTerminatedInputStream may steal
    bytes from underlaying InputStream, which are not accessible by other code.
    I think this will be a problem, if more than 1 emails are sent in the same stream.
    And this is may main concern about your solution here. I mentioned this in an 
earlier email in this Thread,
    in which I sketched a solution to this problem, but at the same time, I think it 
will
    eat um any performance improvements.


Bye,

Stephan



Sandeep Sharma wrote:
> 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]>
> 
> 




--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to