/*
private methods for reading to local buffer
439653
private ByteBuffer readBuffer(final byte[] content) throws IOException {
		final int startPos = getCheckBytesFrom();
		if (startPos > content.length) {
			return null;
		}

		ByteBuffer buf;
		switch (getType()) {
		case MagicMimeEntry.STRING_TYPE: {
			final int len = getContent().length();
			buf = ByteBuffer.allocate(len + 1);
			buf.put(content, startPos, len);
			break;
		}

		case MagicMimeEntry.SHORT_TYPE:
		case MagicMimeEntry.LESHORT_TYPE:
		case MagicMimeEntry.BESHORT_TYPE: {
			buf = ByteBuffer.allocate(2);
			buf.put(content, startPos, 2);
			break;
		}

		case MagicMimeEntry.LELONG_TYPE:
		case MagicMimeEntry.BELONG_TYPE: {
			buf = ByteBuffer.allocate(4);
			buf.put(content, startPos, 4);
			break;
		}

		case MagicMimeEntry.BYTE_TYPE: {
			buf = ByteBuffer.allocate(1);
			buf.put(buf.array(), startPos, 1);
		}

		default: {
			buf = null;
			break;
		}
		}
		return buf;
	}
1035603
@Override
    public Parameter[] read(ScanBuffer buffer) {
        int length = getLength(buffer);
        if (length<0) return null;
        Parameter[] result = new Parameter[length];
        for (int i = 0; i < length; i++) {
            result[i]=serializer.readObjectNotNull(buffer,Parameter.class);
        }
        return result;
    }
197254
public int read(byte []buf, int offset, int length) throws IOException
  {
    try {
      return readInt(buf, offset, length);
    } catch (IOException e) {
      _isKeepalive = false;
      throw e;
    } catch (RuntimeException e) {
      _isKeepalive = false;
      throw e;
    }
  }
459362
@Override
    public long[] read(ScanBuffer buffer) {
        int length = getLength(buffer);
        if (length<0) return null;
        return buffer.getLongs(length);
    }
1126123
@Override
  public int read(byte []buf, int offset, int length) throws IOException
  {
    try {
      SocketChannel s = _s;
      
      if (s == null) {
        return -1;
      }
      
      int remaining = _readBuffer.remaining();
      if (remaining <= 0) {
        _readBuffer.clear();
        
        if (s.read(_readBuffer) < 0) {
          _readBuffer.flip();
          return -1;
        }
        
        _readBuffer.flip();
        
        remaining = _readBuffer.remaining();
      }
      
      int sublen = Math.min(remaining, length); 

      _readBuffer.get(buf, offset, sublen);
      
      int readLength = sublen;

      if (readLength >= 0) {
        _totalReadBytes += readLength;
      }

      return readLength;
    } catch (InterruptedIOException e) {
      if (_throwReadInterrupts)
        throw e;
      
      log.log(Level.FINEST, e.toString(), e);
    } catch (IOException e) {
      if (_throwReadInterrupts) {
        throw e;
      }

      if (log.isLoggable(Level.FINEST)) {
        log.log(Level.FINEST, e.toString(), e);
      }
      else {
        log.finer(e.toString());
      }
      // server/0611
      /*
      try {
        close();
      } catch (IOException e1) {
      }
      */
    }

    return -1;
  }
7776
@Override
    public int read(byte[] buffer, int offset, int length) throws IOException {
        abortIfNeeded();
        int numBytesRead;
        // If no more bytes are available, do not read any bytes into the buffer
        if(this.virtualAvailable <= 0) {
            numBytesRead = -1;
        } else {
            // If the desired read length is greater than the number of available bytes,
            // shorten the read length to the number of available bytes.
            if(length > this.virtualAvailable) {
                // If the number of available bytes is greater than the maximum value of a 32 bit int, then
                // read as many bytes as an int can.
                length = (this.virtualAvailable < Integer.MAX_VALUE) ? (int)this.virtualAvailable : Integer.MAX_VALUE;
            }
            // Read bytes into the buffer.
            numBytesRead = this.decryptedContents.read(buffer, offset, length);
        }
        // If we were able to read bytes, decrement the number of bytes available to be read.
        if(numBytesRead != -1) {
            this.virtualAvailable -= numBytesRead;
        } else {
            // If we've reached the end of the stream, close it
            this.virtualAvailable = 0;
            close();
        }
        return numBytesRead;
    }
