/*
This method tries to read subsequent buffers from the reader using a mark
191898
private static String readLineFromReaderWithMark(final Reader input)
            throws IOException {
        char[] cbuf = new char[charBufferSize];
        try {
            input.mark(charBufferSize);
        } catch (IOException e) {
            // this should never happen
            LOG.warning("Caught exception setting mark on supporting reader: " + e);
            // fallback
            return readLineFromReaderWithoutMark(input);
        }

        // could be changed into do..while, but then
        // we might create an additional StringBuilder
        // instance at the end of the stream
        int count = input.read(cbuf);
        if (count == EOF) // we are at the end of the input data
            return null;

        StringBuilder line = new StringBuilder(expectedLineLength);
        // now work on the buffer(s)
        int ls = lineSeparatorIndex(cbuf, count);
        while (ls == -1) {
            line.append(cbuf, 0, count);
            count = input.read(cbuf);
            if (count == EOF) {
                // we are at the end of the input data
                return line.toString();
            }
            ls = lineSeparatorIndex(cbuf, count);
        }
        line.append(cbuf, 0, ls);

        // correct ls if we have \r\n
        int skipLS = 1;
        if (ls + 1 < count) {
            // we are not at the end of the buffer
            if (cbuf[ls] == '\r' && cbuf[ls + 1] == '\n') {
                skipLS++;
            }
        } else {
            if (cbuf[ls] == '\r' && input.read() == '\n') {
                skipLS++;
            }
        }

        //reset() and skip over last linesep
        input.reset();
        input.skip(line.length() + skipLS);
        return line.toString();
    }
821871
@Override
  public byte []nextBuffer(int offset)
    throws IOException
  {
    if (offset < 0 || SIZE < offset) {
      throw new IllegalStateException(L.l("Invalid offset: " + offset));
    }
    
    if (_bufferCapacity <= SIZE
        || _bufferCapacity <= offset + _bufferSize) {
      _offset = offset;
      flushByteBuffer();

      return buffer();
    }
    else {
      _tBuf.length(offset);
      _bufferSize += offset;

      TempBuffer tempBuf = TempBuffer.create();
      _tBuf.next(tempBuf);
      _tBuf = tempBuf;

      _buffer = _tBuf.buffer();
      _offset = _startOffset;

      return _buffer;
    }
  }
724064
private void buffer() throws IOException {
        int remaining = buffer.length;
        while (remaining > 0) {
            int r = input.read(buffer, buffer.length - remaining, remaining);
            if (r <= 0) {
                // Not enough information to fill the buffer
                offset = 0;
                maxOffset = buffer.length - remaining;
                return;
            }
            remaining -= r;
        }
        maxOffset = buffer.length;
        offset = 0;
    }
60830
private final void resetTokenMark(int mark)
  {

    int qsz = m_compiler.getTokenQueueSize();

    m_processor.m_queueMark = (mark > 0)
                              ? ((mark <= qsz) ? mark - 1 : mark) : 0;

    if (m_processor.m_queueMark < qsz)
    {
      m_processor.m_token =
        (String) m_compiler.getTokenQueue().elementAt(m_processor.m_queueMark++);
      m_processor.m_tokenChar = m_processor.m_token.charAt(0);
    }
    else
    {
      m_processor.m_token = null;
      m_processor.m_tokenChar = 0;
    }
  }
116252
function Reader(buffer, offset, length, littleEndian) {

            var bufferLength = buffer.byteLength || buffer.length;

            this.offset = offset || 0;
            this.length = length || (bufferLength - this.offset);
            this.littleEndian = littleEndian || false;

            this.view = new DataView(buffer, this.offset, this.length);
        }
914211
@Override
  public int read(byte []buffer, int offset, int length)
    throws IOException
  {
    int sublen = _next.read(buffer, offset, length);
    
    if (sublen <= 0) {
      return 0;
    }
    
    _crc = Crc64.generate(_crc, buffer, offset, sublen);
    
    _length += sublen;
    
    return sublen;
  }
810743
private void readNextBlock()
            throws IOException
    {
        lastReadInputCheckpoint = input.getCheckpoint();

        int control = input.read();
        if (control == -1) {
            throw new OrcCorruptionException(input.getOrcDataSourceId(), "Read past end of buffer RLE byte");
        }

        offset = 0;

        // if byte high bit is not set, this is a repetition; otherwise it is a literal sequence
        if ((control & 0x80) == 0) {
            length = control + MIN_REPEAT_SIZE;

            // read the repeated value
            int value = input.read();
            if (value == -1) {
                throw new OrcCorruptionException(input.getOrcDataSourceId(), "Reading RLE byte got EOF");
            }

            // fill buffer with the value
            Arrays.fill(buffer, 0, length, (byte) value);
        }
        else {
            // length is 2's complement of byte
            length = 0x100 - control;

            // read the literals into the buffer
            input.readFully(buffer, 0, length);
        }
    }
757440
private static Map<String, String> readPrefixDeclaration(BufferedReader reader) throws IOException {
        Map<String, String> prefixes = new HashMap<>();
        String line;
        while (!(line = reader.readLine()).isEmpty()) {
            String[] tokens = line.split("[\t| ]+");
            prefixes.put(tokens[0], tokens[1]);
        }
        return prefixes;
    }
1098985
public int read(char []buffer, int offset, int length)
  {
    int readLength = 0;
    
    while (length > 0) {
      if (_length <= _offset) {
        if (_head == null)
          return readLength == 0 ? -1 : readLength;

        TempCharBuffer next = _head.getNext();
        if (_isFree)
          TempCharBuffer.free(_head);
        _head = next;

        if (_head == null)
          return readLength == 0 ? -1 : readLength;
      
        _buffer = _head.buffer();
        _length = _head.getLength();
        _offset = 0;
      }

      int sublen = _length - _offset;

      if (length < sublen)
        sublen = length;

      System.arraycopy(_buffer, _offset, buffer, offset, sublen);

      _offset += sublen;
      offset += sublen;
      length -= sublen;
      readLength += sublen;
    }

    return readLength;
  }
1077426
int nextIndexOf(CharSequence seq) {
        bufferUp();
        // doesn't handle scanning for surrogates
        char startChar = seq.charAt(0);
        for (int offset = bufPos; offset < bufLength; offset++) {
            // scan to first instance of startchar:
            if (startChar != charBuf[offset])
                while(++offset < bufLength && startChar != charBuf[offset]) { /* empty */ }
            int i = offset + 1;
            int last = i + seq.length()-1;
            if (offset < bufLength && last <= bufLength) {
                for (int j = 1; i < last && seq.charAt(j) == charBuf[i]; i++, j++) { /* empty */ }
                if (i == last) // found full sequence
                    return offset - bufPos;
            }
        }
        return -1;
    }
