Get the remaining bytes that could be read from a file or ByteBuffer.

@return Number of remaining bytes
501902
private long getRemainingBytes() {
        if (in != null) {
            if (!useLoadBuf) {
                return in.remaining();
            }
            try {
                if (channel.isOpen()) {
                    return channelSize - channel.position() + in.remaining();
                } else {
                    return in.remaining();
                }
            } catch (Exception e) {
                log.error("Error getRemainingBytes", e);
            }
        }
        return 0;
    }
1264505
public static ByteBuffer getByteBufferFromInputStreamAndCloseStream(InputStream is) throws IOException {
        try {
            // is.available() may return 0, or 1, or the total number of bytes in the stream,
            // or some other number.
            // Do not try to use is.available() == 0 to find the end of the stream!
            byte[] bytes;
            int avail = is.available();
            if (avail > 32) {
                // There are more bytes available than just the ICU data header length.
                // With luck, it is the total number of bytes.
                bytes = new byte[avail];
            } else {
                bytes = new byte[128];  // empty .res files are even smaller
            }
            // Call is.read(...) until one returns a negative value.
            int length = 0;
            for(;;) {
                if (length < bytes.length) {
                    int numRead = is.read(bytes, length, bytes.length - length);
                    if (numRead < 0) {
                        break;  // end of stream
                    }
                    length += numRead;
                } else {
                    // See if we are at the end of the stream before we grow the array.
                    int nextByte = is.read();
                    if (nextByte < 0) {
                        break;
                    }
                    int capacity = 2 * bytes.length;
                    if (capacity < 128) {
                        capacity = 128;
                    } else if (capacity < 0x4000) {
                        capacity *= 2;  // Grow faster until we reach 16kB.
                    }
                    // TODO Java 6 replace new byte[] and arraycopy(): bytes = Arrays.copyOf(bytes, capacity);
                    byte[] newBytes = new byte[capacity];
                    System.arraycopy(bytes, 0, newBytes, 0, length);
                    bytes = newBytes;
                    bytes[length++] = (byte) nextByte;
                }
            }
            return ByteBuffer.wrap(bytes, 0, length);
        } finally {
            is.close();
        }
    }
1306706
public synchronized byte[] readBytes() throws IOException
    {
        final int dataLength = dataInputStream.readInt();
        final byte[] data = new byte[dataLength];

        final int dataRead = dataInputStream.read(data, 0, dataLength);
        if (dataRead != dataLength) throw new IOException("Not enough data available");

        return data;
    }
1057577
private void readMore(final int bytesRequired) throws IOException {
        if ((long) used + (long) bytesRequired > FileUtils.MAX_BUFFER_SIZE) {
            // Since buf is an array, we're limited to reading 2GB per file
            throw new IOException("File is larger than 2GB, cannot read it");
        }
        // Read INITIAL_BUFFER_CHUNK_SIZE for first chunk, or SUBSEQUENT_BUFFER_CHUNK_SIZE for subsequent chunks,
        // but don't try to read past 2GB limit
        final int targetReadSize = Math.max(bytesRequired, // 
                used == 0 ? INITIAL_BUFFER_CHUNK_SIZE : SUBSEQUENT_BUFFER_CHUNK_SIZE);
        // Calculate number of bytes to read, based on the target read size, handling integer overflow
        final int maxNewUsed = (int) Math.min((long) used + (long) targetReadSize, FileUtils.MAX_BUFFER_SIZE);
        final int bytesToRead = maxNewUsed - used;
        if (maxNewUsed > buf.length) {
            // Ran out of space, need to increase the size of the buffer
            long newBufLen = buf.length;
            while (newBufLen < maxNewUsed) {
                newBufLen <<= 1;
            }
            buf = Arrays.copyOf(buf, (int) Math.min(newBufLen, FileUtils.MAX_BUFFER_SIZE));
        }
        int extraBytesStillNotRead = bytesToRead;
        int totBytesRead = 0;
        while (extraBytesStillNotRead > 0) {
            final int bytesRead = read(used, extraBytesStillNotRead);
            if (bytesRead > 0) {
                used += bytesRead;
                totBytesRead += bytesRead;
                extraBytesStillNotRead -= bytesRead;
            } else {
                // EOF
                break;
            }
        }
        if (totBytesRead < bytesRequired) {
            throw new IOException("Premature EOF while reading classfile");
        }
    }
