int InflaterInputStream::doReadArrayBounded()

in activemq-cpp/src/main/decaf/util/zip/InflaterInputStream.cpp [183:266]


int InflaterInputStream::doReadArrayBounded(unsigned char* buffer, int size, int offset, int length) {

    try{

        if (buffer == NULL) {
            throw NullPointerException(
                __FILE__, __LINE__, "Buffer passed was NULL.");
        }

        if (size < 0) {
            throw IndexOutOfBoundsException(
                __FILE__, __LINE__, "size parameter out of Bounds: %d.", size);
        }

        if (offset > size || offset < 0) {
            throw IndexOutOfBoundsException(
                __FILE__, __LINE__, "offset parameter out of Bounds: %d.", offset);
        }

        if (length < 0 || length > size - offset) {
            throw IndexOutOfBoundsException(
                __FILE__, __LINE__, "length parameter out of Bounds: %d.", length);
        }

        if (length == 0) {
            return 0;
        }

        if (isClosed()) {
            throw IOException(
                __FILE__, __LINE__, "Stream already closed.");
        }

        if (atEOF) {
            return -1;
        }

        do {

            if (inflater->needsInput()) {
                this->fill();
            }

            // Invariant: if reading returns -1 or throws, eof must be true.
            // It may also be true if the next read() should return -1.
            try {

                int result = inflater->inflate(buffer, size, offset, length);

                atEOF = inflater->finished();

                if (result > 0) {
                    return result;
                } else if (atEOF) {
                    return -1;
                } else if (inflater->needsDictionary()) {
                    atEOF = true;
                    return -1;
                } else if (this->length == -1) {
                    atEOF = true;
                    throw EOFException(
                        __FILE__, __LINE__, "Reached end of Input.");
                }

            } catch (DataFormatException& e) {

                atEOF = true;
                if (this->length == -1) {
                    throw EOFException(
                        __FILE__, __LINE__, "Reached end of Input." );
                }

                IOException ex(__FILE__, __LINE__, "Error from Inflater");
                ex.initCause(e.clone());
                throw ex;
            }

        } while (true);
    }
    DECAF_CATCH_RETHROW(IOException)
    DECAF_CATCH_RETHROW(IndexOutOfBoundsException)
    DECAF_CATCH_RETHROW(NullPointerException)
    DECAF_CATCHALL_THROW(IOException)
}