Reads a specific integer byte value (4 bytes) from the input byte buffer at the given offset.

@param buf input byte buffer
@param pos offset into the byte buffer to read
@return the int value read
434339
public static int readInt(ByteBuffer buf, int pos) {
    return (((buf.get(pos) & 0xff) << 24) | ((buf.get(pos + 1) & 0xff) << 16)
        | ((buf.get(pos + 2) & 0xff) << 8) | (buf.get(pos + 3) & 0xff));
  }
1277947
static int getSignedInt(ByteBuf buf, int offset) {
        return (buf.getByte(offset)     & 0xFF) << 24 |
               (buf.getByte(offset + 1) & 0xFF) << 16 |
               (buf.getByte(offset + 2) & 0xFF) <<  8 |
                buf.getByte(offset + 3) & 0xFF;
    }
986880
public synchronized long read(long offset, final ByteBuffer buf) {
		if (!validState) {
			throw new InvalidStateException();
		}
		try {
			int readed;
			while (true) {
				if (offset >= offsetOutputCommited) {
					if (bufOutput.position() > 0) {
						log.warn("WARN: autoflush forced");
						flushBuffer();
					}
				}
				bufInput.clear();
				readed = fcInput.position(offset).read(bufInput); // Read 1 sector
				if (readed < HEADER_LEN) { // short+int (6 bytes)
					return -1;
				}
				bufInput.flip();
				final int magicB1 = (bufInput.get() & 0xFF); 	// Header - Magic (short, 2 bytes, msb-first)
				final int magicB2 = (bufInput.get() & 0xFF); 	// Header - Magic (short, 2 bytes, lsb-last)
				if (alignBlocks && (magicB1 == MAGIC_PADDING)) {
					final int diffOffset = nextBlockBoundary(offset);
					if (diffOffset > 0) {
						// log.info("WARN: skipping " + diffOffset + "bytes to next block-boundary");
						offset += diffOffset;
						continue;
					}
				}
				final int magic = ((magicB1 << 8) | magicB2);
				if (magic != MAGIC) {
					log.error("MAGIC HEADER fake=" + Integer.toHexString(magic) + " expected="
							+ Integer.toHexString(MAGIC));
					return -1;
				}
				break;
			}
			//
			final int datalen = bufInput.getInt(); 	// Header - Data Size (int, 4 bytes)
			final int dataUnderFlow = (datalen - (readed - HEADER_LEN));
			int footer = -12345678;
			if (dataUnderFlow < 0) {
				footer = bufInput.get(datalen + HEADER_LEN); 	// Footer (byte)
			}
			bufInput.limit(Math.min(readed, datalen + HEADER_LEN));
			buf.put(bufInput);
			if (dataUnderFlow > 0) {
				buf.limit(datalen);
				int len = fcInput.read(buf);
				if (len < dataUnderFlow) {
					log.error("Unable to read payload readed=" + len + " expected=" + dataUnderFlow);
					return -1;
				}
			}
			if (dataUnderFlow >= 0) {
				// Read Footer (byte)
				bufInput.clear();
				bufInput.limit(FOOTER_LEN);
				if (fcInput.read(bufInput) < FOOTER_LEN) {
					return -1;
				}
				bufInput.flip();
				footer = bufInput.get();
			}
			if (footer != MAGIC_FOOT) {
				log.error("MAGIC FOOT fake=" + Integer.toHexString(footer) + " expected="
						+ Integer.toHexString(MAGIC_FOOT));
				return -1;
			}
			buf.flip();
			return (offset + HEADER_LEN + datalen + FOOTER_LEN);
		} catch (Exception e) {
			log.error("Exception in read(" + offset + ")", e);
		}
		return -1;
	}
1358170
public int readInt(byte []buf, int offset, int length) throws IOException
  {
    if (! _didGet) {
      getConnInput();
    }

    if (_isRequestDone) {
      return -1;
    }

    try {
      int len = length;

      if (_isChunked) {
        if (_chunkLength == 0) {
          int ch;

          for (ch = _rs.read();
               ch >= 0 && (ch == '\r' || ch == '\n' || ch == ' ');
               ch = _rs.read()) {
          }
        
          for (; ch >= 0 && ch != '\n'; ch = _rs.read()) {
            if (ch >= '0' && ch <= '9')
              _chunkLength = 16 * _chunkLength + ch - '0';
            else if (ch >= 'a' && ch <= 'f')
              _chunkLength = 16 * _chunkLength + ch - 'a' + 10;
            else if (ch >= 'A' && ch <= 'F')
              _chunkLength = 16 * _chunkLength + ch - 'A' + 10;
          }

          if (_chunkLength == 0) {
            _isRequestDone = true;
            return -1;
          }
        }
        else if (_chunkLength < 0)
          return -1;
      
        if (_chunkLength < len)
          len = _chunkLength;
      }
      else if (_contentLength < 0) {
      }
      else if (_contentLength == 0) {
        _isRequestDone = true;
        return -1;
      }
      else if (_contentLength < len)
        len = _contentLength;

      len = _rs.read(buf, offset, len);

      if (len < 0) {
      }
      else if (_isChunked)
        _chunkLength -= len;
      else if (_contentLength > 0)
        _contentLength -= len;

      return len;
    } catch (IOException e) {
      _isKeepalive = false;
      throw e;
    } catch (RuntimeException e) {
      _isKeepalive = false;
      throw e;
    }
  }