984300
public int getAvailable()
    throws IOException
  {
    if (_isPartDone)
      return 0;
    else if (_peekOffset < _peekLength)
      return _peekLength - _peekOffset;
    else {
      int ch = read();
      if (ch < 0)
        return 0;
      _peekOffset = 0;
      _peekLength = 1;
      _peek[0] = (byte) ch;

      return 1;
    }
  }
1283247
public final int getRemaining()
    {
        int rem = 0;
        for (int ii=0;ii<length;ii++)
        {
            rem += srcs[offset+ii].remaining();
        }
        return rem;
    }
253717
private byte[] getMMapBytes(long offset)
      throws IOException {
    //Read the first 4 bytes to get the size of the data
    ByteBuffer buf = getDataBuffer(offset);
    int maxLen = (int) Math.min(5, dataSize - offset);

    int size;
    if (buf.remaining() >= maxLen) {
      //Continuous read
      int pos = buf.position();
      size = LongPacker.unpackInt(buf);

      // Used in case of data is spread over multiple buffers
      offset += buf.position() - pos;
    } else {
      //The size of the data is spread over multiple buffers
      int len = maxLen;
      int off = 0;
      sizeBuffer.reset();
      while (len > 0) {
        buf = getDataBuffer(offset + off);
        int count = Math.min(len, buf.remaining());
        buf.get(sizeBuffer.getBuf(), off, count);
        off += count;
        len -= count;
      }
      size = LongPacker.unpackInt(sizeBuffer);
      offset += sizeBuffer.getPos();
      buf = getDataBuffer(offset);
    }

    //Create output bytes
    byte[] res = new byte[size];

    //Check if the data is one buffer
    if (buf.remaining() >= size) {
      //Continuous read
      buf.get(res, 0, size);
    } else {
      int len = size;
      int off = 0;
      while (len > 0) {
        buf = getDataBuffer(offset);
        int count = Math.min(len, buf.remaining());
        buf.get(res, off, count);
        offset += count;
        off += count;
        len -= count;
      }
    }

    return res;
  }
994728
private int getAvailableSize(Object inputStream, int expectedReadSize) throws IOException, InternalException {
    RandomAccessFile file = null;
    BufferedInputStream stream = null;
    if (inputStream instanceof RandomAccessFile) {
      file = (RandomAccessFile) inputStream;
    } else if (inputStream instanceof BufferedInputStream) {
      stream = (BufferedInputStream) inputStream;
    } else {
      throw new InternalException("Unknown input stream. This should not happen.  "
                                  + "Please report to https://github.com/minio/minio-java/issues/");
    }

    // hold current position of file/stream to reset back to this position.
    long pos = 0;
    if (file != null) {
      pos = file.getFilePointer();
    } else {
      stream.mark(expectedReadSize);
    }

    // 16KiB buffer for optimization
    byte[] buf = new byte[16384];
    int bytesToRead = buf.length;
    int bytesRead = 0;
    int totalBytesRead = 0;
    while (totalBytesRead < expectedReadSize) {
      if ((expectedReadSize - totalBytesRead) < bytesToRead) {
        bytesToRead = expectedReadSize - totalBytesRead;
      }

      if (file != null) {
        bytesRead = file.read(buf, 0, bytesToRead);
      } else {
        bytesRead = stream.read(buf, 0, bytesToRead);
      }

      if (bytesRead < 0) {
        // reached EOF
        break;
      }

      totalBytesRead += bytesRead;
    }

    // reset back to saved position.
    if (file != null) {
      file.seek(pos);
    } else {
      stream.reset();
    }

    return totalBytesRead;
  }
