01: /*
02: * This file is part of DrFTPD, Distributed FTP Daemon.
03: *
04: * DrFTPD is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * DrFTPD is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with DrFTPD; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18: package org.drftpd.io;
19:
20: import java.io.IOException;
21: import java.io.InputStream;
22:
23: /**
24: * Not thread-safe (is any-In/OutputStream thread-safe?).
25: *
26: * @author mog
27: * @version $Id: StripAsciiInputStream.java 1513 2006-10-13 22:41:08Z tdsoul $
28: */
29: public class StripAsciiInputStream extends InputStream {
30: private InputStream _in;
31: private int peekChar = -1;
32: boolean _lastWasCarriageReturn = false;
33:
34: public StripAsciiInputStream(InputStream in) {
35: _in = in;
36: }
37:
38: public int read() throws IOException {
39: if (peekChar != -1) {
40: int ret = peekChar;
41: peekChar = -1;
42: System.err.println("return peeked " + ret);
43:
44: return ret;
45: }
46:
47: while (true) {
48: int b = _in.read();
49: System.err.println("read: " + (char) b + "(" + b + ")");
50:
51: if (b == '\r') {
52: System.err.println("read: was \\r");
53: _lastWasCarriageReturn = true;
54:
55: continue;
56: }
57:
58: if (b == '\n') {
59: System.err.println("read: was \\n");
60: }
61:
62: if (_lastWasCarriageReturn) {
63: _lastWasCarriageReturn = false;
64:
65: if (b != '\n') {
66: peekChar = b;
67: System.err.println("return \\r");
68:
69: return '\r';
70: }
71: }
72:
73: System.err.println("return " + (char) b + " (" + b + ")");
74:
75: return b;
76: }
77: }
78:
79: public void close() throws IOException {
80: _in.close();
81: }
82: }
|