1 17 18 package org.apache.james.util; 19 20 import java.io.FilterInputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 24 28 public class DotStuffingInputStream extends FilterInputStream { 29 34 protected int last[] = new int[2]; 35 36 public DotStuffingInputStream(InputStream in) { 37 super(in); 38 last[0] = -1; 39 last[1] = -1; 40 } 41 42 47 public int read() throws IOException { 48 int b = in.read(); 49 if (b == '.' && last[0] == '\r' && last[1] == '\n') { 50 b = in.read(); 52 } 53 last[0] = last[1]; 54 last[1] = b; 55 return b; 56 } 57 58 66 public int read(byte[] b, int off, int len) throws IOException { 67 if (b == null) { 68 throw new NullPointerException (); 69 } else if ((off < 0) || (off > b.length) || (len < 0) || 70 ((off + len) > b.length) || ((off + len) < 0)) { 71 throw new IndexOutOfBoundsException (); 72 } else if (len == 0) { 73 return 0; 74 } 75 76 int c = read(); 77 if (c == -1) { 78 return -1; 79 } 80 b[off] = (byte)c; 81 82 int i = 1; 83 84 for (; i < len ; i++) { 85 c = read(); 86 if (c == -1) { 87 break; 88 } 89 if (b != null) { 90 b[off + i] = (byte)c; 91 } 92 } 93 94 return i; 95 } 96 } 97 | Popular Tags |