310732
protected int readFully(InputStream in, byte buffer[], int offset, int len)
        throws java.io.IOException {
        for (int i = 0; i < len; i++) {
            int q = in.read();
            if (q == -1)
                return ((i == 0) ? -1 : i);
            buffer[i+offset] = (byte)q;
        }
        return len;
    }
961088
function read_buffer(){
	var to_eval = jQuery('#' + command_buffer.get_id()).val();
	if( to_eval != '' ){
	    log('// Evaluating buffer...');
	    var evals = _evaluate(to_eval);
	    log('// ' + evals[1]);
	    _advance_log_to_bottom();
	}
    }
154743
@Override
    public byte[] read(ScanBuffer buffer) {
        int length = getLength(buffer);
        if (length<0) return null;
        return buffer.getBytes(length);
    }
696082
@Override
  public int read(byte []buf, int offset, int length) throws IOException
  {
    try {
      if (_channel == null) {
        return -1;
      }
      
      int remaining = _readBuffer.remaining();
      
      if (remaining > 0) {
        _readBuffer.get(buf, offset, remaining);
      
        return remaining;
      }
      
      _readBuffer.clear();
      
      int channelRead = _channel.read(_readBuffer);
      _readBuffer.flip();
      
      if (channelRead < 0)
        return -1;
      
      _readBuffer.get(buf, offset, channelRead);
      
      return channelRead;
    } catch (InterruptedIOException e) {
      if (_throwReadInterrupts)
        throw e;
      
      log.log(Level.FINEST, e.toString(), e);
    } catch (IOException e) {
      if (_throwReadInterrupts)
        throw e;

      log.log(Level.FINER, e.toString(), e);
      // server/0611
      /*
      try {
        close();
      } catch (IOException e1) {
      }
      */
    }

    return -1;
  }
1219257
public int read(long position, byte[] buffer, int offset, int length,
      ReadOptions options)
    throws IOException {      
    // sanity checks
    dfsClient.checkOpen();
    if (closed) {
      throw new IOException("Stream closed");
    }
    DFSClient.dfsInputStreamfailures.set(0);
			long start = System.currentTimeMillis();
    long filelen = getFileLength();
    if ((position < 0) || (position >= filelen)) {
      return -1;
    }
    int realLen = length;
    if ((position + length) > filelen) {
      realLen = (int)(filelen - position);
    }
    
    DFSReadProfilingData pData = DFSClient.getDFSReadProfilingData();

    // determine the block and byte range within the block
    // corresponding to position and realLen
    List<LocatedBlock> blockRange = getBlockRange(position, realLen);
    
    if (!tryPreadFromLocal(blockRange, position, buffer, offset, length,
        realLen, start)) {
      // non-local or multiple block read.
      int remaining = realLen;
      for (LocatedBlock blk : blockRange) {
        long targetStart = position - blk.getStartOffset();
        long bytesToRead = Math.min(remaining, blk.getBlockSize() - targetStart);
        if (dfsClient.allowParallelReads && dfsClient.parallelReadsThreadPool != null) {
          fetchBlockByteRangeSpeculative(blk, targetStart,
                              targetStart + bytesToRead - 1, buffer, offset,
                              options);
        } else {
          if (pData != null) {
            cliData = new FSClientReadProfilingData();
            pData.addDFSClientReadProfilingData(cliData);
            cliData.startRead();
          }
          
          fetchBlockByteRange(blk, targetStart,
                              targetStart + bytesToRead - 1, buffer, offset,
                              options);
          
          if (pData != null) {
            cliData.endRead();
          }
        }
        remaining -= bytesToRead;
        position += bytesToRead;
        offset += bytesToRead;
      }
      assert remaining == 0 : "Wrong number of bytes read.";
    }
    
    if (dfsClient.stats != null) {
      dfsClient.stats.incrementBytesRead(realLen);
    }
    long timeval = System.currentTimeMillis() - start;
    dfsClient.metrics.incPreadTime(timeval);
    dfsClient.metrics.incPreadSize(realLen);
    dfsClient.metrics.incPreadOps();
    return realLen;
  }
358749
@Override
  public int read(byte []buf, int offset, int length) throws IOException
  {
    TempBuffer cursor = _cursor;
    
    if (cursor == null)
      return -1;

    int sublen = Math.min(length, cursor.length() - _offset);

    System.arraycopy(cursor.buffer(), _offset, buf, offset, sublen);

    if (cursor.length() <= _offset + sublen) {
      _cursor = cursor.next();

      if (_freeWhenDone) {
        cursor.next(null);
        TempBuffer.free(cursor);
        cursor = null;
      }
      _offset = 0;
    }
    else
      _offset += sublen;

    return sublen;
  }
