Merge from mainline
[official-gcc.git] / libjava / classpath / gnu / xml / stream / CRLFReader.java
blob1d214ce52c12744d6894442ec63ad878d897be76
1 /* CRLFReader.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 package gnu.xml.stream;
40 import java.io.BufferedReader;
41 import java.io.IOException;
42 import java.io.Reader;
44 /**
45 * Filtered reader that normalizes CRLF pairs into LFs.
47 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
49 class CRLFReader
50 extends Reader
53 /**
54 * The CR octet.
56 public static final int CR = 13;
58 /**
59 * The LF octet.
61 public static final int LF = 10;
63 private boolean doReset;
65 protected Reader in;
67 /**
68 * Constructor.
70 protected CRLFReader(Reader in)
72 if (!in.markSupported())
73 in = new BufferedReader(in);
74 this.in = in;
77 public int read()
78 throws IOException
80 int c = in.read();
81 if (c == 13) // CR
83 in.mark(1);
84 int d = in.read();
85 if (d == 10) // LF
86 c = d;
87 else
88 in.reset();
90 return c;
93 public int read(char[] b)
94 throws IOException
96 return read(b, 0, b.length);
99 public int read(char[] b, int off, int len)
100 throws IOException
102 in.mark(len + 1);
103 int l = in.read(b, off, len);
104 if (l > 0)
106 int i = indexOfCRLF(b, off, l);
107 if (doReset)
109 in.reset();
110 if (i != -1)
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
116 else
117 l = in.read(b, off, len); // CR(s) but no LF
120 return l;
123 public boolean markSupported()
125 return in.markSupported();
128 public void mark(int limit)
129 throws IOException
131 in.mark(limit);
134 public void reset()
135 throws IOException
137 in.reset();
140 public long skip(long n)
141 throws IOException
143 return in.skip(n);
146 public void close()
147 throws IOException
149 in.close();
152 private int indexOfCRLF(char[] b, int off, int len)
153 throws IOException
155 doReset = false;
156 int lm1 = len - 1;
157 for (int i = off; i < len; i++)
159 if (b[i] == '\r') // CR
161 int d;
162 if (i == lm1)
164 d = in.read();
165 doReset = true;
167 else
168 d = b[i + 1];
169 if (d == '\n') // LF
171 doReset = true;
172 return i;
176 return -1;