org.littleshoot.mina.common.ByteBuffer.position()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(6.2k)|赞(0)|评价(0)|浏览(136)

本文整理了Java中org.littleshoot.mina.common.ByteBuffer.position()方法的一些代码示例,展示了ByteBuffer.position()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuffer.position()方法的具体详情如下:
包路径:org.littleshoot.mina.common.ByteBuffer
类名称:ByteBuffer
方法名:position

ByteBuffer.position介绍

暂无

代码示例

代码示例来源:origin: org.littleshoot/mina-util

/**
 * "Pushes back" a byte on to the specified buffer, by rewinding the
 * position by 1 byte
 * 
 * @param buffer
 *            The buffer to "push back" to
 */
public static void pushBack(ByteBuffer buffer)
  {
  buffer.position(buffer.position() - 1);
  }

代码示例来源:origin: org.littleshoot/mina-port

public ByteBuffer position(int newPosition) {
  buf.position(newPosition);
  return this;
}

代码示例来源:origin: org.littleshoot/mina-port

public int position() {
  return buf.position();
}

代码示例来源:origin: org.littleshoot/mina-port

/**
 * Forwards the position of this buffer as the specified <code>size</code>
 * bytes.
 */
public ByteBuffer skip(int size) {
  autoExpand(size);
  return position(position() + size);
}

代码示例来源:origin: org.littleshoot/mina-port

/**
 * Changes the capacity and limit of this buffer so this buffer get
 * the specified <tt>expectedRemaining</tt> room from the current position.
 * This method works even if you didn't set <tt>autoExpand</tt> to
 * <tt>true</tt>.
 */
public ByteBuffer expand(int expectedRemaining) {
  return expand(position(), expectedRemaining);
}

代码示例来源:origin: org.littleshoot/mina-port

/**
 * @see java.nio.Buffer#remaining()
 */
public int remaining() {
  return limit() - position();
}

代码示例来源:origin: org.littleshoot/mina-port

/**
 * Fills this buffer with <code>NUL (0x00)</code>.
 * This method does not change buffer position.
 */
public ByteBuffer fillAndReset(int size) {
  autoExpand(size);
  int pos = position();
  try {
    fill(size);
  } finally {
    position(pos);
  }
  return this;
}

代码示例来源:origin: org.littleshoot/mina-port

/**
 * Fills this buffer with the specified value.
 * This method does not change buffer position.
 */
public ByteBuffer fillAndReset(byte value, int size) {
  autoExpand(size);
  int pos = position();
  try {
    fill(value, size);
  } finally {
    position(pos);
  }
  return this;
}

代码示例来源:origin: org.littleshoot/mina-util

public DecodingState decode(ByteBuffer in, ProtocolDecoderOutput out)
    throws Exception
  {
  final int beginPos = in.position();
  final int limit = in.limit();
  for (int i = beginPos; i < limit; i++)
    {
    final byte b = in.get(i);
    if (b != m_byteToSkip)
      {
      in.position(i);
      return finishDecode();
      }
    else
      {
      }
    }
  in.position(limit);
  return this;
  }

代码示例来源:origin: org.littleshoot/mina-port

@Override
public int hashCode() {
  int h = 1;
  int p = position();
  for (int i = limit() - 1; i >= p; i--) {
    h = 31 * h + get(i);
  }
  return h;
}

代码示例来源:origin: org.littleshoot/mina-port

@Override
public String toString() {
  StringBuffer buf = new StringBuffer();
  if (isDirect()) {
    buf.append("DirectBuffer");
  } else {
    buf.append("HeapBuffer");
  }
  buf.append("[pos=");
  buf.append(position());
  buf.append(" lim=");
  buf.append(limit());
  buf.append(" cap=");
  buf.append(capacity());
  buf.append(": ");
  buf.append(getHexDump());
  buf.append(']');
  return buf.toString();
}

代码示例来源:origin: org.littleshoot/mina-port

public int compareTo(ByteBuffer that) {
  int n = this.position() + Math.min(this.remaining(), that.remaining());
  for (int i = this.position(), j = that.position(); i < n; i++, j++) {
    byte v1 = this.get(i);
    byte v2 = that.get(j);
    if (v1 == v2) {
      continue;
    }
    if (v1 < v2) {
      return -1;
    }
    return +1;
  }
  return this.remaining() - that.remaining();
}

