1 21 package org.jsmtpd.core.common.io; 22 23 import java.io.EOFException ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.Reader ; 27 28 29 33 public class BufferedWireReader extends Reader { 34 35 private byte[] buffer; 36 private InputStream is; 37 private RefByteArrayOutputStream bos ; 38 private byte[] dataSeparator; 39 private int maxSize; 40 41 public BufferedWireReader (int bufferSize, byte[] dataSeparator, InputStream 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 { 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 (); 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 readLine () throws IOException { 68 return new String (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 { 83 return -1; 84 } 85 86 public void close() throws IOException { 87 is.close(); 88 } 89 90 } 91 | Popular Tags |