001 package com.hs.mail.io;
002
003 import java.io.IOException;
004 import java.io.InputStream;
005 import java.io.InputStreamReader;
006
007 public class LineReader extends InputStreamReader {
008
009 private int maxLineLen = 2048;
010
011 public LineReader(InputStream in) {
012 super(in);
013 }
014
015 public String readLine() throws IOException {
016 StringBuffer sb = new StringBuffer();
017 int bytesRead = 0;
018 while (bytesRead++ < maxLineLen) {
019 int iRead = read();
020 switch (iRead) {
021 case '\r':
022 iRead = read();
023 if (iRead == '\n') {
024 return sb.toString();
025 }
026 // fall through
027 case '\n':
028 // LF without a preceding CR
029 throw new IOException("Bad line terminator");
030 case -1:
031 // premature EOF
032 return null;
033 default:
034 sb.append((char) iRead);
035 }
036 }
037 throw new IOException("Exceeded maximun line length");
038 }
039
040 }