KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > core > common > io > commandStream > CommandStreamParser


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.commandStream;
22
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25
26 import org.jsmtpd.core.common.io.BareLFException;
27 import org.jsmtpd.core.common.io.BufferedWireReader;
28 import org.jsmtpd.core.common.io.InputSizeToBig;
29 import org.jsmtpd.tools.ByteArrayTool;
30
31 /**
32  * Used to read input and get strings from it, to replace a bufferedreader/readline<br>
33  * Strings are size limited, to prevent someone from injecting long random data to server, causing it to
34  * burn memory until out of memory exception...
35  * @author Jean-Francois POUX
36  */

37 public class CommandStreamParser {
38
39     private boolean rejectBareLF;
40     private BufferedWireReader rd;
41     
42     public CommandStreamParser(InputStream JavaDoc is, int maxMessageSize, boolean rejectBareLF) {
43         this.rejectBareLF = rejectBareLF;
44         rd= new BufferedWireReader (512,ByteArrayTool.LF,is,maxMessageSize);
45     }
46
47     public String JavaDoc readLine() throws InputSizeToBig, IOException JavaDoc, BareLFException {
48         /*
49         // BOs stuff, read in a loop for telnet-like client :D
50         byte[] buffer = new byte[maxMessageSize];
51         int readCount = 0;
52
53         readCount = is.read(buffer);
54         String dbg = new String (buffer);
55         if (readCount > 3) {
56             byte[] realBuffer = new byte[readCount];
57             System.arraycopy(buffer, 0, realBuffer, 0, readCount);
58
59
60             if (realBuffer[readCount - 1] != ByteArrayTool.LF[0])
61                 throw new InputSizeToBig();
62
63             if ((realBuffer[readCount - 2] != ByteArrayTool.CR[0]) && (rejectBareLF))
64                 throw new BareLFException();
65
66             return new String(realBuffer).trim();
67         }
68
69         return null;
70         */

71         byte[] data = rd.readBlock();
72         
73         if (data==null)
74             throw new IOException JavaDoc("Null stream");
75         if (data.length<3)
76             return "";
77         
78         if ((data[data.length-2]!=ByteArrayTool.CR[0]) && rejectBareLF)
79             throw new BareLFException();
80         
81         String JavaDoc res = new String JavaDoc (data);
82         res=res.replaceAll("\r","");
83         res=res.replaceAll("\n","");
84         
85         
86         return res;
87     }
88
89 }
Popular Tags