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

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

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

Buffer.appendBuffer介绍

[英]Appends the specified Buffer to the end of this Buffer. The buffer will expand as necessary to accommodate any bytes written.

Returns a reference to this so multiple operations can be appended together.
[中]将指定的缓冲区追加到此缓冲区的末尾。缓冲区将根据需要扩展以容纳写入的任何字节。
返回对此的引用,以便可以将多个操作附加在一起。

代码示例

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

@Override
public void handle(Buffer event) {
 body().appendBuffer(event);
}

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

@Override
public void writeToBuffer(Buffer buff) {
 buff.appendInt(this.length());
 buff.appendBuffer(this);
}

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

@Override
public void encodeToWire(Buffer buffer, Buffer b) {
 buffer.appendInt(b.length());
 buffer.appendBuffer(b);
}

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

/**
 * This method is called to provide the parser with data.
 *
 * @param buffer  a chunk of data
 */
public void handle(Buffer buffer) {
 if (buff.length() == 0) {
  buff = buffer;
 } else {
  buff.appendBuffer(buffer);
 }
 handleParsing();
 if (buff != null && maxRecordSize > 0 && buff.length() > maxRecordSize) {
  IllegalStateException ex = new IllegalStateException("The current record is too long");
  if (exceptionHandler != null) {
   exceptionHandler.handle(ex);
  } else {
   throw ex;
  }
 }
}

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

@Override
public void encodeToWire(Buffer buffer, JsonArray jsonArray) {
 Buffer encoded = jsonArray.toBuffer();
 buffer.appendInt(encoded.length());
 buffer.appendBuffer(encoded);
}

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

@Override
public void encodeToWire(Buffer buffer, JsonObject jsonObject) {
 Buffer encoded = jsonObject.toBuffer();
 buffer.appendInt(encoded.length());
 buffer.appendBuffer(encoded);
}

代码示例来源: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

private void viaString(JsonObject jsonObject) throws Exception {
 Buffer buffer = Buffer.buffer();
 String strJson = jsonObject.encode();
 byte[] encoded = strJson.getBytes(CharsetUtil.UTF_8);
 buffer.appendInt(encoded.length);
 Buffer buff = Buffer.buffer(encoded);
 buffer.appendBuffer(buff);
 consume(buffer);
}

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

@Test
public void testAppendDoesNotModifyByteBufIndex() throws Exception {
 ByteBuf buf = Unpooled.copiedBuffer("foobar".getBytes());
 assertEquals(0, buf.readerIndex());
 assertEquals(6, buf.writerIndex());
 Buffer buffer = Buffer.buffer(buf);
 Buffer other = Buffer.buffer("prefix");
 other.appendBuffer(buffer);
 assertEquals(0, buf.readerIndex());
 assertEquals(6, buf.writerIndex());
 assertEquals(other.toString(), "prefixfoobar");
}

代码示例来源: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

public void testAppendBuff(Function<byte[], Buffer> bufferFactory) throws Exception {
 int bytesLen = 100;
 byte[] bytes = TestUtils.randomByteArray(bytesLen);
 Buffer toAppend = bufferFactory.apply(bytes);
 Buffer b = Buffer.buffer();
 b.appendBuffer(toAppend);
 assertEquals(b.length(), bytes.length);
 assertTrue(TestUtils.byteArraysEqual(bytes, b.getBytes()));
 b.appendBuffer(toAppend);
 assertEquals(b.length(), 2 * bytes.length);
 assertNullPointerException(() -> b.appendBuffer(null));
}

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

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

代码示例来源: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

private void delimited(Buffer delim) {
 int lines = 50;
 Buffer[] expected = new Buffer[lines];
 //We create lines of length zero to <lines> and shuffle them
 List<Buffer> lineList = generateLines(lines, true, delim.getByte(0));
 expected = lineList.toArray(expected);
 int totLength = lines * (lines - 1) / 2; // The sum of 0...(lines - 1)
 Buffer inp = Buffer.buffer(totLength + lines * delim.length());
 for (int i = 0; i < lines; i++) {
  inp.appendBuffer(expected[i]);
  inp.appendBuffer(delim);
 }
 //We then try every combination of chunk size up to twice the input string length
 for (int i = 1; i < inp.length() * 2; i++) {
  doTestDelimited(inp, delim, new Integer[]{i}, expected);
 }
 //Then we try a sequence of random chunk sizes
 List<Integer> chunkSizes = generateChunkSizes(lines);
 //Repeat a few times
 for (int i = 0; i < 10; i++) {
  Collections.shuffle(chunkSizes);
  doTestDelimited(inp, delim, chunkSizes.toArray(new Integer[]{}), expected);
 }
}

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

