This commit was manufactured by cvs2svn to create branch
[official-gcc.git] / libjava / gnu / java / net / CRLFInputStream.java
blob706c23447bec1771e68b46b7bde72632db79247a
1 /* CRLFInputStream.java --
2 Copyright (C) 2002, 2003, 2004 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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. */
39 package gnu.java.net;
41 import java.io.FilterInputStream;
42 import java.io.InputStream;
43 import java.io.IOException;
45 /**
46 * An input stream that filters out CR/LF pairs into LFs.
48 * @author Chris Burdess (dog@gnu.org)
50 public class CRLFInputStream
51 extends FilterInputStream
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 /**
64 * Buffer.
66 protected int buf = -1;
68 /**
69 * Buffer at time of mark.
71 protected int markBuf = -1;
73 /**
74 * Constructs a CR/LF input stream connected to the specified input
75 * stream.
77 public CRLFInputStream(InputStream in)
79 super(in);
82 /**
83 * Reads the next byte of data from this input stream.
84 * Returns -1 if the end of the stream has been reached.
85 * @exception IOException if an I/O error occurs
87 public int read()
88 throws IOException
90 int c;
91 if (buf != -1)
93 c = buf;
94 buf = -1;
95 return c;
97 else
99 c = super.read();
100 if (c == CR)
102 buf = super.read();
103 if (buf == LF)
105 c = buf;
106 buf = -1;
110 return c;
114 * Reads up to b.length bytes of data from this input stream into
115 * an array of bytes.
116 * Returns -1 if the end of the stream has been reached.
117 * @exception IOException if an I/O error occurs
119 public int read(byte[] b)
120 throws IOException
122 return read(b, 0, b.length);
126 * Reads up to len bytes of data from this input stream into an
127 * array of bytes, starting at the specified offset.
128 * Returns -1 if the end of the stream has been reached.
129 * @exception IOException if an I/O error occurs
131 public int read(byte[] b, int off, int len)
132 throws IOException
134 int shift = 0;
135 if (buf != -1)
137 // Push buf onto start of byte array
138 b[off] = (byte) buf;
139 off++;
140 len--;
141 buf = -1;
142 shift++;
144 int l = super.read(b, off, len);
145 l = removeCRLF(b, off - shift, l);
146 return l;
150 * Indicates whether this stream supports the mark and reset methods.
152 public boolean markSupported()
154 return in.markSupported();
158 * Marks the current position in this stream.
160 public void mark(int readlimit)
162 in.mark(readlimit);
163 markBuf = buf;
167 * Repositions this stream to the position at the time the mark method was
168 * called.
170 public void reset()
171 throws IOException
173 in.reset();
174 buf = markBuf;
177 private int removeCRLF(byte[] b, int off, int len)
179 int end = off + len;
180 for (int i = off; i < end; i++)
182 if (b[i] == CR)
184 if (i + 1 == end)
186 // This is the last byte, impossible to determine whether CRLF
187 buf = CR;
188 len--;
190 else if (b[i + 1] == LF)
192 // Shift left
193 end--;
194 for (int j = i; j < end; j++)
196 b[j] = b[j + 1];
198 len--;
199 end = off + len;
203 return len;