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