KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > core > common > io > dataStream > DataStreamParser


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.dataStream;
22
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26
27 import org.jsmtpd.core.common.io.BareLFException;
28 import org.jsmtpd.core.common.io.InvalidStreamParserInitialisation;
29 import org.jsmtpd.core.common.io.InputSizeToBig;
30
31 /**
32  * @author Jean-Francois POUX
33  */

34 public class DataStreamParser {
35
36     private ByteArrayOutputStream JavaDoc bos = null;
37     private byte[] buffer = null;
38     private long maxMessageSize;
39     private int readSize = 0;
40     private byte[] cleanData = null;
41     private long currentSize = 0;
42
43     /**
44      *
45      * @param bufferSize read buffer to use
46      * @param maxMessageSize maximum message size
47      * @throws InvalidStreamParserInitialisation
48      */

49     public DataStreamParser(int bufferSize, long maxMessageSize) throws InvalidStreamParserInitialisation {
50         if (bufferSize < 10)
51             throw new InvalidStreamParserInitialisation();
52         
53         if (maxMessageSize < 10)
54             throw new InvalidStreamParserInitialisation();
55         
56         bos = new ByteArrayOutputStream JavaDoc(bufferSize);
57         this.maxMessageSize = maxMessageSize;
58         buffer = new byte[bufferSize];
59     }
60
61     /**
62      * adds a string to the buffer
63      * @param toAppend
64      */

65     public void appendString(String JavaDoc toAppend) {
66         String JavaDoc tmp = toAppend + "\r\n";
67         try {
68             bos.write(tmp.getBytes());
69         } catch (IOException JavaDoc e) {}
70     }
71
72     /**
73      * Reads the input stream by block, returns when crlf is encountered
74      * @param in
75      * @throws IOException
76      * @throws InputSizeToBig
77      * @throws BareLFException
78      */

79     public void parseInputStream(InputStream JavaDoc in) throws IOException JavaDoc, InputSizeToBig, BareLFException {
80         while (true) {
81             // out bound ex !
82
readSize = in.read(buffer);
83             if (readSize<0)
84                 throw new IOException JavaDoc("Negative byte count read");
85             currentSize += readSize;
86             bos.write(buffer, 0, readSize);
87             if (currentSize > maxMessageSize)
88                 throw new InputSizeToBig();
89
90             if (checkEOS())
91                 break;
92
93         }
94     }
95
96     /**
97      * Checks in the buffer if there is a crlf sequence
98      * @return
99      * @throws BareLFException
100      */

101     private boolean checkEOS() throws BareLFException {
102
103         // Detect CR LF . CR LF => return true
104
if (buffer[readSize - 1] == 10) // don't cast bos to array immediatly (inc perf)
105
{
106             int sz = bos.size();
107             byte[] pt = bos.toByteArray();
108             if ((pt[sz - 2] == 13) && (pt[sz - 3] == 46) && (pt[sz - 4] == 10) && (pt[sz - 5] == 13)) { // last thing in the buffer is <CR><LF>.<CR><LF>
109
cleanData = new byte[bos.size() - 5];
110                 System.arraycopy(bos.toByteArray(), 0, cleanData, 0, bos.size() - 5);
111                 return true;
112             }
113         }
114
115         return false;
116     }
117
118     public byte[] getData() {
119         if (cleanData != null)
120             return cleanData;
121         return bos.toByteArray();
122     }
123
124     public void checkData() {
125     }
126 }
Popular Tags