1 21 22 package org.armedbear.j.mail; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 27 public final class MailReader 28 { 29 private final InputStream inputStream; 30 31 private byte[] buf = new byte[16384]; 32 private int count; 33 private int pos; 34 private char[] chars = new char[1024]; 35 private long offset; 36 37 public MailReader(InputStream inputStream) 38 { 39 this.inputStream = inputStream; 40 } 41 42 public final long getOffset() 43 { 44 return offset; 45 } 46 47 public String readLine() throws IOException 48 { 49 int i = 0; 50 while (true) { 51 if (pos >= count) { 52 fill(); 53 if (pos >= count) { 54 if (i > 0) 56 return new String (chars, 0, i); 57 else 58 return null; 59 } 60 } 61 byte b = buf[pos++]; 62 if (b == 10) { 63 ++offset; 65 return new String (chars, 0, i); 66 } else if (b == 13) { 67 ++offset; 69 } else { 70 if (i == chars.length) { 71 char[] newChars = new char[chars.length * 2]; 73 System.arraycopy(chars, 0, newChars, 0, chars.length); 74 chars = newChars; 75 } 76 ++offset; 77 chars[i++] = (char) (b & 0xff); 78 } 79 } 80 } 81 82 public void close() throws IOException 83 { 84 inputStream.close(); 85 } 86 87 private final void fill() throws IOException 88 { 89 pos = 0; 90 count = inputStream.read(buf); 91 } 92 } 93 | Popular Tags |