1 17 18 package org.apache.james.imapserver; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.io.OutputStream ; 23 24 32 public class ImapRequestLineReader 33 { 34 private InputStream input; 35 private OutputStream output; 36 37 private boolean nextSeen = false; 38 private char nextChar; 40 ImapRequestLineReader( InputStream input, OutputStream output ) 41 { 42 this.input = input; 43 this.output = output; 44 } 45 46 54 public char nextWordChar() throws ProtocolException 55 { 56 char next = nextChar(); 57 while ( next == ' ' ) { 58 consume(); 59 next = nextChar(); 60 } 61 62 if ( next == '\r' || next == '\n' ) { 63 throw new ProtocolException( "Missing argument." ); 64 } 65 66 return next; 67 } 68 69 75 public char nextChar() throws ProtocolException 76 { 77 if ( ! nextSeen ) { 78 int next = -1; 79 80 try { 81 next = input.read(); 82 } 83 catch ( IOException e ) { 84 throw new ProtocolException( "Error reading from stream." ); 85 } 86 if ( next == -1 ) { 87 throw new ProtocolException( "Unexpected end of stream." ); 88 } 89 90 nextSeen = true; 91 nextChar = ( char ) next; 92 } 94 return nextChar; 95 } 96 97 103 public void eol() throws ProtocolException 104 { 105 char next = nextChar(); 106 107 while ( next == ' ' ) { 109 consume(); 110 next = nextChar(); 111 } 112 113 if ( next == '\r' ) { 115 consume(); 116 next = nextChar(); 117 } 118 119 if ( next != '\n' ) { 121 throw new ProtocolException( "Expected end-of-line, found more characters."); 123 } 124 } 125 126 134 public char consume() throws ProtocolException 135 { 136 char current = nextChar(); 137 nextSeen = false; 138 nextChar = 0; 139 return current; 140 } 141 142 143 149 public void read( byte[] holder ) throws ProtocolException 150 { 151 int readTotal = 0; 152 try 153 { 154 while ( readTotal < holder.length ) 155 { 156 int count = 0; 157 count = input.read( holder, readTotal, holder.length - readTotal ); 158 if ( count == -1 ) { 159 throw new ProtocolException( "Unexpectd end of stream." ); 160 } 161 readTotal += count; 162 } 163 nextSeen = false; 165 nextChar = 0; 166 } 167 catch ( IOException e ) { 168 throw new ProtocolException( "Error reading from stream." ); 169 } 170 171 } 172 173 177 public void commandContinuationRequest() 178 throws ProtocolException 179 { 180 try { 181 output.write( '+' ); 182 output.write( '\r' ); 183 output.write( '\n' ); 184 output.flush(); 185 } 186 catch ( IOException e ) { 187 throw new ProtocolException("Unexpected exception in sending command continuation request."); 188 } 189 } 190 191 public void consumeLine() 192 throws ProtocolException 193 { 194 char next = nextChar(); 195 while ( next != '\n' ) { 196 consume(); 197 next = nextChar(); 198 } 199 consume(); 200 } 201 } 202 | Popular Tags |