View Javadoc
1 /* 2 * Copyright (C) The Apache Software Foundation. All rights reserved. 3 * 4 * This software is published under the terms of the Apache Software License 5 * version 1.1, a copy of which has been included with this distribution in 6 * the LICENSE file. 7 */ 8 package org.apache.james.smtpserver; 9 10 import java.io.FilterInputStream; 11 import java.io.IOException; 12 import java.io.InputStream; 13 14 /*** 15 * Removes the dot-stuffing happening during the SMTP DATA transport 16 * 17 * @author Serge Knystautas <sergek@lokitech.com> 18 */ 19 public class SMTPInputStream extends FilterInputStream { 20 /*** 21 * An array to hold the last two bytes read off the stream. 22 * This allows the stream to detect '\r\n' sequences even 23 * when they occur across read boundaries. 24 */ 25 protected int last[] = new int[2]; 26 27 public SMTPInputStream(InputStream in) { 28 super(in); 29 last[0] = -1; 30 last[1] = -1; 31 } 32 33 /*** 34 * Read through the stream, checking for '\r\n.' 35 * 36 * @return the byte read from the stream 37 */ 38 public int read() throws IOException { 39 int b = in.read(); 40 if (b == '.' && last[0] == '\r' && last[1] == '\n') { 41 //skip this '.' because it should have been stuffed 42 b = in.read(); 43 } 44 last[0] = last[1]; 45 last[1] = b; 46 return b; 47 } 48 49 /*** 50 * Read through the stream, checking for '\r\n.' 51 * 52 * @param b the byte array into which the bytes will be read 53 * @param off the offset into the byte array where the bytes will be inserted 54 * @param len the maximum number of bytes to be read off the stream 55 * @return the number of bytes read 56 */ 57 public int read(byte[] b, int off, int len) throws IOException { 58 if (b == null) { 59 throw new NullPointerException(); 60 } else if ((off < 0) || (off > b.length) || (len < 0) || 61 ((off + len) > b.length) || ((off + len) < 0)) { 62 throw new IndexOutOfBoundsException(); 63 } else if (len == 0) { 64 return 0; 65 } 66 67 int c = read(); 68 if (c == -1) { 69 return -1; 70 } 71 b[off] = (byte)c; 72 73 int i = 1; 74 75 for (; i < len ; i++) { 76 c = read(); 77 if (c == -1) { 78 break; 79 } 80 if (b != null) { 81 b[off + i] = (byte)c; 82 } 83 } 84 85 return i; 86 } 87 }

This page was automatically generated by Maven