Obtains new byte if hasn't.
@param input Stream
@param data Empty or current data
@param available Indicates whether or not it should check first if there
are available bytes
@return Next or current data
@throws IOException if input.read() fails
517259
private static Opt<Integer> data(final InputStream input,
        final Opt<Integer> data, final boolean available) throws IOException {
        final Opt<Integer> ret;
        if (data.has()) {
            ret = data;
        } else if (available && input.available() <= 0) {
            ret = new Opt.Single<>(-1);
        } else {
            ret = new Opt.Single<>(input.read());
        }
        return ret;
    }
543317
@Override
    public InputStream openInputStream() throws IOException {
        if (outputStream != null) {
            return new ByteArrayInputStream(outputStream.toByteArray());
        } else {
            return new ByteArrayInputStream(new byte[0]);
        }
    }
538849
public static String read(InputStream input) throws IOException, NotFoundException {
        BufferedImage image = ImageIO.read(input);
        return read(image);
    }
879205
private boolean loadMore(int available)
        throws IOException
    {
        mByteCount += (mByteBufferEnd - available);

        // Bytes that need to be moved to the beginning of buffer?
        if (available > 0) {
	    /* 11-Nov-2008, TSa: can only move if we own the buffer; otherwise
	     *   we are stuck with the data.
	     */
            if (mBytePtr > 0 && canModifyBuffer()) {
                for (int i = 0; i < available; ++i) {
                    mByteBuffer[i] = mByteBuffer[mBytePtr+i];
                }
                mBytePtr = 0;
		mByteBufferEnd = available;
            }
        } else {
            /* Ok; here we can actually reasonably expect an EOF,
             * so let's do a separate read right away:
             */
            int count = readBytes();
            if (count < 1) {
                if (count < 0) { // -1
                    freeBuffers(); // to help GC?
                    return false;
                }
                // 0 count is no good; let's err out
                reportStrangeStream();
            }
        }

        /* Need at least 4 bytes; if we don't get that many, it's an
         * error.
         */
        while (mByteBufferEnd < 4) {
            int count = readBytesAt(mByteBufferEnd);
            if (count < 1) {
                if (count < 0) { // -1, EOF... no good!
                    freeBuffers(); // to help GC?
                    reportUnexpectedEOF(mByteBufferEnd, 4);
                }
                // 0 count is no good; let's err out
                reportStrangeStream();
            }
        }
        return true;
    }
329112
private byte[] digestWithoutSalt(InputStream data, int bufferLength) throws IOException {
		final byte[] buffer = new byte[bufferLength];
		int read;
		while ((read = data.read(buffer, 0, bufferLength)) > -1) {
			this.digest.update(buffer, 0, read);
		}
		return this.digest.digest();
	}
1242609
public InputStream openInputStream()
    throws IOException
  {
    StreamSource indirectSource = _indirectSource;
    
    if (indirectSource != null) {
      return indirectSource.openInputStream();
    }
    
    TempOutputStream out = _out;
    if (out != null) {
      return out.openInputStreamNoFree();
    }
    
    // System.err.println("OpenInputStream: fail to open input stream for " + this);
    throw new IOException(L.l("{0}: no input stream is available",
                              this));
  }
790281
public static String formatBytes (byte[] data) {
    // If the data exists, format it.
    if (data != null) {
      return formatBytes(data, 0, data.length, true);
    }
    // Otherwise, return an empty String as the overloaded method would have done.
    else {
      return "";
    }
  }
1234089
public byte readByte () throws IOException
  {
    // Buffer depleted ?
    if (m_nHead == m_nTail)
    {
      m_nHead = 0;
      // Refill.
      m_nTail = m_aIS.read (m_aBuffer, m_nHead, m_nBufSize);
      if (m_nTail == -1)
      {
        // No more data available.
        throw new IOException ("No more data is available");
      }
      if (m_aNotifier != null)
      {
        m_aNotifier.noteBytesRead (m_nTail);
      }
    }
    return m_aBuffer[m_nHead++];
  }
119713
public void open(InputStream stream) throws IOException
    {

        int size = (int) (stream.available() / UNIT_SIZE);
        _array = new int[size];

        DataInputStream in = null;
        try
        {
            in = new DataInputStream(new BufferedInputStream(
                    stream));
            for (int i = 0; i < size; ++i)
            {
                _array[i] = in.readInt();
            }
        }
        finally
        {
            if (in != null)
            {
                in.close();
            }
        }
    }
478544
private static byte[] preload(InputStream stream) throws IOException {
        byte[] buf = new byte[4096];
        ByteArrayOutputStream preloaded = new ByteArrayOutputStream();

        int read;
        while((read = stream.read(buf)) != -1) {
            preloaded.write(buf, 0, read);
        }

        return preloaded.toByteArray();
    }
