Frames | No Frames |
1: /* ChunkedInputStream.java -- 2: Copyright (C) 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.protocol.http; 40: 41: import java.io.FilterInputStream; 42: import java.io.IOException; 43: import java.io.InputStream; 44: import java.net.ProtocolException; 45: 46: /** 47: * Input stream wrapper for the "chunked" transfer-coding. 48: * 49: * @author Chris Burdess (dog@gnu.org) 50: */ 51: public class ChunkedInputStream 52: extends FilterInputStream 53: { 54: 55: private static final byte CR = 0x0d; 56: private static final byte LF = 0x0a; 57: 58: /** Size of the chunk we're reading. */ 59: int size; 60: /** Number of bytes we've read in this chunk. */ 61: int count; 62: /** 63: * True when we should read meta-information, false when we should 64: * read data. 65: */ 66: boolean meta; 67: /** True when we've hit EOF. */ 68: boolean eof; 69: Headers headers; 70: 71: /** 72: * Constructor. 73: * @param in the response socket input stream 74: * @param headers the headers to receive additional header lines 75: */ 76: public ChunkedInputStream(InputStream in, Headers headers) 77: { 78: super(in); 79: this.headers = headers; 80: size = -1; 81: count = 0; 82: meta = true; 83: } 84: 85: public int read() 86: throws IOException 87: { 88: byte[] buf = new byte[1]; 89: int len = read(buf, 0, 1); 90: if (len == -1) 91: { 92: return -1; 93: } 94: int ret = (int) buf[0]; 95: if (ret < 0) 96: { 97: ret += 0x100; 98: } 99: return ret; 100: } 101: 102: public int read(byte[] buffer) 103: throws IOException 104: { 105: return read(buffer, 0, buffer.length); 106: } 107: 108: public int read(byte[] buffer, int offset, int length) 109: throws IOException 110: { 111: if (eof) 112: { 113: return -1; 114: } 115: if (meta) 116: { 117: // Read chunk header 118: int c, last = 0; 119: boolean seenSemi = false; 120: StringBuilder buf = new StringBuilder(); 121: do 122: { 123: c = in.read(); 124: if (c == 0x3b) // ; 125: { 126: seenSemi = true; 127: } 128: else if (c == 0x0a && last == 0x0d) // CRLF 129: { 130: size = Integer.parseInt(buf.toString(), 16); 131: break; 132: } 133: else if (!seenSemi && c >= 0x30) 134: { 135: buf.append ((char) c); 136: } 137: last = c; 138: } 139: while(c != -1); 140: count = 0; 141: meta = false; 142: } 143: if (size == 0) 144: { 145: // Read trailer 146: headers.parse(in); 147: eof = true; 148: return -1; 149: } 150: else 151: { 152: int canRead = Math.min(size - count, length); 153: int len = in.read(buffer, offset, canRead); 154: if (len == -1) 155: { 156: // This is an error condition but it isn't clear what we 157: // should do with it. 158: eof = true; 159: return -1; 160: } 161: count += len; 162: if (count == size) 163: { 164: // Read CRLF 165: int c1 = in.read(); 166: int c2 = in.read(); 167: if (c1 == -1 || c2 == -1) 168: { 169: // EOF before CRLF: bad, but ignore 170: eof = true; 171: return -1; 172: } 173: if (c1 != 0x0d || c2 != 0x0a) 174: { 175: throw new ProtocolException("expecting CRLF: " + c1 + "," + c2); 176: } 177: meta = true; 178: } 179: return len; 180: } 181: } 182: 183: }