1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.mime4j.io;
21
22 import org.apache.james.mime4j.util.ByteArrayBuffer;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26
27
28
29
30
31
32 public class LineReaderInputStreamAdaptor extends LineReaderInputStream {
33
34 private final LineReaderInputStream bis;
35 private final int maxLineLen;
36
37 private boolean used = false;
38 private boolean eof = false;
39
40 public LineReaderInputStreamAdaptor(
41 final InputStream is,
42 int maxLineLen) {
43 super(is);
44 if (is instanceof LineReaderInputStream) {
45 this.bis = (LineReaderInputStream) is;
46 } else {
47 this.bis = null;
48 }
49 this.maxLineLen = maxLineLen;
50 }
51
52 public LineReaderInputStreamAdaptor(
53 final InputStream is) {
54 this(is, -1);
55 }
56
57 @Override
58 public int read() throws IOException {
59 int i = in.read();
60 this.eof = i == -1;
61 this.used = true;
62 return i;
63 }
64
65 @Override
66 public int read(byte[] b, int off, int len) throws IOException {
67 int i = in.read(b, off, len);
68 this.eof = i == -1;
69 this.used = true;
70 return i;
71 }
72
73 @Override
74 public int readLine(final ByteArrayBuffer dst) throws IOException {
75 int i;
76 if (this.bis != null) {
77 i = this.bis.readLine(dst);
78 } else {
79 i = doReadLine(dst);
80 }
81 this.eof = i == -1;
82 this.used = true;
83 return i;
84 }
85
86 private int doReadLine(final ByteArrayBuffer dst) throws IOException {
87 int total = 0;
88 int ch;
89 while ((ch = in.read()) != -1) {
90 dst.append(ch);
91 total++;
92 if (this.maxLineLen > 0 && dst.length() >= this.maxLineLen) {
93 throw new MaxLineLimitException("Maximum line length limit exceeded");
94 }
95 if (ch == '\n') {
96 break;
97 }
98 }
99 if (total == 0 && ch == -1) {
100 return -1;
101 } else {
102 return total;
103 }
104 }
105
106 public boolean eof() {
107 return this.eof;
108 }
109
110 public boolean isUsed() {
111 return this.used;
112 }
113
114 @Override
115 public String toString() {
116 return "[LineReaderInputStreamAdaptor: " + bis + "]";
117 }
118 }