protected int _read()

in axis-rt-core/src/main/java/org/apache/axis/attachments/DimeDelimitedInputStream.java [204:305]


    protected int _read(byte[] b, final int off, final int len)
        throws IOException {
        if (len < 0) throw new IllegalArgumentException
                (Messages.getMessage("attach.readLengthError",
                 "" + len));

        if (off < 0) throw new IllegalArgumentException
                (Messages.getMessage("attach.readOffsetError",
                 "" + off));
        if (b == null) throw new IllegalArgumentException
                (Messages.getMessage("attach.readArrayNullError"));
        if (b.length < off + len) throw new IllegalArgumentException
                (Messages.getMessage("attach.readArraySizeError",
                        "" + b.length, "" + len, "" + off));

        if (null != streamInError) throw streamInError;

        if (0 == len) return 0; //quick.

        if(recordLength == 0 && bytesRead == 0 &&  !moreChunks){
          ++bytesRead; //odd case no data to read -- give back 0 next time -1;
          if(ME){
              finalClose();
          }
          return 0;
        }
        if (bytesRead >= recordLength && !moreChunks) {
            dataPadLength -= readPad(dataPadLength);
            if(ME){
              finalClose();
            }
            return -1;
        }

        int totalbytesread = 0;
        int bytes2read = 0;

        do {
            if (bytesRead >= recordLength && moreChunks)
              readHeader(true);

            bytes2read = (int) Math.min(recordLength - bytesRead,
                        (long) len - totalbytesread);
            bytes2read = (int) Math.min(recordLength - bytesRead,
                        (long) len - totalbytesread);
            try {
                bytes2read = is.read(b, off + totalbytesread,
                 bytes2read);
            } catch (IOException e) {
                streamInError = e;
                throw e;
            }

            if (0 < bytes2read) {
                totalbytesread += bytes2read;
                bytesRead += bytes2read;
            }

        }
        while (bytes2read > -1 && totalbytesread < len &&
            (bytesRead < recordLength || moreChunks));

        if (0 > bytes2read) {
            if (moreChunks) {
                streamInError = new IOException(Messages.getMessage(
                                "attach.DimeStreamError0"));
                throw streamInError;
            }
            if (bytesRead < recordLength) {
                streamInError = new IOException(Messages.getMessage
                            ("attach.DimeStreamError1",
                              "" + (recordLength - bytesRead)));
                throw streamInError;
            }
            if (!ME) {
                streamInError = new IOException(Messages.getMessage(
                                "attach.DimeStreamError0"));
                throw streamInError;
            }
            //in theory the last chunk of data should also have been padded, but lets be tolerant of that.
            dataPadLength = 0;

        } else if (bytesRead >= recordLength) {
            //get rid of pading.
            try {
                dataPadLength -= readPad(dataPadLength);
            } catch (IOException e) {
                //in theory the last chunk of data should also have been padded, but lets be tolerant of that.
                if (!ME) throw e;
                else {
                    dataPadLength = 0;
                    streamInError = null;
                }
            }
        }

        if (bytesRead >= recordLength && ME) {
              finalClose();
        }

        return totalbytesread >= 0 ? totalbytesread : -1;
    }