io.vertx.core.buffer.Buffer.length()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(6.9k)|赞(0)|评价(0)|浏览(203)

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

Buffer.length介绍

[英]Returns the length of the buffer, measured in bytes. All positions are indexed from zero.
[中]返回缓冲区的长度,以字节为单位。所有位置都从零开始索引。

代码示例

代码示例来源:origin: eclipse-vertx/vert.x

@Override
 public void handle(Object event) {
  if (event instanceof ByteBuf) {
   ByteBuf byteBuf = (ByteBuf) event;
   byteBuf = VertxHandler.safeBuffer(byteBuf, allocator);
   Buffer data = Buffer.buffer(byteBuf);
   reportBytesRead(data.length());
   dataHandler.handle(data);
  }
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

private synchronized void doRead(ByteBuffer bb) {
 Buffer buff = Buffer.buffer(readBufferSize);
 int readSize = getReadSize();
 doRead(buff, 0, bb, readPos, readSize, ar -> {
  if (!ar.succeeded()) {
   handleException(ar.cause());
   return;
  }
  Buffer buffer = ar.result();
  if (buffer.length() == 0) {
   // Empty buffer represents end of file
   handleEnd();
   return;
  }
  readPos += buffer.length();
  readLength -= buffer.length();
  if (queue.write(buffer)) {
   doRead(bb);
  }
 });
}

代码示例来源:origin: eclipse-vertx/vert.x

static Http2Settings decodeSettings(String base64Settings) {
 try {
  Http2Settings settings = new Http2Settings();
  Buffer buffer = Buffer.buffer(Base64.getUrlDecoder().decode(base64Settings));
  int pos = 0;
  int len = buffer.length();
  while (pos < len) {
   int i = buffer.getUnsignedShort(pos);
   pos += 2;
   long j = buffer.getUnsignedInt(pos);
   pos += 4;
   settings.put((char)i, (Long)j);
  }
  return settings;
 } catch (Exception ignore) {
 }
 return null;
}

代码示例来源:origin: eclipse-vertx/vert.x

private Buffer loadJsonAsBuffer(URL url) {
 try {
  Buffer encoded = new JsonObject(Json.mapper.readValue(url, Map.class)).toBuffer();
  return Buffer.buffer().appendInt(encoded.length()).appendBuffer(encoded);
 } catch (IOException e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) {
 int[] consumed = { padding };
 VertxHttp2Stream req;
 synchronized (this) {
  req = streams.get(streamId);
 }
 if (req != null) {
  data = safeBuffer(data, ctx.alloc());
  Buffer buff = Buffer.buffer(data);
  context.executeFromIO(v -> {
   int len = buff.length();
   if (req.onDataRead(buff)) {
    consumed[0] += len;
   }
  });
  if (endOfStream) {
   context.executeFromIO(v -> req.onEnd());
  }
 }
 return consumed[0];
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testAppendByte2() throws Exception {
 int bytesLen = 100;
 Buffer b = Buffer.buffer(TestUtils.randomByteArray(bytesLen));
 b.setByte(b.length(), (byte) 9);
}

代码示例来源:origin: eclipse-vertx/vert.x

/**
 * Handle the request when a websocket upgrade header is present.
 */
private void handle(HttpServerRequestImpl req) {
 Buffer body = Buffer.buffer();
 boolean[] failed = new boolean[1];
 req.handler(buff -> {
  if (!failed[0]) {
   body.appendBuffer(buff);
   if (body.length() > 8192) {
    failed[0] = true;
    // Request Entity Too Large
    HttpServerResponseImpl resp = req.response();
    resp.setStatusCode(413).end();
    resp.close();
   }
  }
 });
 req.endHandler(v -> {
  if (!failed[0]) {
   handle(req, body);
  }
 });
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testCreateBuffers() throws Exception {
 Buffer buff = Buffer.buffer(1000);
 assertEquals(0, buff.length());
 String str = TestUtils.randomUnicodeString(100);
 buff = Buffer.buffer(str);
 assertEquals(buff.length(), str.getBytes("UTF-8").length);
 assertEquals(str, buff.toString());
 // TODO create with string with encoding
 byte[] bytes = TestUtils.randomByteArray(100);
 buff = Buffer.buffer(bytes);
 assertEquals(buff.length(), bytes.length);
 assertEquals(Buffer.buffer(bytes), Buffer.buffer(buff.getBytes()));
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testLength() throws Exception {
 byte[] bytes = TestUtils.randomByteArray(100);
 Buffer buffer = Buffer.buffer(bytes);
 assertEquals(100, Buffer.buffer(buffer.getByteBuf()).length());
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testLength2() throws Exception {
 byte[] bytes = TestUtils.randomByteArray(100);
 assertEquals(90, Buffer.buffer(Unpooled.copiedBuffer(bytes).slice(10, 90)).length());
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testAppendInt() {
 Buffer b = Buffer.buffer(TestUtils.randomByteArray(100));
 b.appendInt(Integer.MAX_VALUE);
 assertEquals(104, b.length());
 b.appendIntLE(Integer.MAX_VALUE);
 assertEquals(108, b.length());
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testAppendShort() {
 Buffer b = Buffer.buffer(TestUtils.randomByteArray(100));
 b.appendShort(Short.MAX_VALUE);
 assertEquals(102, b.length());
 b.appendShortLE(Short.MAX_VALUE);
 assertEquals(104, b.length());
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testAppendUnsignedShort() {
 Buffer b = Buffer.buffer(TestUtils.randomByteArray(100));
 b.appendUnsignedShort(Short.MAX_VALUE + Short.MAX_VALUE / 2);
 assertEquals(102, b.length());
 b.appendUnsignedShortLE(Short.MAX_VALUE + Short.MAX_VALUE / 2);
 assertEquals(104, b.length());
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testAppendUnsignedInt() {
 Buffer b = Buffer.buffer(TestUtils.randomByteArray(100));
 b.appendUnsignedInt(Integer.MAX_VALUE + (long) Integer.MAX_VALUE / 2);
 assertEquals(104, b.length());
 b.appendUnsignedIntLE(Integer.MAX_VALUE + (long) Integer.MAX_VALUE / 2);
 assertEquals(108, b.length());
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testAppendMedium() {
 Buffer b = Buffer.buffer(TestUtils.randomByteArray(100));
 b.appendMedium(MEDIUM_MAX_VALUE);
 assertEquals(103, b.length());
 b.appendMediumLE(MEDIUM_MAX_VALUE);
 assertEquals(106, b.length());
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testAppendLong() {
 Buffer b = Buffer.buffer(TestUtils.randomByteArray(100));
 b.appendLong(Long.MAX_VALUE);
 assertEquals(108, b.length());
 b.appendLongLE(Long.MAX_VALUE);
 assertEquals(116, b.length());
}

代码示例来源:origin: eclipse-vertx/vert.x

void testEchoStringWithEncoding(String encoding) {
 String sent = TestUtils.randomUnicodeString(100);
 Buffer buffSent = Buffer.buffer(sent, encoding);
 testEcho(sock -> sock.write(sent, encoding), buff -> assertEquals(buffSent, buff), buffSent.length());
}

代码示例来源:origin: eclipse-vertx/vert.x

private void direct(JsonObject jsonObject) throws Exception {
  Buffer buffer = Buffer.buffer();
  Buffer encoded = jsonObject.toBuffer();
  buffer.appendInt(encoded.length());
  buffer.appendBuffer(encoded);
  consume(buffer);
 }
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testAppendUnsignedByte() {
 Buffer b = Buffer.buffer(TestUtils.randomByteArray(100));
 b.appendUnsignedByte((short) (Byte.MAX_VALUE + Byte.MAX_VALUE / 2));
 assertEquals(101, b.length());
}

代码示例来源:origin: eclipse-vertx/vert.x

private void testSetBufferWithOffsetAndLen(Function<byte[], Buffer> bufferFactory) throws Exception {
 int bytesLen = 100;
 byte[] bytes = TestUtils.randomByteArray(bytesLen);
 Buffer src = bufferFactory.apply(bytes);
 int len = bytesLen - 2;
 Buffer b = Buffer.buffer();
 b.setByte(0, (byte) '0');
 b.setBuffer(1, src, 1, len);
 assertEquals(b.length(), len + 1);
 byte[] copy = new byte[len];
 System.arraycopy(bytes, 1, copy, 0, len);
 assertTrue(TestUtils.byteArraysEqual(copy, b.getBytes(1, b.length())));
 b.setBuffer(b.length(), src, 1, len);
 assertEquals(b.length(), 2 * len + 1);
 assertNullPointerException(() -> b.setBuffer(1, null));
 assertNullPointerException(() -> b.setBuffer(1, null, 0, len));
}

相关文章