代码示例来源:origin: org.littleshoot/mina-port

public void writeObject(Object obj) throws IOException {
  ByteBuffer buf = ByteBuffer.allocate(64, false);
  buf.setAutoExpand(true);
  buf.putObject(obj);
  int objectSize = buf.position() - 4;
  if (objectSize > maxObjectSize) {
    buf.release();
    throw new IllegalArgumentException(
        "The encoded object is too big: " + objectSize + " (> "
            + maxObjectSize + ')');
  }
  out.write(buf.array(), 0, buf.position());
  buf.release();
}

代码示例来源:origin: org.littleshoot/mina-port

public void append(ByteBuffer in) {
  if (overflowPosition != 0) {
    discard(in);
  } else if (buf.position() > maxLineLength - in.remaining()) {
      overflowPosition = buf.position();
      buf.clear();
      discard(in);
  } else {
    getBuffer().put(in);
  }
}

代码示例来源:origin: org.littleshoot/mina-port

private void discard(ByteBuffer in) {
    if (Integer.MAX_VALUE - in.remaining() < overflowPosition) {
      overflowPosition = Integer.MAX_VALUE;
    } else {
      overflowPosition += in.remaining();
    }
    in.position(in.limit());
  }
}

代码示例来源:origin: org.littleshoot/mina-port

public void encode(IoSession session, Object message,
    ProtocolEncoderOutput out) throws Exception {
  CharsetEncoder encoder = (CharsetEncoder) session.getAttribute(ENCODER);
  if (encoder == null) {
    encoder = charset.newEncoder();
    session.setAttribute(ENCODER, encoder);
  }
  String value = message.toString();
  ByteBuffer buf = ByteBuffer.allocate(value.length())
      .setAutoExpand(true);
  buf.putString(value, encoder);
  if (buf.position() > maxLineLength) {
    throw new IllegalArgumentException("Line length: " + buf.position());
  }
  buf.putString(delimiter.getValue(), encoder);
  buf.flip();
  out.write(buf);
}

代码示例来源:origin: org.littleshoot/mina-util

private static ByteBuffer createBuffer(final ByteBuffer buffer)
  {
  final ByteBuffer data = ByteBuffer.allocate(
    buffer.limit() - buffer.position());
  
  LOG.trace("Created buffer with capacity: "+data.capacity());
  data.put(buffer);
  data.rewind();
  return data;
  }

代码示例来源:origin: org.littleshoot/mina-util

private static ByteBuffer createBuffer(final ByteBuffer buffer)
  {
  // We calculate this here because the final split buffer will not
  // necessarily have a size equal to the chunk size -- it will
  // usually be smaller.
  final ByteBuffer data = ByteBuffer.allocate(
    buffer.limit() - buffer.position());
  
  data.put(buffer);
  data.flip();
  return data;
  }

代码示例来源:origin: org.littleshoot/mina-port

public void encode(IoSession session, Object message,
      ProtocolEncoderOutput out) throws Exception {
    if (!(message instanceof Serializable)) {
      throw new NotSerializableException();
    }

    ByteBuffer buf = ByteBuffer.allocate(64);
    buf.setAutoExpand(true);
    buf.putObject(message);

    int objectSize = buf.position() - 4;
    if (objectSize > maxObjectSize) {
      buf.release();
      throw new IllegalArgumentException(
          "The encoded object is too big: " + objectSize + " (> "
              + maxObjectSize + ')');
    }

    buf.flip();
    out.write(buf);
  }
}

代码示例来源:origin: org.littleshoot/mina-port

@Override
public boolean equals(Object o) {
  if (!(o instanceof ByteBuffer)) {
    return false;
  }
  ByteBuffer that = (ByteBuffer) o;
  if (this.remaining() != that.remaining()) {
    return false;
  }
  int p = this.position();
  for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--) {
    byte v1 = this.get(i);
    byte v2 = that.get(j);
    if (v1 != v2) {
      return false;
    }
  }
  return true;
}

相关文章