1 16 package org.apache.commons.net.io; 17 18 import java.io.IOException ; 19 import java.io.PushbackReader ; 20 import java.io.Reader ; 21 22 37 public final class DotTerminatedMessageReader extends Reader 38 { 39 private static final String LS; 40 private static final char[] LS_CHARS; 41 42 static 43 { 44 LS = System.getProperty("line.separator"); 45 LS_CHARS = LS.toCharArray(); 46 } 47 48 private boolean atBeginning; 49 private boolean eof; 50 private int pos; 51 private char[] internalBuffer; 52 private PushbackReader internalReader; 53 54 59 public DotTerminatedMessageReader(Reader reader) 60 { 61 super(reader); 62 internalBuffer = new char[LS_CHARS.length + 3]; 63 pos = internalBuffer.length; 64 atBeginning = true; 66 eof = false; 67 internalReader = new PushbackReader (reader); 68 } 69 70 82 public int read() throws IOException 83 { 84 int ch; 85 86 synchronized (lock) 87 { 88 if (pos < internalBuffer.length) 89 { 90 return internalBuffer[pos++]; 91 } 92 93 if (eof) 94 { 95 return -1; 96 } 97 98 if ((ch = internalReader.read()) == -1) 99 { 100 eof = true; 101 return -1; 102 } 103 104 if (atBeginning) 105 { 106 atBeginning = false; 107 if (ch == '.') 108 { 109 ch = internalReader.read(); 110 111 if (ch != '.') 112 { 113 eof = true; 115 internalReader.read(); 116 return -1; 117 } 118 else 119 { 120 return '.'; 121 } 122 } 123 } 124 125 if (ch == '\r') 126 { 127 ch = internalReader.read(); 128 129 if (ch == '\n') 130 { 131 ch = internalReader.read(); 132 133 if (ch == '.') 134 { 135 ch = internalReader.read(); 136 137 if (ch != '.') 138 { 139 internalReader.read(); 141 eof = true; 142 } 143 else 144 { 145 internalBuffer[--pos] = (char) ch; 146 } 147 } 148 else 149 { 150 internalReader.unread(ch); 151 } 152 153 pos -= LS_CHARS.length; 154 System.arraycopy(LS_CHARS, 0, internalBuffer, pos, 155 LS_CHARS.length); 156 ch = internalBuffer[pos++]; 157 } 158 else 159 { 160 internalBuffer[--pos] = (char) ch; 161 return '\r'; 162 } 163 } 164 165 return ch; 166 } 167 } 168 169 179 public int read(char[] buffer) throws IOException 180 { 181 return read(buffer, 0, buffer.length); 182 } 183 184 198 public int read(char[] buffer, int offset, int length) throws IOException 199 { 200 int ch, off; 201 synchronized (lock) 202 { 203 if (length < 1) 204 { 205 return 0; 206 } 207 if ((ch = read()) == -1) 208 { 209 return -1; 210 } 211 off = offset; 212 213 do 214 { 215 buffer[offset++] = (char) ch; 216 } 217 while (--length > 0 && (ch = read()) != -1); 218 219 return (offset - off); 220 } 221 } 222 223 229 public boolean ready() throws IOException 230 { 231 synchronized (lock) 232 { 233 return (pos < internalBuffer.length || internalReader.ready()); 234 } 235 } 236 237 251 public void close() throws IOException 252 { 253 synchronized (lock) 254 { 255 if (internalReader == null) 256 { 257 return; 258 } 259 260 if (!eof) 261 { 262 while (read() != -1) 263 { 264 ; 265 } 266 } 267 eof = true; 268 atBeginning = false; 269 pos = internalBuffer.length; 270 internalReader = null; 271 } 272 } 273 } 274 | Popular Tags |