1 /* CRLFInputStream.java -- 2 Copyright (C) 2002, 2003, 2004, 2006 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 39 package gnu.java.net; 40 41 import java.io.BufferedInputStream; 42 import java.io.IOException; 43 import java.io.InputStream; 44 45 /** 46 * An input stream that filters out CR/LF pairs into LFs. 47 * 48 * @author Chris Burdess (dog@gnu.org) 49 */ 50 public class CRLFInputStream 51 extends InputStream 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 /** 64 * The underlying input stream. 65 */ 66 protected InputStream in; 67 68 private boolean doReset; 69 70 /** 71 * Constructs a CR/LF input stream connected to the specified input 72 * stream. 73 */ CRLFInputStream(InputStream in)74 public CRLFInputStream(InputStream in) 75 { 76 this.in = in.markSupported() ? in : new BufferedInputStream(in); 77 } 78 79 /** 80 * Reads the next byte of data from this input stream. 81 * Returns -1 if the end of the stream has been reached. 82 * @exception IOException if an I/O error occurs 83 */ read()84 public int read() 85 throws IOException 86 { 87 int c = in.read(); 88 if (c == CR) 89 { 90 in.mark(1); 91 int d = in.read(); 92 if (d == LF) 93 { 94 c = d; 95 } 96 else 97 { 98 in.reset(); 99 } 100 } 101 return c; 102 } 103 104 /** 105 * Reads up to b.length bytes of data from this input stream into 106 * an array of bytes. 107 * Returns -1 if the end of the stream has been reached. 108 * @exception IOException if an I/O error occurs 109 */ read(byte[] b)110 public int read(byte[] b) 111 throws IOException 112 { 113 return read(b, 0, b.length); 114 } 115 116 /** 117 * Reads up to len bytes of data from this input stream into an 118 * array of bytes, starting at the specified offset. 119 * Returns -1 if the end of the stream has been reached. 120 * @exception IOException if an I/O error occurs 121 */ read(byte[] b, int off, int len)122 public int read(byte[] b, int off, int len) 123 throws IOException 124 { 125 in.mark(len + 1); 126 int l = in.read(b, off, len); 127 if (l > 0) 128 { 129 int i = indexOfCRLF(b, off, l); 130 if (doReset) 131 { 132 in.reset(); 133 if (i != -1) 134 { 135 l = in.read(b, off, (i + 1) - off); // read to CR 136 in.read(); // skip LF 137 b[i] = LF; // fix CR as LF 138 } 139 else 140 { 141 l = in.read(b, off, len); // CR(s) but no LF 142 } 143 } 144 } 145 return l; 146 } 147 indexOfCRLF(byte[] b, int off, int len)148 private int indexOfCRLF(byte[] b, int off, int len) 149 throws IOException 150 { 151 doReset = false; 152 int end = off + len; 153 int em1 = end - 1; 154 for (int i = off; i < end; i++) 155 { 156 if (b[i] == CR) 157 { 158 int d; 159 if (i == em1) 160 { 161 d = in.read(); 162 doReset = true; 163 } 164 else 165 { 166 d = b[i + 1]; 167 } 168 if (d == LF) 169 { 170 doReset = true; 171 return i; 172 } 173 } 174 } 175 return -1; 176 } 177 178 } 179