294061
protected int checkRead(int offset, int length) {
    checkOffset(offset);
    if (limit == -1) {
      if (offset + length > capacity) {
        if (capacity < maxCapacity) {
          if (this.offset + offset + length <= bytes.size()) {
            capacity = bytes.size() - this.offset;
          } else {
            capacity(calculateCapacity(offset + length));
          }
        } else {
          throw new BufferUnderflowException();
        }
      }
    } else {
      if (offset + length > limit) {
        throw new BufferUnderflowException();
      }
    }
    return offset(offset);
  }
1147856
@Override
    public int readLine(byte[] b, int off, int len) throws IOException {
        //Call into the UpgradeInputByteBufferUtil to indicate a readLine is going on and we need to block
        _inBuffer.readLineCall();
        
        int readLineReturn = super.readLine(b, off, len);
        //Call into the UpgradeInputByteBufferUtil to indicate the readLine is complete
        _inBuffer.readLineCall();
        
        return readLineReturn;
    }
138145
private boolean readNextChunkFromFile() throws IOException {
        if (readOffset >= filledUpToWhenStarted || readOffset < 0) {
            return false;
        }

        bufferedReader.clear();
        dataFile.read(readOffset, bufferedReader);
        bufferedReader.position(0);
        if (bufferedReader.limit() > 0) {
            readOffset += bufferedReader.limit();
            while (bufferedReader.remaining() > 0) {
                byte[] dst = new byte[elementSize];
                bufferedReader.get(dst);
                pendingElements.add(dst);
            }
        }
        return true;
    }
1091794
private static String readToken(Buffer buffer) {
    try {
      long tokenSize = buffer.indexOfElement(TOKEN_DELIMITERS);
      if (tokenSize == -1L) tokenSize = buffer.size();

      return tokenSize != 0L
          ? buffer.readUtf8(tokenSize)
          : null;
    } catch (EOFException e) {
      throw new AssertionError();
    }
  }
464245
private void readChunk()
	throws IOException {

	String line = readLine(_in).trim();
	int length  = Integer.parseInt(line, 16);

	if (length > 0) {

	    // make sure the chunk will fit into the buffer

	    if (length > _buf.length) {
		_buf = new byte[length];
	    }

	    int bytesLeft = length;
	    int reqBytes = 0;
	    int off = 0;
	    int read = 0;

	    /*  multiple reads might be necessary to load
		the entire chunk */
	    while (bytesLeft != 0) {
		reqBytes = bytesLeft;
		read = _in.read(_buf, off, reqBytes);
		if (read == -1) break;
		bytesLeft -= read;
		off += read;
	    }

	    _max = off;
	    _index = 0;

	} else {
	    // end of data indicated
	    _eof = true;
	}

	_in.read(); // skip CR
	_in.read(); // skip LF

    }
916779
public Object read() throws IOException {
    char[] buffer = new char[BUFFER_LENGTH];
    reader.mark(BUFFER_LENGTH);
    int count = 0;
    int textEnd;

    while (true) {
      int r = reader.read(buffer, count, buffer.length - count);

      if (r == -1) {
        // the input is exhausted; return the remaining characters
        textEnd = count;
        break;
      }

      count += r;
      int possibleMarker = findPossibleMarker(buffer, count);

      if (possibleMarker != 0) {
        // return the characters that precede the marker
        textEnd = possibleMarker;
        break;
      }

      if (count < marker.length()) {
        // the buffer contains only the prefix of a marker so we must read more
        continue;
      }

      // we've read a marker so return the value that follows
      reader.reset();
      String json = reader.readLine().substring(marker.length());
      return jsonParser.parse(json);
    }

    if (count == 0) {
      return null;
    }

    // return characters
    reader.reset();
    count = reader.read(buffer, 0, textEnd);
    return new String(buffer, 0, count);
  }
1026150
@Override
	protected synchronized void read(long offset, byte[] b) throws IOException {
		if (byteBuffer != null) {
			byteBuffer.position((int) offset);
			byteBuffer.get(b);
		} else {
			throw new IOException("Read failed, file " + getPath() + " not mapped for I/O");
		}
	}