Imported GNU Classpath 0.20
[gcc.git] / libjava / classpath / gnu / xml / stream / CRLFReader.java
1 /* CRLFReader.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37
38 package gnu.xml.stream;
39
40 import java.io.BufferedReader;
41 import java.io.IOException;
42 import java.io.Reader;
43
44 /**
45 * Filtered reader that normalizes CRLF pairs into LFs.
46 *
47 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
48 */
49 class CRLFReader
50 extends Reader
51 {
52
53 /**
54 * The CR octet.
55 */
56 public static final int CR = 13;
57
58 /**
59 * The LF octet.
60 */
61 public static final int LF = 10;
62
63 private boolean doReset;
64
65 protected Reader in;
66
67 /**
68 * Constructor.
69 */
70 protected CRLFReader(Reader in)
71 {
72 if (!in.markSupported())
73 in = new BufferedReader(in);
74 this.in = in;
75 }
76
77 public int read()
78 throws IOException
79 {
80 int c = in.read();
81 if (c == 13) // CR
82 {
83 in.mark(1);
84 int d = in.read();
85 if (d == 10) // LF
86 c = d;
87 else
88 in.reset();
89 }
90 return c;
91 }
92
93 public int read(char[] b)
94 throws IOException
95 {
96 return read(b, 0, b.length);
97 }
98
99 public int read(char[] b, int off, int len)
100 throws IOException
101 {
102 in.mark(len + 1);
103 int l = in.read(b, off, len);
104 if (l > 0)
105 {
106 int i = indexOfCRLF(b, off, l);
107 if (doReset)
108 {
109 in.reset();
110 if (i != -1)
111 {
112 l = in.read(b, off, (i + 1) - off); // read to CR
113 in.read(); // skip LF
114 b[i] = '\n'; // fix CR as LF
115 }
116 else
117 l = in.read(b, off, len); // CR(s) but no LF
118 }
119 }
120 return l;
121 }
122
123 public boolean markSupported()
124 {
125 return in.markSupported();
126 }
127
128 public void mark(int limit)
129 throws IOException
130 {
131 in.mark(limit);
132 }
133
134 public void reset()
135 throws IOException
136 {
137 in.reset();
138 }
139
140 public long skip(long n)
141 throws IOException
142 {
143 return in.skip(n);
144 }
145
146 public void close()
147 throws IOException
148 {
149 in.close();
150 }
151
152 private int indexOfCRLF(char[] b, int off, int len)
153 throws IOException
154 {
155 doReset = false;
156 int lm1 = len - 1;
157 for (int i = off; i < len; i++)
158 {
159 if (b[i] == '\r') // CR
160 {
161 int d;
162 if (i == lm1)
163 {
164 d = in.read();
165 doReset = true;
166 }
167 else
168 d = b[i + 1];
169 if (d == '\n') // LF
170 {
171 doReset = true;
172 return i;
173 }
174 }
175 }
176 return -1;
177 }
178
179 }
180