/** * Reads a single byte from this sequence of input streams and returns it as * an integer in the range from 0 to 255. It tries to read from the current * stream first; if the end of this stream has been reached, it reads from * the next one. Blocks until one byte has been read, the end of the last * input stream in the sequence has been reached, or an exception is thrown. * * @return the byte read or -1 if either the end of the last stream in the * sequence has been reached or this input stream sequence is * closed. * @throws IOException * if an error occurs while reading the current source input * stream. */ public override int read() { // throws IOException { while (inJ != null) { int result = inJ.read(); if (result >= 0) { return(result); } nextStream(); } return(-1); }
private int read() //throws IOException { // Call the read for the appropriate stream { if (inStream == null) { return(inReader.read()); } return(inStream.read()); }
private int fillbuf(InputStream localIn, byte[] localBuf) //throws IOException { if (markpos == -1 || (pos - markpos >= marklimit)) { /* Mark position not set or exceeded readlimit */ int result = localIn.read(localBuf); if (result > 0) { markpos = -1; pos = 0; count = result == -1 ? 0 : result; } return(result); } if (markpos == 0 && marklimit > localBuf.Length) { /* Increase buffer size to accommodate the readlimit */ int newLength = localBuf.Length * 2; if (newLength > marklimit) { newLength = marklimit; } byte[] newbuf = new byte[newLength]; java.lang.SystemJ.arraycopy(localBuf, 0, newbuf, 0, localBuf.Length); // Reassign buf, which will invalidate any local references // FIXME: what if buf was null? localBuf = buf = newbuf; } else if (markpos > 0) { java.lang.SystemJ.arraycopy(localBuf, markpos, localBuf, 0, localBuf.Length - markpos); } /* Set the new position and mark position */ pos -= markpos; count = markpos = 0; int bytesread = localIn.read(localBuf, pos, localBuf.Length - pos); count = bytesread <= 0 ? pos : pos + bytesread; return(bytesread); }
/* * Reads at most {@code length} bytes from this stream and stores them in * byte array {@code buffer} starting at offset {@code offset}. Returns the * number of bytes actually read or -1 if no bytes were read and the end of * the stream was encountered. If all the buffered bytes have been used, a * mark has not been set and the requested number of bytes is larger than * the receiver's buffer size, this implementation bypasses the buffer and * simply places the results directly into {@code buffer}. * * @param buffer * the byte array in which to store the bytes read. * @param offset * the initial position in {@code buffer} to store the bytes read * from this stream. * @param length * the maximum number of bytes to store in {@code buffer}. * @return the number of bytes actually read or -1 if end of stream. * @throws IndexOutOfBoundsException * if {@code offset < 0} or {@code length < 0}, or if * {@code offset + length} is greater than the size of * {@code buffer}. * @throws IOException * if the stream is already closed or another IOException * occurs. */ public override int read(byte[] buffer, int offset, int length) //throws IOException { // Use local ref since buf may be invalidated by an unsynchronized // close() byte[] localBuf = buf; if (localBuf == null) { // luni.24=Stream is closed throw new IOException("Stream is closed"); //$NON-NLS-1$ } // avoid int overflow if (offset > buffer.Length - length || offset < 0 || length < 0) { throw new java.lang.IndexOutOfBoundsException(); } if (length == 0) { return(0); } InputStream localIn = inJ; if (localIn == null) { // luni.24=Stream is closed throw new IOException("Stream is closed"); //$NON-NLS-1$ } int required; if (pos < count) { /* There are bytes available in the buffer. */ int copylength = count - pos >= length ? length : count - pos; java.lang.SystemJ.arraycopy(localBuf, pos, buffer, offset, copylength); pos += copylength; if (copylength == length || localIn.available() == 0) { return(copylength); } offset += copylength; required = length - copylength; } else { required = length; } while (true) { int read; /* * If we're not marked and the required size is greater than the * buffer, simply read the bytes directly bypassing the buffer. */ if (markpos == -1 && required >= localBuf.Length) { read = localIn.read(buffer, offset, required); if (read == -1) { return(required == length ? -1 : length - required); } } else { if (fillbuf(localIn, localBuf) == -1) { return(required == length ? -1 : length - required); } // localBuf may have been invalidated by fillbuf if (localBuf != buf) { localBuf = buf; if (localBuf == null) { // luni.24=Stream is closed throw new IOException("Stream is closed"); //$NON-NLS-1$ } } read = count - pos >= required ? required : count - pos; java.lang.SystemJ.arraycopy(localBuf, pos, buffer, offset, read); pos += read; } required -= read; if (required == 0) { return(length); } if (localIn.available() == 0) { return(length - required); } offset += read; } }
/* * Reads at most {@code length} characters from this reader and stores them * at position {@code offset} in the character array {@code buf}. Returns * the number of characters actually read or -1 if the end of the reader has * been reached. The bytes are either obtained from converting bytes in this * reader's buffer or by first filling the buffer from the source * InputStream and then reading from the buffer. * * @param buf * the array to store the characters read. * @param offset * the initial position in {@code buf} to store the characters * read from this reader. * @param length * the maximum number of characters to read. * @return the number of characters read or -1 if the end of the reader has * been reached. * @throws IndexOutOfBoundsException * if {@code offset < 0} or {@code length < 0}, or if * {@code offset + length} is greater than the length of * {@code buf}. * @throws IOException * if this reader is closed or some other I/O error occurs. */ public override int read(char[] buf, int offset, int length) {// throws IOException { lock (lockJ) { if (!isOpen()) { // luni.BA=InputStreamReader is closed. throw new IOException("InputStreamReader is closed."); //$NON-NLS-1$ } if (offset < 0 || offset > buf.Length - length || length < 0) { throw new java.lang.IndexOutOfBoundsException(); } if (length == 0) { return(0); } java.nio.CharBuffer outJ = java.nio.CharBuffer.wrap(buf, offset, length); java.nio.charset.CoderResult result = java.nio.charset.CoderResult.UNDERFLOW; // bytes.remaining() indicates number of bytes in buffer // when 1-st time entered, it'll be equal to zero bool needInput = !bytes.hasRemaining(); while (outJ.hasRemaining()) { // fill the buffer if needed if (needInput) { try { if ((inJ.available() == 0) && (outJ.position() > offset)) { // we could return the result without blocking read break; } } catch (IOException e) { // available didn't work so just try the read } int to_read = bytes.capacity() - bytes.limit(); int off = bytes.arrayOffset() + bytes.limit(); int was_red = inJ.read((byte[])bytes.array(), off, to_read); if (was_red == -1) { endOfInput = true; break; } else if (was_red == 0) { break; } bytes.limit(bytes.limit() + was_red); needInput = false; } // decode bytes result = decoder.decode(bytes, outJ, false); if (result.isUnderflow()) { // compact the buffer if no space left if (bytes.limit() == bytes.capacity()) { bytes.compact(); bytes.limit(bytes.position()); bytes.position(0); } needInput = true; } else { break; } } if (result == java.nio.charset.CoderResult.UNDERFLOW && endOfInput) { result = decoder.decode(bytes, outJ, true); decoder.flush(outJ); decoder.reset(); } if (result.isMalformed()) { throw new java.nio.charset.MalformedInputException(result.length()); } else if (result.isUnmappable()) { throw new java.nio.charset.UnmappableCharacterException(result.length()); } return(outJ.position() - offset == 0 ? -1 : outJ.position() - offset); } }
//throws IOException private int fillbuf(InputStream localIn, byte[] localBuf) { if (markpos == -1 || (pos - markpos >= marklimit)) { /* Mark position not set or exceeded readlimit */ int result = localIn.read(localBuf); if (result > 0) { markpos = -1; pos = 0; count = result == -1 ? 0 : result; } return result; } if (markpos == 0 && marklimit > localBuf.Length) { /* Increase buffer size to accommodate the readlimit */ int newLength = localBuf.Length * 2; if (newLength > marklimit) { newLength = marklimit; } byte[] newbuf = new byte[newLength]; java.lang.SystemJ.arraycopy(localBuf, 0, newbuf, 0, localBuf.Length); // Reassign buf, which will invalidate any local references // FIXME: what if buf was null? localBuf = buf = newbuf; } else if (markpos > 0) { java.lang.SystemJ.arraycopy(localBuf, markpos, localBuf, 0, localBuf.Length - markpos); } /* Set the new position and mark position */ pos -= markpos; count = markpos = 0; int bytesread = localIn.read(localBuf, pos, localBuf.Length - pos); count = bytesread <= 0 ? pos : pos + bytesread; return bytesread; }
public override int read(byte[] buffer) { return(inJ.read(buffer)); }