1 16 package org.apache.commons.net.io; 17 18 import java.io.FilterInputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 22 33 34 public final class ToNetASCIIInputStream extends FilterInputStream 35 { 36 private static final int __NOTHING_SPECIAL = 0; 37 private static final int __LAST_WAS_CR = 1; 38 private static final int __LAST_WAS_NL = 2; 39 private int __status; 40 41 47 public ToNetASCIIInputStream(InputStream input) 48 { 49 super(input); 50 __status = __NOTHING_SPECIAL; 51 } 52 53 54 63 public int read() throws IOException 64 { 65 int ch; 66 67 if (__status == __LAST_WAS_NL) 68 { 69 __status = __NOTHING_SPECIAL; 70 return '\n'; 71 } 72 73 ch = in.read(); 74 75 switch (ch) 76 { 77 case '\r': 78 __status = __LAST_WAS_CR; 79 return '\r'; 80 case '\n': 81 if (__status != __LAST_WAS_CR) 82 { 83 __status = __LAST_WAS_NL; 84 return '\r'; 85 } 86 default: 88 __status = __NOTHING_SPECIAL; 89 return ch; 90 } 91 } 94 95 96 107 public int read(byte buffer[]) throws IOException 108 { 109 return read(buffer, 0, buffer.length); 110 } 111 112 113 127 public int read(byte buffer[], int offset, int length) throws IOException 128 { 129 int ch, off; 130 131 if (length < 1) 132 return 0; 133 134 ch = available(); 135 136 if (length > ch) 137 length = ch; 138 139 if (length < 1) 141 length = 1; 142 143 if ((ch = read()) == -1) 144 return -1; 145 146 off = offset; 147 148 do 149 { 150 buffer[offset++] = (byte)ch; 151 } 152 while (--length > 0 && (ch = read()) != -1); 153 154 return (offset - off); 155 } 156 157 158 public boolean markSupported() 159 { 160 return false; 161 } 162 163 public int available() throws IOException 164 { 165 int result; 166 167 result = in.available(); 168 169 if (__status == __LAST_WAS_NL) 170 return (result + 1); 171 172 return result; 173 } 174 } 175 | Popular Tags |