1152191
@Override
    public synchronized int available() throws IOException {
        if (buffer == null || in == -1) {
            return 0;
        }
        return in <= out ? buffer.length - out + in : in - out;
    }
1257935
@Override
    public byte readByte() throws IndexOutOfBoundsException, IOException {
        final int read = internalReadBytes(1);
        if (read == -1) {
            // not sure this is really the right thing to do
            throw new IndexOutOfBoundsException();
        }
        return getByte(this.readerIndex++);
    }
613501
public final void bytes(Object source, Class sourceClass, byte[] data)
    {
        if (data == null)
            bytes(source, sourceClass, data, 0, 0);
        else
            bytes(source, sourceClass, data, 0, data.length);
    }
359265
private boolean loadMore(int available)
        throws IOException
    {
        mByteCount += (mByteBufferEnd - available);

        // Bytes that need to be moved to the beginning of buffer?
        if (available > 0) {
	    /* 11-Nov-2008, TSa: can only move if we own the buffer; otherwise
	     *   we are stuck with the data.
	     */
            if (mBytePtr > 0 && canModifyBuffer()) {
                for (int i = 0; i < available; ++i) {
                    mByteBuffer[i] = mByteBuffer[mBytePtr+i];
                }
                mBytePtr = 0;
		mByteBufferEnd = available;
            }
        } else {
            /* Ok; here we can actually reasonably expect an EOF,
             * so let's do a separate read right away:
             */
            int count = readBytes();
            if (count < 1) {
                if (count < 0) { // -1
                    freeBuffers(); // to help GC?
                    return false;
                }
                // 0 count is no good; let's err out
                reportStrangeStream();
            }
        }

        /* We now have at least one byte... and that allows us to
         * calculate exactly how many bytes we need!
         */
        @SuppressWarnings("cast")
        int c = (int) mByteBuffer[mBytePtr];
        if (c >= 0) { // single byte (ascii) char... cool, can return
            return true;
        }

        // Ok, a multi-byte char, let's check how many bytes we'll need:
        int needed;
        if ((c & 0xE0) == 0xC0) { // 2 bytes (0x0080 - 0x07FF)
            needed = 2;
        } else if ((c & 0xF0) == 0xE0) { // 3 bytes (0x0800 - 0xFFFF)
            needed = 3;
        } else if ((c & 0xF8) == 0xF0) {
            // 4 bytes; double-char BS, with surrogates and all...
            needed = 4;
        } else {
            reportInvalidInitial(c & 0xFF, 0);
            // never gets here... but compiler whines without this:
            needed = 1;
        }

        /* And then we'll just need to load up to that many bytes;
         * if an EOF is hit, that'll be an error. But we need not do
         * actual decoding here, just load enough bytes.
         */
        while ((mBytePtr + needed) > mByteBufferEnd) {
            int count = readBytesAt(mByteBufferEnd);
            if (count < 1) {
                if (count < 0) { // -1, EOF... no good!
                    freeBuffers();
                    reportUnexpectedEOF(mByteBufferEnd, needed);
                }
                // 0 count is no good; let's err out
                reportStrangeStream();
            }
        }
        return true;
    }
62174
public static byte[] calculate(final Data[] data) throws IOException {
        int bitSize = 0;
        for (final Data target : data) {
            bitSize += target.numOfBits;
        }
        int pos = 0;
        final FastBitSet bitSet = new FastBitSet(bitSize);
        for (final Data target : data) {
            int count = 0;
            final byte[] bytes = calculate(target);
            for (final byte b : bytes) {
                byte bits = b;
                for (int j = 0; j < 8; j++) {
                    bitSet.set(pos, (bits & 0x1) == 0x1);
                    pos++;
                    count++;
                    if (count >= target.numOfBits) {
                        break;
                    }
                    bits >>= 1;
                }
            }
        }
        return bitSet.toByteArray();
    }
98394
@GwtIncompatible("InputStream")
  static InputStream asInputStream(final ByteInput input) {
    checkNotNull(input);
    return new InputStream() {
      @Override
      public int read() throws IOException {
        return input.read();
      }

      @Override
      public int read(byte[] b, int off, int len) throws IOException {
        checkNotNull(b);
        checkPositionIndexes(off, off + len, b.length);
        if (len == 0) {
          return 0;
        }
        int firstByte = read();
        if (firstByte == -1) {
          return -1;
        }
        b[off] = (byte) firstByte;
        for (int dst = 1; dst < len; dst++) {
          int readByte = read();
          if (readByte == -1) {
            return dst;
          }
          b[off + dst] = (byte) readByte;
        }
        return len;
      }

      @Override
      public void close() throws IOException {
        input.close();
      }
    };
  }
430023
protected static byte[] getDataCopy(byte[] data, int size) {
        if (data == null) {
            return NO_DATA;
        }
        byte[] newData = new byte[size];
        System.arraycopy(data, 0, newData, 0, size);
        return newData;
    }