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

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

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

Buffer.slice介绍

[英]Returns a slice of this buffer. Modifying the content of the returned buffer or this buffer affects each other's content while they maintain separate indexes and marks.
[中]返回此缓冲区的一个片段。修改返回的缓冲区或此缓冲区的内容会影响彼此的内容,同时它们会维护单独的索引和标记。

代码示例

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

@Override
public JsonObject decodeFromWire(int pos, Buffer buffer) {
 int length = buffer.getInt(pos);
 pos += 4;
 return new JsonObject(buffer.slice(pos, pos + length));
}

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

@Override
public JsonArray decodeFromWire(int pos, Buffer buffer) {
 int length = buffer.getInt(pos);
 pos += 4;
 return new JsonArray(buffer.slice(pos, pos + length));
}

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

private byte[] readBytes(int len) throws VertxException {
 if (pos + len > in.length()) {
  throw new VertxException("Invalid DER: stream too short, missing tag");
 }
 Buffer s = in.slice(pos, pos + len);
 pos += len;
 return s.getBytes();
}

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

private void direct(Buffer buffer) throws Exception {
  int pos = 0;
  int length = buffer.getInt(pos);
  pos += 4;
  consume(new JsonObject(buffer.slice(pos, pos + length)));
 }
}

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

/**
 * Splits the provided buffer into multiple frames (which do not exceed the maximum web socket frame size)
 * and writes them in order to the socket.
 */
private void writePartialMessage(FrameType frameType, Buffer data, int offset) {
 int end = offset + maxWebSocketFrameSize;
 boolean isFinal;
 if (end >= data.length()) {
  end  = data.length();
  isFinal = true;
 } else {
  isFinal = false;
 }
 Buffer slice = data.slice(offset, end);
 WebSocketFrame frame;
 if (offset == 0 || !supportsContinuation) {
  frame = new WebSocketFrameImpl(frameType, slice.getByteBuf(), isFinal);
 } else {
  frame = WebSocketFrame.continuationFrame(slice, isFinal);
 }
 writeFrame(frame);
 int newOffset = offset + maxWebSocketFrameSize;
 if (!isFinal) {
  writePartialMessage(frameType, data, newOffset);
 }
}

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

@Test
public void testPartialH2CAmbiguousRequest() throws Exception {
 server.requestHandler(req -> {
  assertEquals("POST", req.rawMethod());
  testComplete();
 });
 Buffer fullRequest = Buffer.buffer("POST /whatever HTTP/1.1\r\n\r\n");
 startServer();
 NetClient client = vertx.createNetClient();
 client.connect(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(so -> {
  so.write(fullRequest.slice(0, 1));
  vertx.setTimer(1000, id -> {
   so.write(fullRequest.slice(1, fullRequest.length()));
  });
 }));
 await();
}

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

