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

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

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

Buffer.toString介绍

[英]Returns a String representation of the Buffer with the UTF-8 encoding
[中]返回具有UTF-8编码的缓冲区的字符串表示形式

代码示例

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

@Test
public void testToString() throws Exception {
 String str = TestUtils.randomUnicodeString(100);
 Buffer buff = Buffer.buffer(str);
 assertEquals(str, buff.toString());
 //TODO toString with encoding
}

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

@Test
public void testReadFileInDirThenReadDir() {
 Buffer buff = vertx.fileSystem().readFileBlocking("webroot/subdir/subfile.html");
 assertEquals(buff.toString(), "<html><body>subfile</body></html>");
 Set<String> names = vertx.fileSystem().readDirBlocking("webroot/subdir").stream().map(path -> {
  int idx = path.lastIndexOf(File.separator);
  return idx == -1 ? path : path.substring(idx + 1);
 }).collect(Collectors.toSet());
 assertEquals(names, new HashSet<>(Arrays.asList("subdir2", "subfile.html")));
}

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

@Test
public void encodeToBuffer() {
 Buffer json = Json.encodeToBuffer("Hello World!");
 assertNotNull(json);
 // json strings are always UTF8
 assertEquals("\"Hello World!\"", json.toString("UTF-8"));
}

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

@Test
public void testPausedHttpClientResponseUnpauseTheConnectionAtRequestEnd() throws Exception {
 testHttpClientResponsePause(resp -> {
  resp.handler(buff -> {
   // Pausing the request here should have no effect since it's the last chunk
   assertEquals("ok", buff.toString());
   resp.pause();
  });
 });
}

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

@Test
 public void testDirect() {
  Buffer buff = Buffer.factory.directBuffer("hello world".getBytes());
  assertEquals("hello world", buff.toString());
  buff.appendString(" foobar");
  assertEquals("hello world foobar", buff.toString());
  ByteBuf bb = buff.getByteBuf().unwrap();
  assertTrue(bb.isDirect());
  assertTrue(bb.release());
  try {
   // Check it's deallocated
   buff.toString();
   fail();
  } catch (IllegalReferenceCountException e) {
  }
 }
}

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

@Test
public void testAppendString1() throws Exception {
 String str = TestUtils.randomUnicodeString(100);
 byte[] sb = str.getBytes("UTF-8");
 Buffer b = Buffer.buffer();
 b.appendString(str);
 assertEquals(b.length(), sb.length);
 assertTrue(str.equals(b.toString("UTF-8")));
 assertTrue(str.equals(b.toString(StandardCharsets.UTF_8)));
 assertNullPointerException(() -> b.appendString(null));
 assertNullPointerException(() -> b.appendString(null, "UTF-8"));
 assertNullPointerException(() -> b.appendString("", null));
}

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

@Test
public void testListenSocketAddress() {
 NetClient netClient = vertx.createNetClient();
 server = vertx.createHttpServer().requestHandler(req -> req.response().end());
 SocketAddress sockAddress = SocketAddress.inetSocketAddress(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST);
 server.listen(sockAddress, onSuccess(server -> {
  netClient.connect(sockAddress, onSuccess(sock -> {
   sock.handler(buf -> {
    assertTrue("Response is not an http 200", buf.toString("UTF-8").startsWith("HTTP/1.1 200 OK"));
    testComplete();
   });
   sock.write("GET / HTTP/1.1\r\n\r\n");
  }));
 }));
 try {
  await();
 } finally {
  netClient.close();
 }
}

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

@Test
public void testDefaultRequestHeaders() {
  Handler<HttpServerRequest> requestHandler = req -> {
    //    assertEquals(2, req.headers().size());
    assertEquals(HttpVersion.HTTP_2, req.version());
    //  assertEquals("localhost:" + DEFAULT_HTTP_PORT, req.headers().get("host"));
    assertNotNull(req.headers().get("Accept-Encoding"));
    req.response().end(Buffer.buffer(COMPRESS_TEST_STRING).toString(CharsetUtil.UTF_8));
  };
  serverWithMinCompressionLevel.requestHandler(requestHandler);
  serverWithMaxCompressionLevel.requestHandler(requestHandler);
  serverWithMinCompressionLevel.listen(onSuccess(serverReady -> {
    testMinCompression();
    testRawMinCompression();
  }));
  serverWithMaxCompressionLevel.listen(onSuccess(serverReady -> {
    testMaxCompression();
    testRawMaxCompression();
  }));
  await();
}

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

@Test
public void testServerWebsocketPingPong() {
 server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST));
 server.websocketHandler(ws -> {
  ws.pongHandler(buff -> {
   assertEquals("ping", buff.toString());
   testComplete();
  });
  ws.writePing(Buffer.buffer("ping"));
 }).listen(onSuccess(v -> {
  client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> {});
 }));
 await();
}

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

@Test
public void testSendFileFromClasspath() {
 vertx.createHttpServer(new HttpServerOptions().setPort(8080)).requestHandler(res -> {
  res.response().sendFile(webRoot + "/somefile.html");
 }).listen(onSuccess(res -> {
  vertx.createHttpClient(new HttpClientOptions()).request(HttpMethod.GET, 8080, "localhost", "/", onSuccess(resp -> {
   resp.bodyHandler(buff -> {
    assertTrue(buff.toString().startsWith("<html><body>blah</body></html>"));
    testComplete();
   });
  })).end();
 }));
 await();
}

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