1010362
public ByteBuffer getBuffer() {
        assert(isDirect == false);
        assert(buffer.b().hasArray());
        assert(!buffer.b().isDirect());
        buffer.b().flip();
        return buffer.b().asReadOnlyBuffer();
    }
134780
private int ensureBuffer(int offset, int length) throws IOException {
		final int lastPos = offset + length - 1;
		final int desiredSize = ((lastPos / BUFFER_SIZE) + 1) * BUFFER_SIZE;
		final int currentSize = this.buffer.length;

		if (desiredSize > currentSize) {
			final byte[] readBuffer = new byte[desiredSize - currentSize];
			final int count = this.in.read(readBuffer);

			if (count > 0) {
				final byte[] newBuffer = new byte[currentSize + count];
				System.arraycopy(this.buffer, 0, newBuffer, 0, currentSize);
				System.arraycopy(readBuffer, 0, newBuffer, currentSize, count);
				this.buffer = newBuffer;
			}
			return (lastPos < this.buffer.length) ? length : length - (lastPos - this.buffer.length + 1);
		}
		return length;
	}
1186053
public byte[] getBytes()
    {
        int length = _eob - _start;
        byte[] copy = new byte[length];
        System.arraycopy(_bytes, _start, copy, 0, length);
        return copy;
    }
249545
@Nonnull
  public ByteBuffer getByteBuffer() {
    if (base instanceof byte[] && offset >= BYTE_ARRAY_OFFSET) {
      final byte[] bytes = (byte[]) base;

      // the offset includes an object header... this is only needed for unsafe copies
      final long arrayOffset = offset - BYTE_ARRAY_OFFSET;

      // verify that the offset and length points somewhere inside the byte array
      // and that the offset can safely be truncated to a 32-bit integer
      if ((long) bytes.length < arrayOffset + numBytes) {
        throw new ArrayIndexOutOfBoundsException();
      }

      return ByteBuffer.wrap(bytes, (int) arrayOffset, numBytes);
    } else {
      return ByteBuffer.wrap(getBytes());
    }
  }
1340732
@Override
    public Number readNumber() {
        if (buf.hasRemaining()) {
            int remaining = buf.remaining();
            if (log.isTraceEnabled()) {
                log.trace("Remaining bytes for Number: {}", remaining);
            }
            Number v;
            if (currentDataType == AMF3.TYPE_NUMBER) {
                // prevent buffer underrun if we dont have 8 bytes for the expected double
                if (remaining >= 8) {
                    v = buf.getDouble();
                } else {
                    v = buf.getInt();
                }
            } else {
                // we are decoding an int
                v = readInteger();
            }
            if (log.isTraceEnabled()) {
                log.trace("readNumber - value: {}", v);
            }
            return v;
        } else {
            log.warn("No remaining bytes for buffer readNumber");
        }
        return null;
    }
60654
public int dataAvailable() {
        if (readBuffer.position() < fillBuffer.position()) {
            return readBuffer.remaining();
        } else if (readBuffer.position() > fillBuffer.position()) {
            return capacity - fillBuffer.remaining();
        } else {
            if (readBuffer.hasRemaining()) {
                return readBuffer.remaining() + fillBuffer.position();
            } else {
                return 0;
            }
        }
    }
1326175
protected byte[] byteBufferToByteArray() {
        if (byteBuffer.hasArray()) {
            return byteBuffer.array();
        } else {
            final byte[] byteArray = new byte[byteBuffer.remaining()];
            byteBuffer.get(byteArray);
            return byteArray;
        }
    }
962127
protected final int readBytes()
        throws IOException
    {
        mBytePtr = 0;
        mByteBufferEnd = 0;
        if (mIn != null) {
            int count = mIn.read(mByteBuffer, 0, mByteBuffer.length);
            if (count > 0) {
                mByteBufferEnd = count;
            }
            return count;
        }
        return -1;
    }
1026783
public int available() throws IOException {
        checkForClosed();
        return (int) Math.min(_file.length() - _offset + bytesLeftInBuffer(), Integer.MAX_VALUE);
    }