1254459
@Override
    public String[] read(ScanBuffer buffer) {
        int length = getLength(buffer);
        if (length<0) return null;
        String[] result = new String[length];
        for (int i = 0; i < length; i++) {
            result[i]=stringSerializer.read(buffer);
        }
        return result;
    }
748995
@Override
    public int read(char[] cbuf, int start, int len) throws IOException
    {
        // Let's first ensure there's enough room...
        if (start < 0 || (start+len) > cbuf.length) {
            reportBounds(cbuf, start, len);
        }
        // Already EOF?
        if (mByteBuffer == null) {
            return -1;
        }
        if (len < 1) {
            return 0;
        }

        len += start;
        int outPtr = start;

        // Ok, first; do we have a surrogate from last round?
        if (mSurrogate != NULL_CHAR) {
            cbuf[outPtr++] = mSurrogate;
            mSurrogate = NULL_CHAR;
            // No need to load more, already got one char
        } else {
            /* Note: we'll try to avoid blocking as much as possible. As a
             * result, we only need to get 4 bytes for a full char.
             */
            int left = (mByteBufferEnd - mBytePtr);
            if (left < 4) {
                if (!loadMore(left)) { // (legal) EOF?
                    return -1;
                }
            }
        }

        byte[] buf = mByteBuffer;

        main_loop:
        while (outPtr < len) {
            int ptr = mBytePtr;
            int ch;

            if (mBigEndian) {
                ch = (buf[ptr] << 24) | ((buf[ptr+1] & 0xFF) << 16)
                    | ((buf[ptr+2] & 0xFF) << 8) | (buf[ptr+3] & 0xFF);
            } else {
                ch = (buf[ptr] & 0xFF) | ((buf[ptr+1] & 0xFF) << 8)
                    | ((buf[ptr+2] & 0xFF) << 16) | (buf[ptr+3] << 24);
            }
            mBytePtr += 4;

            // Does it need to be split to surrogates?
            // (also, we can and need to verify illegal chars)
            if (ch >= 0x7F) {
                if (ch <= 0x9F) {
                    if (mXml11) { // high-order ctrl char detection...
                        if (ch != 0x85) {
                            reportInvalid(ch, outPtr-start, "(can only be included via entity in xml 1.1)");
                        }
                        ch = CONVERT_NEL_TO;
                    }
                } else if (ch >= 0xD800) {
                    // Illegal?
                    if (ch > XmlConsts.MAX_UNICODE_CHAR) {
                        reportInvalid(ch, outPtr-start,
                                      "(above "+Integer.toHexString(XmlConsts.MAX_UNICODE_CHAR)+") ");
                    }
                    if (ch > 0xFFFF) { // need to split into surrogates?
                        ch -= 0x10000; // to normalize it starting with 0x0
                        cbuf[outPtr++] = (char) (0xD800 + (ch >> 10));
                        // hmmh. can this ever be 0? (not legal, at least?)
                        ch = (0xDC00 | (ch & 0x03FF));
                        // Room for second part?
                        if (outPtr >= len) { // nope
                            mSurrogate = (char) ch;
                            break main_loop;
                        }
                    } else { // in 16-bit range... just need validity checks
                        if (ch < 0xE000) {
                            reportInvalid(ch, outPtr-start, "(a surrogate char) ");
                        } else if (ch >= 0xFFFE) {
                            reportInvalid(ch, outPtr-start, "");
                        }
                    }
                } else if (ch == 0x2028 && mXml11) { // LSEP
                    ch = CONVERT_LSEP_TO;
                }
            }
            cbuf[outPtr++] = (char) ch;
            if (mBytePtr >= mByteBufferEnd) {
                break main_loop;
            }
        }

        len = outPtr - start;
        mCharCount += len;
        return len;
    }
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;
  }
430126
public int read(byte []buffer, int offset, int length)
    throws IOException
  {
    return _stream.read(buffer, offset, length);
  }
477034
private int read1(byte b[], int off, int len)
  throws IOException {
    int avail = count-pos;
    if( avail <= 0 ) {
      if(len>=buf.length) {
        // read a chunk to user buffer directly; avoid one copy
        int nread = readChecksumChunk(b, off, len);
        return nread;
      } else {
        // read a chunk into the local buffer
        fill();
        if( count <= 0 ) {
          return -1;
        } else {
          avail = count;
        }
      }
    }
    
    // copy content of the local buffer to the user buffer
    int cnt = (avail < len) ? avail : len;
    System.arraycopy(buf, pos, b, off, cnt);
    pos += cnt;
    return cnt;    
  }