void testEcho(Consumer<NetSocket> writer, Consumer<Buffer> dataChecker, int length) {
 Handler<AsyncResult<NetSocket>> clientHandler = (asyncResult) -> {
  if (asyncResult.succeeded()) {
   NetSocket sock = asyncResult.result();
   Buffer buff = Buffer.buffer();
   sock.handler((buffer) -> {
    buff.appendBuffer(buffer);
    if (buff.length() == length) {
     dataChecker.accept(buff);
     testComplete();
    }
    if (buff.length() > length) {
     fail("Too many bytes received");
    }
   });
   writer.accept(sock);
  } else {
   fail("failed to connect");
  }
 };
 startEchoServer(testAddress, s -> client.connect(testAddress, clientHandler));
 await();
}

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

@Test
public void testWriteSameBufferMoreThanOnce() throws Exception {
 server.connectHandler(socket -> {
  Buffer received = Buffer.buffer();
  socket.handler(buff -> {
   received.appendBuffer(buff);
   if (received.toString().equals("foofoo")) {
    testComplete();
   }
  });
 }).listen(testAddress, ar -> {
  assertTrue(ar.succeeded());
  client.connect(testAddress, result -> {
   NetSocket socket = result.result();
   Buffer buff = Buffer.buffer("foo");
   socket.write(buff);
   socket.write(buff);
  });
 });
 await();
}

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

private void testResponseBody(String expected) throws Exception {
 server.requestHandler(req -> {
  HttpServerResponse resp = req.response();
  resp.end(expected);
 });
 startServer();
 client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onSuccess(resp -> {
  AtomicInteger count = new AtomicInteger();
  Buffer content = Buffer.buffer();
  resp.handler(buff -> {
   content.appendBuffer(buff);
   count.incrementAndGet();
  });
  resp.endHandler(v -> {
   assertTrue(count.get() > 0);
   assertEquals(expected, content.toString());
   testComplete();
  });
 }))
   .exceptionHandler(err -> fail())
   .end();
 await();
}

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

private void testPost(String expected) throws Exception {
 Buffer content = Buffer.buffer();
 AtomicInteger count = new AtomicInteger();
 server.requestHandler(req -> {
  assertEquals(HttpMethod.POST, req.method());
  req.handler(buff -> {
   content.appendBuffer(buff);
   count.getAndIncrement();
  });
  req.endHandler(v -> {
   assertTrue(count.get() > 0);
   req.response().end();
  });
 });
 startServer();
 client.post(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", onSuccess(resp -> {
  resp.endHandler(v -> {
   assertEquals(expected, content.toString());
   testComplete();
  });
 })).exceptionHandler(err -> {
  fail();
 }).end(expected);
 await();
}

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

private void testWriteFromConnectHandler(WebsocketVersion version) throws Exception {
 String path = "/some/path";
 Buffer buff = Buffer.buffer("AAA");
 server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> {
  assertEquals(path, ws.path());
  ws.writeFrame(WebSocketFrame.binaryFrame(buff, true));
 });
 server.listen(ar -> {
  assertTrue(ar.succeeded());
  client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, ws -> {
   Buffer received = Buffer.buffer();
   ws.handler(data -> {
    received.appendBuffer(data);
    if (received.length() == buff.length()) {
     assertEquals(buff, received);
     ws.close();
     testComplete();
    }
   });
  });
 });
 await();
}

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

@Test
public void testRequestWrite() {
 int times = 3;
 Buffer chunk = TestUtils.randomBuffer(1000);
 server.requestHandler(req -> {
  req.bodyHandler(buff -> {
   Buffer expected = Buffer.buffer();
   for (int i = 0;i < times;i++) {
    expected.appendBuffer(chunk);
   }
   assertEquals(expected, buff);
   testComplete();
  });
 });
 server.listen(onSuccess(s -> {
  HttpClientRequest req = client.request(HttpMethod.POST, DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, DEFAULT_TEST_URI, noOpHandler());
  req.setChunked(true);
  int padding = 5;
  for (int i = 0;i < times;i++) {
   Buffer paddedChunk = TestUtils.leftPad(padding, chunk);
   assertEquals(paddedChunk.getByteBuf().readerIndex(), padding);
   req.write(paddedChunk);
  }
  req.end();
 }));
 await();
}

相关文章