View Javadoc

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) 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 }