req.setChunked(true);
for (int i = 0; i < buffer.length() / 8192; i++) {
 req.write(buffer.slice(i * 8192, (i + 1) * 8192));
 Thread.sleep(0, 100);

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

request.response().setChunked(true);
for (int i = 0; i < buffer.length() / 8192; i++) {
 request.response().write(buffer.slice(i * 8192, (i + 1) * 8192));

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

@Test
public void testSlice1() throws Exception {
 Buffer buff = TestUtils.randomBuffer(100);
 Buffer sliced = buff.slice();
 assertEquals(buff, sliced);
 long rand = TestUtils.randomLong();
 sliced.setLong(0, rand);
 assertEquals(rand, buff.getLong(0));
 buff.appendString(TestUtils.randomUnicodeString(100));
 assertEquals(100, sliced.length());
}

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

@Test
public void testSlice2() throws Exception {
 Buffer buff = TestUtils.randomBuffer(100);
 Buffer sliced = buff.slice(10, 20);
 for (int i = 0; i < 10; i++) {
  assertEquals(buff.getByte(10 + i), sliced.getByte(i));
 }
 long rand = TestUtils.randomLong();
 sliced.setLong(0, rand);
 assertEquals(rand, buff.getLong(10));
 buff.appendString(TestUtils.randomUnicodeString(100));
 assertEquals(10, sliced.length());
}

代码示例来源:origin: io.vertx/vertx-core

@Override
public JsonObject decodeFromWire(int pos, Buffer buffer) {
 int length = buffer.getInt(pos);
 pos += 4;
 return new JsonObject(buffer.slice(pos, pos + length));
}

代码示例来源:origin: io.vertx/vertx-core

@Override
public JsonArray decodeFromWire(int pos, Buffer buffer) {
 int length = buffer.getInt(pos);
 pos += 4;
 return new JsonArray(buffer.slice(pos, pos + length));
}

代码示例来源:origin: io.vertx/vertx-core

private byte[] readBytes(int len) throws VertxException {
 if (pos + len > in.length()) {
  throw new VertxException("Invalid DER: stream too short, missing tag");
 }
 Buffer s = in.slice(pos, pos + len);
 pos += len;
 return s.getBytes();
}

代码示例来源:origin: io.vertx/vertx-core

private void direct(Buffer buffer) throws Exception {
  int pos = 0;
  int length = buffer.getInt(pos);
  pos += 4;
  consume(new JsonObject(buffer.slice(pos, pos + length)));
 }
}

代码示例来源:origin: io.vertx/vertx-core

/**
 * Splits the provided buffer into multiple frames (which do not exceed the maximum web socket frame size)
 * and writes them in order to the socket.
 */
private void writePartialMessage(FrameType frameType, Buffer data, int offset) {
 int end = offset + maxWebSocketFrameSize;
 boolean isFinal;
 if (end >= data.length()) {
  end  = data.length();
  isFinal = true;
 } else {
  isFinal = false;
 }
 Buffer slice = data.slice(offset, end);
 WebSocketFrame frame;
 if (offset == 0 || !supportsContinuation) {
  frame = new WebSocketFrameImpl(frameType, slice.getByteBuf(), isFinal);
 } else {
  frame = WebSocketFrame.continuationFrame(slice, isFinal);
 }
 writeFrame(frame);
 int newOffset = offset + maxWebSocketFrameSize;
 if (!isFinal) {
  writePartialMessage(frameType, data, newOffset);
 }
}

代码示例来源:origin: vert-x3/vertx-web

/**
 * Writing multiple continuation frames from the client side should result in a single message on the server side
 * after the frames are re-combined
 */
@Test
public void testCombineBinaryContinuationFramesRawWebSocket() throws InterruptedException {
 String serverPath = "/combine";
 AtomicReference<Buffer> serverReceivedMessage = new AtomicReference<>();
 setupSockJsServer(serverPath, (sock, requestBuffer) -> {
  serverReceivedMessage.set(requestBuffer);
  sock.write(Buffer.buffer("reply"));
  sock.close();
 });
 Buffer largeMessage = Buffer.buffer(TestUtils.randomAlphaString(30));
 WebSocketFrame frame1 = WebSocketFrame.binaryFrame(largeMessage.slice(0, 10), false);
 WebSocketFrame frame2 = WebSocketFrame.continuationFrame(largeMessage.slice(10, 20), false);
 WebSocketFrame frame3 = WebSocketFrame.continuationFrame(largeMessage.slice(20, largeMessage.length()), true);
 WebSocket ws = setupRawWebsocketClient(serverPath);
 ws.writeFrame(frame1);
 ws.writeFrame(frame2);
 ws.writeFrame(frame3);
 await(5, TimeUnit.SECONDS);
 assertEquals("Server did not combine continuation frames correctly", largeMessage, serverReceivedMessage.get());
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void testPartialH2CAmbiguousRequest() throws Exception {
 server.requestHandler(req -> {
  assertEquals("POST", req.rawMethod());
  testComplete();
 });
 Buffer fullRequest = Buffer.buffer("POST /whatever HTTP/1.1\r\n\r\n");
 startServer();
 NetClient client = vertx.createNetClient();
 client.connect(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(so -> {
  so.write(fullRequest.slice(0, 1));
  vertx.setTimer(1000, id -> {
   so.write(fullRequest.slice(1, fullRequest.length()));
  });
 }));
 await();
}

代码示例来源:origin: vert-x3/vertx-web

@Test
public void testCombineTextFrameSockJs() throws InterruptedException {
 String serverPath = "/text-combine-sockjs";
 setupSockJsServer(serverPath, this::echoRequest);
 List<Buffer> receivedMessages = new ArrayList<>();
 WebSocket openedWebSocket = setupSockJsClient(serverPath, receivedMessages);
 Buffer largeMessage = Buffer.buffer("[\"" + TestUtils.randomAlphaString(30) + "\"]");
 WebSocketFrame frame1 = new WebSocketFrameImpl(FrameType.TEXT, largeMessage.slice(0, 10).getByteBuf(), false);
 WebSocketFrame frame2 = WebSocketFrame.continuationFrame(largeMessage.slice(10, 20), false);
 WebSocketFrame frame3 = WebSocketFrame.continuationFrame(largeMessage.slice(20, largeMessage.length()), true);
 log.debug("Client sending " + frame1.textData());
 openedWebSocket.writeFrame(frame1);
 log.debug("Client sending " + frame2.textData());
 openedWebSocket.writeFrame(frame2);
 log.debug("Client sending " + frame3.textData());
 openedWebSocket.writeFrame(frame3);
 await(5, TimeUnit.SECONDS);
 assertEquals("Client should have received 2 messages: the reply and the close.", 2, receivedMessages.size());
 Buffer expectedReply = Buffer.buffer("a" + largeMessage.toString());
 assertEquals("Client reply should have matched request", expectedReply, receivedMessages.get(0));
 assertEquals("Final message should have been a close", SOCKJS_CLOSE_REPLY, receivedMessages.get(1));
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void testSlice1() throws Exception {
 Buffer buff = TestUtils.randomBuffer(100);
 Buffer sliced = buff.slice();
 assertEquals(buff, sliced);
 long rand = TestUtils.randomLong();
 sliced.setLong(0, rand);
 assertEquals(rand, buff.getLong(0));
 buff.appendString(TestUtils.randomUnicodeString(100));
 assertEquals(100, sliced.length());
}

代码示例来源:origin: io.vertx/vertx-core

@Test
public void testSlice2() throws Exception {
 Buffer buff = TestUtils.randomBuffer(100);
 Buffer sliced = buff.slice(10, 20);
 for (int i = 0; i < 10; i++) {
  assertEquals(buff.getByte(10 + i), sliced.getByte(i));
 }
 long rand = TestUtils.randomLong();
 sliced.setLong(0, rand);
 assertEquals(rand, buff.getLong(10));
 buff.appendString(TestUtils.randomUnicodeString(100));
 assertEquals(10, sliced.length());
}

相关文章

微信公众号

最新文章

更多