1 package org.jsmtpd.core.common.io.commandStream; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.util.LinkedList ; 6 import java.util.List ; 7 8 import org.jsmtpd.core.common.io.BareLFException; 9 import org.jsmtpd.core.common.io.BufferedWireReader; 10 import org.jsmtpd.core.common.io.InputSizeToBig; 11 import org.jsmtpd.tools.ByteArrayTool; 12 13 14 public class MultiLineCommandStreamParser { 15 private List <byte[]> partBuffer = new LinkedList <byte[]>(); 16 private BufferedWireReader rd; 17 private boolean rejectBareLF; 18 19 public MultiLineCommandStreamParser(InputStream is, int maxMessageSize, boolean rejectBareLF) { 20 this.rejectBareLF = rejectBareLF; 21 rd= new BufferedWireReader (512,ByteArrayTool.LF,is,maxMessageSize); 22 } 23 24 public String readLine() throws InputSizeToBig, IOException , BareLFException { 25 byte[] data = getData(); 26 27 if (data.length<3) 28 return ""; 29 if ((data[data.length-2]!=ByteArrayTool.CR[0]) && rejectBareLF) 30 throw new BareLFException(); 31 32 String res = new String (data); 33 res=res.replaceAll("\r",""); 34 res=res.replaceAll("\n",""); 35 return res; 36 } 37 38 private byte[] getData () throws InputSizeToBig, IOException , BareLFException{ 39 byte[] data; 40 if (partBuffer.size()==0) 41 fillBuffer(); 42 if (partBuffer.size()==0) 43 throw new IOException (); 44 data = partBuffer.get(0); 45 partBuffer.remove(0); 46 return data; 47 } 48 49 private void fillBuffer () throws InputSizeToBig, IOException , BareLFException{ 50 byte[] data = rd.readBlock(); 51 if (data==null) 52 throw new IOException ("Null stream"); 53 54 List <byte[]> tmp = ByteArrayTool.split(data,ByteArrayTool.LF[0]); 55 for (int i=0;i<tmp.size();i++) { 56 partBuffer.add(tmp.get(i)); 57 } 58 } 59 } 60 | Popular Tags |