1 16 package org.apache.commons.net.io; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.PushbackInputStream ; 21 22 32 33 public final class FromNetASCIIInputStream extends PushbackInputStream 34 { 35 static final boolean _noConversionRequired; 36 static final String _lineSeparator; 37 static final byte[] _lineSeparatorBytes; 38 39 static { 40 _lineSeparator = System.getProperty("line.separator"); 41 _noConversionRequired = _lineSeparator.equals("\r\n"); 42 _lineSeparatorBytes = _lineSeparator.getBytes(); 43 } 44 45 private int __length = 0; 46 47 56 public static final boolean isConversionRequired() 57 { 58 return !_noConversionRequired; 59 } 60 61 65 public FromNetASCIIInputStream(InputStream input) 66 { 67 super(input, _lineSeparatorBytes.length + 1); 68 } 69 70 71 private int __read() throws IOException 72 { 73 int ch; 74 75 ch = super.read(); 76 77 if (ch == '\r') 78 { 79 ch = super.read(); 80 if (ch == '\n') 81 { 82 unread(_lineSeparatorBytes); 83 ch = super.read(); 84 --__length; 86 } 87 else 88 { 89 if (ch != -1) 90 unread(ch); 91 return '\r'; 92 } 93 } 94 95 return ch; 96 } 97 98 99 112 public int read() throws IOException 113 { 114 if (_noConversionRequired) 115 return super.read(); 116 117 return __read(); 118 } 119 120 121 132 public int read(byte buffer[]) throws IOException 133 { 134 return read(buffer, 0, buffer.length); 135 } 136 137 138 152 public int read(byte buffer[], int offset, int length) throws IOException 153 { 154 int ch, off; 155 156 if (length < 1) 157 return 0; 158 159 ch = available(); 160 161 __length = (length > ch ? ch : length); 162 163 if (__length < 1) 165 __length = 1; 166 167 if (_noConversionRequired) 168 return super.read(buffer, offset, __length); 169 170 if ((ch = __read()) == -1) 171 return -1; 172 173 off = offset; 174 175 do 176 { 177 buffer[offset++] = (byte)ch; 178 } 179 while (--__length > 0 && (ch = __read()) != -1); 180 181 182 return (offset - off); 183 } 184 185 186 194 public int available() throws IOException 195 { 196 return (buf.length - pos) + in.available(); 197 } 198 199 } 200 | Popular Tags |