1 | /**************************************************************** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
3 | * or more contributor license agreements. See the NOTICE file * |
---|
4 | * distributed with this work for additional information * |
---|
5 | * regarding copyright ownership. The ASF licenses this file * |
---|
6 | * to you under the Apache License, Version 2.0 (the * |
---|
7 | * "License"); you may not use this file except in compliance * |
---|
8 | * with the License. You may obtain a copy of the License at * |
---|
9 | * * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
11 | * * |
---|
12 | * Unless required by applicable law or agreed to in writing, * |
---|
13 | * software distributed under the License is distributed on an * |
---|
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
15 | * KIND, either express or implied. See the License for the * |
---|
16 | * specific language governing permissions and limitations * |
---|
17 | * under the License. * |
---|
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 | * <code>InputStream</code> used by the MIME parser to detect whether the |
---|
29 | * underlying data stream was used (read from) and whether the end of the |
---|
30 | * stream was reached. |
---|
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) |
---|
75 | throws MaxLineLimitException, IOException { |
---|
76 | int i; |
---|
77 | if (this.bis != null) { |
---|
78 | i = this.bis.readLine(dst); |
---|
79 | } else { |
---|
80 | i = doReadLine(dst); |
---|
81 | } |
---|
82 | this.eof = i == -1; |
---|
83 | this.used = true; |
---|
84 | return i; |
---|
85 | } |
---|
86 | |
---|
87 | private int doReadLine(final ByteArrayBuffer dst) |
---|
88 | throws MaxLineLimitException, IOException { |
---|
89 | int total = 0; |
---|
90 | int ch; |
---|
91 | while ((ch = in.read()) != -1) { |
---|
92 | dst.append(ch); |
---|
93 | total++; |
---|
94 | if (this.maxLineLen > 0 && dst.length() >= this.maxLineLen) { |
---|
95 | throw new MaxLineLimitException("Maximum line length limit exceeded"); |
---|
96 | } |
---|
97 | if (ch == '\n') { |
---|
98 | break; |
---|
99 | } |
---|
100 | } |
---|
101 | if (total == 0 && ch == -1) { |
---|
102 | return -1; |
---|
103 | } else { |
---|
104 | return total; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | public boolean eof() { |
---|
109 | return this.eof; |
---|
110 | } |
---|
111 | |
---|
112 | public boolean isUsed() { |
---|
113 | return this.used; |
---|
114 | } |
---|
115 | |
---|
116 | @Override |
---|
117 | public String toString() { |
---|
118 | return "[LineReaderInputStreamAdaptor: " + bis + "]"; |
---|
119 | } |
---|
120 | |
---|
121 | @Override |
---|
122 | public boolean unread(ByteArrayBuffer buf) { |
---|
123 | if (bis != null) { |
---|
124 | return bis.unread(buf); |
---|
125 | } else { |
---|
126 | return false; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | @Override |
---|
131 | public long skip(long count) throws IOException { |
---|
132 | if (count <= 0) { |
---|
133 | return 0; // So specified by InputStream.skip(long). |
---|
134 | } |
---|
135 | final int bufferSize = count > 8192 ? 8192 : (int) count; |
---|
136 | final byte[] buffer = new byte[bufferSize]; |
---|
137 | long result = 0; |
---|
138 | while (count > 0) { |
---|
139 | int res = read(buffer); |
---|
140 | if (res == -1) { |
---|
141 | break; |
---|
142 | } |
---|
143 | result += res; |
---|
144 | count -= res; |
---|
145 | } |
---|
146 | return result; |
---|
147 | } |
---|
148 | } |
---|