500836
public final int getUint24(final int pos) {
        final int position = origin + pos;

        if (pos + 2 >= limit || pos < 0) throw new IllegalArgumentException("limit excceed: "
                                                                            + (pos < 0 ? pos : (pos + 2)));

        byte[] buf = buffer;
        return (0xff & buf[position]) | ((0xff & buf[position + 1]) << 8) | ((0xff & buf[position + 2]) << 16);
    }
1104381
public byte getByte(long pos) {
    checkOffsetAndCount(size, pos, 1);
    for (Segment s = head; true; s = s.next) {
      int segmentByteCount = s.limit - s.pos;
      if (pos < segmentByteCount) return s.data[s.pos + (int) pos];
      pos -= segmentByteCount;
    }
  }
709875
public synchronized void returnBuf(byte[] buf) {
        if (buf == null || buf.length > mSizeLimit) {
            return;
        }
        mBuffersByLastUse.add(buf);
        int pos = Collections.binarySearch(mBuffersBySize, buf, BUF_COMPARATOR);
        if (pos < 0) {
            pos = -pos - 1;
        }
        mBuffersBySize.add(pos, buf);
        mCurrentSize += buf.length;
        trim();
    }
653508
public final int getInt16(final int pos) {
        final int position = origin + pos;

        if (pos + 1 >= limit || pos < 0) throw new IllegalArgumentException("limit excceed: "
                                                                            + (pos < 0 ? pos : (pos + 1)));

        byte[] buf = buffer;
        return (0xff & buf[position]) | ((buf[position + 1]) << 8);
    }
1109541
public final int getUint8(final int pos) {
        if (pos >= limit || pos < 0) throw new IllegalArgumentException("limit excceed: " + pos);

        return 0xff & buffer[origin + pos];
    }
203372
public void getBytes(long pos, byte[] data, long offset, long length) {
        unsafe.copyMemory(null, pos + addr, data, BYTE_ARRAY_OFFSET + offset, length);
    }
1177192
private int read(ByteBuffer buf)
    {
        int nbytes;
        try {
            nbytes = fd.read(buf);
            if (nbytes == -1) {
                errno.set(ZError.ENOTCONN);
            }
            else if (nbytes == 0) {
                if (!fd.isBlocking()) {
                    //  If not a single byte can be read from the socket in non-blocking mode
                    //  we'll get an error (this may happen during the speculative read).

                    //  Several errors are OK. When speculative read is being done we may not
                    //  be able to read a single byte from the socket. Also, SIGSTOP issued
                    //  by a debugging tool can result in EINTR error.
                    errno.set(ZError.EAGAIN);
                    nbytes = -1;
                }
            }
        }
        catch (IOException e) {
            errno.set(ZError.ENOTCONN);
            nbytes = -1;
        }

        return nbytes;
    }
469517
protected final synchronized int readData(final long startPos, final byte[] buffer, final int offset,
                                        final int len)
            throws IOException {
        seek(startPos);
        return read(buffer, offset, len);
    }
275886
public int read() throws IOException {
        logger.debug("read()");
        logger.debug("count={} pos={}", count, pos);
        return (pos < count) ? (buf[pos++] & 0xff) : (-1);
    }
1117112
public static void writeLong(byte[] buf, int pos, long v) {
    checkBoundary(buf, pos, 8);
    buf[pos++] = (byte) (0xff & (v >> 56));
    buf[pos++] = (byte) (0xff & (v >> 48));
    buf[pos++] = (byte) (0xff & (v >> 40));
    buf[pos++] = (byte) (0xff & (v >> 32));
    buf[pos++] = (byte) (0xff & (v >> 24));
    buf[pos++] = (byte) (0xff & (v >> 16));
    buf[pos++] = (byte) (0xff & (v >> 8));
    buf[pos] = (byte) (0xff & v);
  }
931991
public static void writeInt(byte[] buf, int pos, int v) {
    checkBoundary(buf, pos, 4);
    buf[pos++] = (byte) (0xff & (v >> 24));
    buf[pos++] = (byte) (0xff & (v >> 16));
    buf[pos++] = (byte) (0xff & (v >> 8));
    buf[pos] = (byte) (0xff & v);
  }
670525
@Override
    public int read(final byte[] buffer, final int bufPos, final int length)
            throws IOException {
        int i = super.read(buffer, bufPos, length);
        if ((i == length) || (i == -1))
            return i;
        int j = super.read(buffer, bufPos + i, length - i);
        if (j == -1)
            return i;
        return j + i;
    }
20612
public final long getBeUlong48(final int pos) {
        final int position = origin + pos;

        if (pos + 5 >= limit || pos < 0) throw new IllegalArgumentException("limit excceed: "
                                                                            + (pos < 0 ? pos : (pos + 5)));

        byte[] buf = buffer;
        return ((long) (0xff & buf[position + 5])) | ((long) (0xff & buf[position + 4]) << 8)
               | ((long) (0xff & buf[position + 3]) << 16) | ((long) (0xff & buf[position + 2]) << 24)
               | ((long) (0xff & buf[position + 1]) << 32) | ((long) (0xff & buf[position]) << 40);
    }