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.util;
21  
22  import java.io.FilterInputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  
26  /***
27   * Removes the dot-stuffing happening during the NNTP and SMTP message
28   * transfer
29   */
30  public class DotStuffingInputStream extends FilterInputStream {
31      /***
32       * An array to hold the last two bytes read off the stream.
33       * This allows the stream to detect '\r\n' sequences even
34       * when they occur across read boundaries.
35       */
36      protected int last[] = new int[2];
37  
38      public DotStuffingInputStream(InputStream in) {
39          super(in);
40          last[0] = -1;
41          last[1] = -1;
42      }
43  
44      /***
45       * Read through the stream, checking for '\r\n.'
46       *
47       * @return the byte read from the stream
48       */
49      public int read() throws IOException {
50          int b = in.read();
51          if (b == '.' && last[0] == '\r' && last[1] == '\n') {
52              //skip this '.' because it should have been stuffed
53              b = in.read();
54          }
55          last[0] = last[1];
56          last[1] = b;
57          return b;
58      }
59  
60      /***
61       * Read through the stream, checking for '\r\n.'
62       *
63       * @param b the byte array into which the bytes will be read
64       * @param off the offset into the byte array where the bytes will be inserted
65       * @param len the maximum number of bytes to be read off the stream
66       * @return the number of bytes read
67       */
68      public int read(byte[] b, int off, int len) throws IOException {
69          if (b == null) {
70              throw new NullPointerException();
71          } else if ((off < 0) || (off > b.length) || (len < 0) ||
72                 ((off + len) > b.length) || ((off + len) < 0)) {
73              throw new IndexOutOfBoundsException();
74          } else if (len == 0) {
75              return 0;
76          }
77  
78          int c = read();
79          if (c == -1) {
80              return -1;
81          }
82          b[off] = (byte)c;
83  
84          int i = 1;
85  
86          for (; i < len ; i++) {
87              c = read();
88              if (c == -1) {
89                  break;
90              }
91              if (b != null) {
92                  b[off + i] = (byte)c;
93              }
94          }
95  
96          return i;
97      }
98  }