| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.util.http; |
| 16 |
|
| 17 |
import java.io.InputStream; |
| 18 |
import java.io.IOException; |
| 19 |
|
| 20 |
public class HTTPChunkedInputStream extends InputStream { |
| 21 |
|
| 22 |
protected byte[] _buf; |
| 23 |
protected int _index; |
| 24 |
protected int _max; |
| 25 |
protected boolean _eof; |
| 26 |
protected InputStream _in; |
| 27 |
|
| 28 |
public HTTPChunkedInputStream(InputStream in) { |
| 29 |
_in = in; |
| 30 |
// initial buf size - will adjust automatically |
| 31 |
_buf = new byte[2048]; |
| 32 |
_index = 0; |
| 33 |
_max = 0; |
| 34 |
_eof = false; |
| 35 |
} |
| 36 |
|
| 37 |
/* only called when the buffer is empty */ |
| 38 |
private void readChunk() |
| 39 |
throws IOException { |
| 40 |
|
| 41 |
String line = readLine(_in).trim(); |
| 42 |
int length = Integer.parseInt(line, 16); |
| 43 |
|
| 44 |
if (length > 0) { |
| 45 |
|
| 46 |
// make sure the chunk will fit into the buffer |
| 47 |
|
| 48 |
if (length > _buf.length) { |
| 49 |
_buf = new byte[length]; |
| 50 |
} |
| 51 |
|
| 52 |
int bytesLeft = length; |
| 53 |
int reqBytes = 0; |
| 54 |
int off = 0; |
| 55 |
int read = 0; |
| 56 |
|
| 57 |
/* multiple reads might be necessary to load |
| 58 |
the entire chunk */ |
| 59 |
while (bytesLeft != 0) { |
| 60 |
reqBytes = bytesLeft; |
| 61 |
read = _in.read(_buf, off, reqBytes); |
| 62 |
if (read == -1) break; |
| 63 |
bytesLeft -= read; |
| 64 |
off += read; |
| 65 |
} |
| 66 |
|
| 67 |
_max = off; |
| 68 |
_index = 0; |
| 69 |
|
| 70 |
} else { |
| 71 |
// end of data indicated |
| 72 |
_eof = true; |
| 73 |
} |
| 74 |
|
| 75 |
_in.read(); // skip CR |
| 76 |
_in.read(); // skip LF |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Read a line of text from the given Stream and return it |
| 82 |
* as a String. Assumes lines end in CRLF. |
| 83 |
*/ |
| 84 |
private String readLine(InputStream in) throws IOException { |
| 85 |
StringBuffer buf = new StringBuffer(); |
| 86 |
int c, length = 0; |
| 87 |
|
| 88 |
while(true) { |
| 89 |
c = in.read(); |
| 90 |
if (c == -1 || c == '\n' || length > 512) { |
| 91 |
break; |
| 92 |
} else if (c == '\r') { |
| 93 |
in.read(); |
| 94 |
return buf.toString(); |
| 95 |
} else { |
| 96 |
buf.append((char)c); |
| 97 |
length++; |
| 98 |
} |
| 99 |
|
| 100 |
} |
| 101 |
return buf.toString(); |
| 102 |
} |
| 103 |
|
| 104 |
public int read(byte [] buffer, int off, int len) |
| 105 |
throws IOException { |
| 106 |
if (_eof) return -1; |
| 107 |
if (_max == _index) readChunk(); |
| 108 |
|
| 109 |
if (_index + len <= _max) { |
| 110 |
// that's easy |
| 111 |
System.arraycopy(_buf, _index, buffer, off, len); |
| 112 |
_index += len; |
| 113 |
return len; |
| 114 |
} else { |
| 115 |
int maximum = _max - _index; |
| 116 |
System.arraycopy(_buf, _index, buffer, off, maximum); |
| 117 |
_index += maximum; |
| 118 |
int read = read(buffer, off+maximum, len-maximum); |
| 119 |
if (read == -1) { |
| 120 |
return maximum; |
| 121 |
} else { |
| 122 |
return maximum + read; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
public int read() |
| 128 |
throws IOException { |
| 129 |
if (_eof) return -1; |
| 130 |
if (_max == _index) readChunk(); |
| 131 |
return _buf[_index++] & 0xff; |
| 132 |
} |
| 133 |
|
| 134 |
public int available() |
| 135 |
throws IOException { |
| 136 |
return _in.available(); |
| 137 |
} |
| 138 |
|
| 139 |
public void close() |
| 140 |
throws IOException { |
| 141 |
_in.close(); |
| 142 |
} |
| 143 |
|
| 144 |
} |