KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > core > common > io > BufferedWireReader


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */

21 package org.jsmtpd.core.common.io;
22
23 import java.io.EOFException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.Reader JavaDoc;
27
28
29 /**
30  * Reads byte from an InputStream to byte array or String
31  * @author Jean-Francois POUX
32  */

33 public class BufferedWireReader extends Reader JavaDoc {
34
35     private byte[] buffer;
36     private InputStream JavaDoc is;
37     private RefByteArrayOutputStream bos ;
38     private byte[] dataSeparator;
39     private int maxSize;
40     
41     public BufferedWireReader (int bufferSize, byte[] dataSeparator, InputStream JavaDoc is, int maxSize) {
42         buffer=new byte[bufferSize];
43         this.is=is;
44         this.maxSize=maxSize;
45         bos= new RefByteArrayOutputStream();
46         this.dataSeparator=dataSeparator;
47     }
48     
49     public byte[] readBlock () throws IOException JavaDoc {
50         bos.reset();
51         int readCount=0;
52         while (true) {
53             if (is.available()+bos.size()>maxSize)
54                 throw new InputSizeToBig();
55             readCount=is.read(buffer);
56             if (readCount<0)
57                 throw new EOFException JavaDoc();
58             byte[] effectiveData = new byte[readCount];
59             System.arraycopy(buffer,0,effectiveData,0,readCount);
60             bos.write(effectiveData);
61             if (checkEOS())
62                 break;
63         }
64         return bos.toByteArray();
65     }
66     
67     public String JavaDoc readLine () throws IOException JavaDoc {
68         return new String JavaDoc (readBlock());
69     }
70     
71     private boolean checkEOS () {
72         if (bos.size()<dataSeparator.length)
73             return false;
74         byte[] buffer = bos.toRefByteArray();
75         for (int i=0;i<dataSeparator.length;i++) {
76             if (dataSeparator[i]!=buffer[bos.validData()-i-1])
77                 return false;
78         }
79         return true;
80     }
81     
82     public int read(char[] cbuf, int off, int len) throws IOException JavaDoc {
83         return -1;
84     }
85
86     public void close() throws IOException JavaDoc {
87         is.close();
88     }
89
90 }
91
Popular Tags