KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > MailReader


1 /*
2  * MailReader.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: MailReader.java,v 1.1.1.1 2002/09/24 16:09:53 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.mail;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26
27 public final class MailReader
28 {
29     private final InputStream JavaDoc 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 JavaDoc inputStream)
38     {
39         this.inputStream = inputStream;
40     }
41
42     public final long getOffset()
43     {
44         return offset;
45     }
46
47     public String JavaDoc readLine() throws IOException JavaDoc
48     {
49         int i = 0;
50         while (true) {
51             if (pos >= count) {
52                 fill();
53                 if (pos >= count) {
54                     // End of stream.
55
if (i > 0)
56                         return new String JavaDoc(chars, 0, i);
57                     else
58                         return null;
59                 }
60             }
61             byte b = buf[pos++];
62             if (b == 10) {
63                 // End of line.
64
++offset;
65                 return new String JavaDoc(chars, 0, i);
66             } else if (b == 13) {
67                 // Ignore.
68
++offset;
69             } else {
70                 if (i == chars.length) {
71                     // Need to grow char array.
72
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 JavaDoc
83     {
84         inputStream.close();
85     }
86
87     private final void fill() throws IOException JavaDoc
88     {
89         pos = 0;
90         count = inputStream.read(buf);
91     }
92 }
93
Popular Tags