home *** CD-ROM | disk | FTP | other *** search
- package sun.net.www.http;
-
- import java.io.EOFException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.PushbackInputStream;
- import java.util.Vector;
-
- public class ChunkedInputStream extends InputStream {
- private byte[] buf;
- private byte[][] bufs;
- private int bufIndex = 0;
- private int count;
- private int pos;
- // $FF: renamed from: in java.io.InputStream
- private InputStream field_0;
- private char[] lineBuffer;
-
- public void print() {
- System.out.println("pos is " + this.pos);
- System.out.println("count is " + this.count);
- System.out.println("bufIndex is " + this.bufIndex);
- System.out.println("bufs.length is " + this.bufs.length);
-
- for(int var1 = 0; var1 < this.bufs.length; ++var1) {
- System.out.println("bufs " + var1 + " is len " + this.bufs[var1].length);
- }
-
- }
-
- private void ensureOpen() throws IOException {
- if (this.field_0 == null) {
- throw new IOException("Stream closed");
- }
- }
-
- public ChunkedInputStream(InputStream var1) throws IOException {
- this.field_0 = var1;
- this.prefill();
- }
-
- private void readFully(byte[] var1, int var2, int var3) throws IOException {
- InputStream var4 = this.field_0;
-
- int var6;
- for(int var5 = 0; var5 < var3; var5 += var6) {
- var6 = var4.read(var1, var2 + var5, var3 - var5);
- if (var6 < 0) {
- throw new EOFException();
- }
- }
-
- }
-
- private String readLine() throws IOException {
- InputStream var1 = this.field_0;
- char[] var2 = this.lineBuffer;
- if (var2 == null) {
- var2 = this.lineBuffer = new char[128];
- }
-
- int var3 = var2.length;
- int var4 = 0;
-
- while(true) {
- int var5;
- switch (var5 = var1.read()) {
- case 13:
- int var6 = var1.read();
- if (var6 != 10 && var6 != -1) {
- if (!(var1 instanceof PushbackInputStream)) {
- var1 = this.field_0 = new PushbackInputStream(var1);
- }
-
- ((PushbackInputStream)var1).unread(var6);
- }
- case -1:
- case 10:
- if (var5 == -1 && var4 == 0) {
- return null;
- }
-
- return String.copyValueOf(var2, 0, var4);
- }
-
- --var3;
- if (var3 < 0) {
- var2 = new char[var4 + 128];
- var3 = var2.length - var4 - 1;
- System.arraycopy(this.lineBuffer, 0, var2, 0, var4);
- this.lineBuffer = var2;
- }
-
- var2[var4++] = (char)var5;
- }
- }
-
- private int readChunkSize() throws IOException {
- int var1 = -1;
-
- try {
- String var2 = this.readLine();
-
- int var3;
- for(var3 = 0; var3 < var2.length(); ++var3) {
- char var4 = var2.charAt(var3);
- if (Character.digit(var4, 16) == -1) {
- break;
- }
- }
-
- var1 = Integer.parseInt(var2.substring(0, var3), 16);
- return var1;
- } catch (NumberFormatException var5) {
- throw new IOException("Bogus chunk size");
- }
- }
-
- private void prefill() throws IOException {
- Vector var1 = new Vector();
-
- while(true) {
- int var3 = this.readChunkSize();
- if (var3 == 0) {
- this.bufs = new byte[var1.size()][];
- var1.copyInto(this.bufs);
- return;
- }
-
- byte[] var2 = new byte[var3];
- this.readFully(var2, 0, var3);
- this.readCRLF();
- var1.addElement(var2);
- }
- }
-
- private void readCRLF() throws IOException {
- int var1 = this.field_0.read();
- if (var1 != 13) {
- throw new IOException("missing CRLF");
- } else {
- var1 = this.field_0.read();
- if (var1 != 10) {
- throw new IOException("missing CRLF");
- }
- }
- }
-
- private void fill() throws IOException {
- if (this.bufs.length != 0 && this.bufIndex < this.bufs.length) {
- this.pos = 0;
- this.buf = this.bufs[this.bufIndex];
- this.bufs[this.bufIndex] = null;
- ++this.bufIndex;
- this.count = this.buf.length;
- }
- }
-
- public synchronized int read() throws IOException {
- this.ensureOpen();
- if (this.pos >= this.count) {
- this.fill();
- if (this.pos >= this.count) {
- return -1;
- }
- }
-
- return this.buf[this.pos++] & 255;
- }
-
- private int read1(byte[] var1, int var2, int var3) throws IOException {
- int var4 = this.count - this.pos;
- if (var4 <= 0) {
- this.fill();
- var4 = this.count - this.pos;
- if (var4 <= 0) {
- return -1;
- }
- }
-
- int var5 = var4 < var3 ? var4 : var3;
- System.arraycopy(this.buf, this.pos, var1, var2, var5);
- this.pos += var5;
- return var5;
- }
-
- public synchronized int read(byte[] var1, int var2, int var3) throws IOException {
- this.ensureOpen();
- if (var2 >= 0 && var2 <= var1.length && var3 >= 0 && var2 + var3 <= var1.length && var2 + var3 >= 0) {
- return var3 == 0 ? 0 : this.read1(var1, var2, var3);
- } else {
- throw new IndexOutOfBoundsException();
- }
- }
-
- public synchronized int available() throws IOException {
- int var1 = 0;
- this.ensureOpen();
- var1 = this.count - this.pos;
-
- for(int var2 = this.bufIndex; var2 < this.bufs.length; ++var2) {
- var1 += this.bufs[var2].length;
- }
-
- return var1;
- }
-
- public synchronized void close() throws IOException {
- if (this.field_0 != null) {
- this.field_0.close();
- this.field_0 = null;
- this.buf = null;
- this.bufs = null;
- }
- }
- }
-