1 36 package org.columba.ristretto.pop3; 37 38 import java.io.FilterInputStream ; 39 import java.io.IOException ; 40 import java.io.InputStream ; 41 42 48 public class POP3MultiLineStream extends FilterInputStream { 49 50 private boolean eof; 51 private int size; 52 private int readCount; 53 private boolean newLine; 54 55 61 public POP3MultiLineStream(InputStream in, int size) { 62 super(in); 63 64 this.size = size; 65 eof = false; 66 67 newLine = true; 68 } 69 70 71 72 75 public int read() throws IOException { 76 if( eof ) return -1; 77 78 int read = in.read(); 80 81 if( read == '.' && newLine ) { 83 read = in.read(); 84 if( read == '\r' ) { 85 eof = true; 87 88 in.read(); 90 91 return -1; 92 } else { 93 readCount++; 95 newLine = false; 96 return read; 97 } 98 } else { 99 newLine = (read == '\n'); 100 readCount++; 101 return read; 102 } 103 } 104 105 108 public int read(byte[] arg0, int arg1, int arg2) throws IOException { 109 int next; 110 for( int i=0; i<arg2; i++) { 111 next = read(); 112 if( next == -1 ) { 113 if( i == 0 ) { 114 return -1; 115 } else { 116 return i; 117 } 118 } 119 arg0[arg1+i] = (byte) next; 120 } 121 return arg2; 122 } 123 124 127 public int available() throws IOException { 128 return size - readCount; 129 } 130 131 } 132 | Popular Tags |