FileDocCategorySizeDatePackage
ChunkedInputStream.javaAPI DocApache Axis 1.45728Sat Apr 22 18:57:28 BST 2006org.apache.axis.transport.http

ChunkedInputStream

public class ChunkedInputStream extends FilterInputStream
author
Rick Rineholt

Fields Summary
protected long
chunkSize
protected volatile boolean
closed
private static final int
maxCharLong
private byte[]
buf
Constructors Summary
private ChunkedInputStream()


       
        super(null);
    
public ChunkedInputStream(InputStream is)

        super(is);
    
Methods Summary
public intavailable()

        if (closed) {
            return 0;
        }
        int rc = (int) Math.min(chunkSize, Integer.MAX_VALUE);

        return  Math.min(rc, in.available());
    
public voidclose()

   
        synchronized (this) {
            if (closed) {
                return;
            }
            closed = true;
        }

        byte[] b = new byte[1024];
        int bread = -1;

        do {
            bread = read(b, 0, b.length);
        }
        while (bread != -1);
    
protected longgetChunked()

        int bufsz = 0;
        chunkSize = -1L; 
        int c = -1;

        do {
            c = in.read();
            if (c > -1) {
                if (c != '\r" && c != '\n" && c != ' " && c != '\t") {
                    buf[bufsz++] = ((byte) c);
                }
            }
        }
        while (c > -1 && (c != '\n" || bufsz == 0) && bufsz < buf.length);
        if (c < 0) {
            closed = true;
        }
        String sbuf = new String(buf, 0, bufsz);

        if (bufsz > maxCharLong) {
            closed = true;
            throw new IOException("Chunked input stream failed to receive valid chunk size:" + sbuf);
        }
        try {
            chunkSize = Long.parseLong(sbuf, 16);
        } catch (NumberFormatException ne) {
            closed = true;
            throw new IOException("'" + sbuf + "' " + ne.getMessage());
        }
        if (chunkSize < 0L) {
            closed = true;
        } if (chunkSize == 0) {
            closed = true;
            // consume last \r\n tokens of 0\r\n\r\n chunk
            if (in.read() != -1) {
                in.read();
            }
        }
        if (chunkSize != 0L && c < 0) {
            //If chunk size is zero try and be tolerant that there maybe no cr or lf at the end.
            throw new IOException("HTTP Chunked stream closed in middle of chunk.");
        }
        if (chunkSize < 0L) {
            throw new IOException("HTTP Chunk size received " +
                                  chunkSize + " is less than zero.");
        }
        return chunkSize;                  
    
public booleanmarkSupported()

        return false;
    
public synchronized intread()

        if (closed) {
            return -1;
        }
        try {
            if (chunkSize < 1L) {
                if (0l == getChunked()) {
                    return -1;
                }
            }
            int rc = in.read();
            if (rc > 0) {
                chunkSize--;
            }
            return rc;
        } catch (IOException e) {
            closed = true;
            throw e;
        }
    
public intread(byte[] b)

        return read(b, 0, b.length);
    
public synchronized intread(byte[] b, int off, int len)

        if (closed) {
            return -1;
        }

        int totalread = 0;
        int bytesread = 0; 

        try {
            do {
                if (chunkSize < 1L) {
                    if (0l == getChunked()) {
                        if (totalread == 0) return -1;
                        else return totalread;
                    }
                }
                bytesread = in.read(b, off + totalread, Math.min(len - totalread,
                                (int) Math.min(chunkSize, Integer.MAX_VALUE)));
                if (bytesread > 0) {
                    totalread += bytesread;
                    chunkSize -= bytesread;
                }
            }
            while (len - totalread > 0 && bytesread > -1);
        } catch (IOException e) {
            closed = true;
            throw e;
        }
        return  totalread;
    
public voidreset()

        throw new IOException("Don't support marked streams");
    
public longskip(long n)

        if (closed) {
            return 0;
        }
        long skipped = 0l;
        byte[] b = new byte[1024];
        int bread = -1;

        do {
            bread = read(b, 0, b.length);
            if (bread > 0) skipped += bread;
        }
        while (bread != -1 && skipped < n);
        return skipped;