@Test
public void testDefaultRequestHeaders() {
 Handler<HttpServerRequest> requestHandler = req -> {
  assertEquals(2, req.headers().size());
  //  assertEquals("localhost:" + DEFAULT_HTTP_PORT, req.headers().get("host"));
  assertNotNull(req.headers().get("Accept-Encoding"));
  req.response().end(Buffer.buffer(COMPRESS_TEST_STRING).toString(CharsetUtil.UTF_8));
 };
 serverWithMinCompressionLevel.requestHandler(requestHandler);
 serverWithMaxCompressionLevel.requestHandler(requestHandler);
 serverWithMinCompressionLevel.listen(onSuccess(serverReady -> {
  testMinCompression();
  testRawMinCompression();
 }));
 serverWithMaxCompressionLevel.listen(onSuccess(serverReady -> {
  testMaxCompression();
  testRawMaxCompression();
 }));
 await();
}

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

@Test
public void testSkipEncoding() throws Exception {
 serverWithMaxCompressionLevel.requestHandler(req -> {
  assertNotNull(req.headers().get("Accept-Encoding"));
  req.response()
   .putHeader(HttpHeaders.CONTENT_ENCODING, HttpHeaders.IDENTITY)
   .end(Buffer.buffer(COMPRESS_TEST_STRING).toString(CharsetUtil.UTF_8));
 });
 startServer(serverWithMaxCompressionLevel);
 clientraw.get(DEFAULT_HTTP_PORT + 1, DEFAULT_HTTP_HOST, "some-uri",
  onSuccess(resp -> {
   resp.bodyHandler(responseBuffer -> {
    String responseBody = responseBuffer.toString(CharsetUtil.UTF_8);
    assertEquals(COMPRESS_TEST_STRING, responseBody);
    testComplete();
   });
  })).putHeader(HttpHeaders.ACCEPT_ENCODING, HttpHeaders.DEFLATE_GZIP).end();
 await();
}

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

@Test
public void testSendOpenRangeFileFromClasspath() {
 vertx.createHttpServer(new HttpServerOptions().setPort(8080)).requestHandler(res -> {
  res.response().sendFile("webroot/somefile.html", 6);
 }).listen(onSuccess(res -> {
  vertx.createHttpClient(new HttpClientOptions()).request(HttpMethod.GET, 8080, "localhost", "/", onSuccess(resp -> {
   resp.bodyHandler(buff -> {
    assertTrue(buff.toString().startsWith("<body>blah</body></html>"));
    testComplete();
   });
  })).end();
 }));
 await();
}

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

@Test
public void testServerActualPortWhenSet() {
 server
   .requestHandler(request -> {
    request.response().end("hello");
   })
   .listen(ar -> {
    assertEquals(ar.result().actualPort(), DEFAULT_HTTP_PORT);
    vertx.createHttpClient(createBaseClientOptions()).getNow(ar.result().actualPort(), DEFAULT_HTTP_HOST, "/", onSuccess(response -> {
     assertEquals(response.statusCode(), 200);
     response.bodyHandler(body -> {
      assertEquals(body.toString("UTF-8"), "hello");
      testComplete();
     });
    }));
   });
 await();
}

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

@Test
public void testClientWebsocketPingPong() {
 server = vertx.createHttpServer(new HttpServerOptions().setIdleTimeout(1).setPort(DEFAULT_HTTP_PORT).setHost(HttpTestBase.DEFAULT_HTTP_HOST));
 server.websocketHandler(ws -> {
 }).listen(onSuccess(v -> {
  client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, "/", ws -> {
   ws.pongHandler( pong -> {
    assertEquals("ping", pong.toString());
    testComplete();
   });
   ws.writePing(Buffer.buffer("ping"));
  });
 }));
 await();
}

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

@Test
// Let's manually handle the websocket handshake and write a frame to the client
public void testHandleWSManually() throws Exception {
 String path = "/some/path";
 String message = "here is some text data";
 server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).requestHandler(req -> {
  NetSocket sock = getUpgradedNetSocket(req, path);
  // Let's write a Text frame raw
  Buffer buff = Buffer.buffer();
  buff.appendByte((byte)129); // Text frame
  buff.appendByte((byte)message.length());
  buff.appendString(message);
  sock.write(buff);
 });
 server.listen(ar -> {
  assertTrue(ar.succeeded());
  client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path).
    handler(ws -> {
     ws.handler(buff -> {
      assertEquals(message, buff.toString("UTF-8"));
      testComplete();
     });
    });
 });
 await();
}

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

@Test
public void testSendRangeFileFromClasspath() {
 vertx.createHttpServer(new HttpServerOptions().setPort(8080)).requestHandler(res -> {
  res.response().sendFile("webroot/somefile.html", 6, 6);
 }).listen(onSuccess(res -> {
  vertx.createHttpClient(new HttpClientOptions()).request(HttpMethod.GET, 8080, "localhost", "/", onSuccess(resp -> {
   resp.bodyHandler(buff -> {
    assertEquals("<body>", buff.toString());
    testComplete();
   });
  })).end();
 }));
 await();
}

相关文章