1 22 23 package gnu.mail.util; 24 25 import java.io.BufferedInputStream ; 26 import java.io.FilterInputStream ; 27 import java.io.InputStream ; 28 import java.io.IOException ; 29 30 import javax.mail.Session ; 31 32 43 public class DotTerminatedInputStream 44 extends ReadFilterInputStream 45 { 46 static final int MarkReadLimit = 5; 47 static final int EOS = -1; 48 49 private boolean isEOS = false; 50 51 55 public DotTerminatedInputStream (InputStream in) { 56 super (in.markSupported () ? in : new BufferedInputStream (in)); 58 } 59 60 66 public int available () 67 throws IOException 68 { 69 int n; 70 if (isEOS) { 71 n = 0; 72 } 73 else { 74 n = super.available (); 75 } 76 77 return n; 78 } 79 80 86 public int read () 87 throws IOException 88 { 89 final int CR = 13; 90 final int LF = 10; 91 final int DOT = '.'; 92 93 int b; 94 95 if (isEOS) { 96 97 b = EOS; 98 99 } 100 else { 101 102 b = super.read (); 103 mark (MarkReadLimit); 104 if (b == CR && 105 super.read () == LF && 106 super.read () == DOT && 107 super.read () == CR && 108 super.read () == LF) { 109 110 isEOS = true; 111 112 } 113 114 else { 115 117 reset (); 118 mark (MarkReadLimit); 119 120 if (b == LF && 121 super.read () == DOT && 122 super.read () == LF) { 123 124 isEOS = true; 125 126 } 127 } 128 129 if (isEOS) { 130 131 b = EOS; 133 134 } 135 136 else { 137 138 reset (); 139 140 } 141 } 142 143 return b; 144 } 145 } 146 147 | Popular Tags |