本文整理了Java中io.vertx.core.buffer.Buffer.appendString()
方法的一些代码示例,展示了Buffer.appendString()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Buffer.appendString()
方法的具体详情如下:
包路径:io.vertx.core.buffer.Buffer
类名称:Buffer
方法名:appendString
[英]Appends the specified String str to the end of the Buffer with UTF-8 encoding.
The buffer will expand as necessary to accommodate any bytes written.
Returns a reference to this so multiple operations can be appended together
[中]使用UTF-8编码将指定的字符串str追加到缓冲区的末尾。
缓冲区将根据需要扩展以容纳写入的任何字节。
返回对此的引用,以便可以将多个操作附加在一起
代码示例来源:origin: vert-x3/vertx-examples
public Buffer generateHTML(int sz) {
final Buffer buffer = Buffer.buffer();
buffer.appendString("<!DOCTYPE html>");
buffer.appendString("<html>");
buffer.appendString("<head>");
buffer.appendString("<title>H2 test</title>");
buffer.appendString("<style>" +
"#wrap { width:" + width * sz + "px; height:" + height * sz + "px };" +
".row { width:" + width * sz + "px }" +
"img { margin:0; padding:0; border:none; float:left; width: " + sz + "px; height: " + sz + "px }" +
"</style>");
buffer.appendString("</head>");
buffer.appendString("<body><div id=\"wrap\">");
for (int i = 0; i < height; i++) {
buffer.appendString("<div class=\"row\">");
for (int j = 0; j < width; j++) {
buffer.appendString("<img src=\"/img/" + j + "/" + i + "\">");
}
buffer.appendString("</div>");
}
buffer.appendString("</div></body>");
buffer.appendString("</html>");
return buffer;
}
代码示例来源:origin: eclipse-vertx/vert.x
@Override
public void writeToBuffer(Buffer buffer) {
buffer.appendInt(str.length());
buffer.appendString(str);
}
代码示例来源:origin: vert-x3/vertx-examples
@Override
public void encodeToWire(Buffer buffer, CustomMessage customMessage) {
// Easiest ways is using JSON object
JsonObject jsonToEncode = new JsonObject();
jsonToEncode.put("statusCode", customMessage.getStatusCode());
jsonToEncode.put("resultCode", customMessage.getResultCode());
jsonToEncode.put("summary", customMessage.getSummary());
// Encode object to string
String jsonToStr = jsonToEncode.encode();
// Length of JSON: is NOT characters count
int length = jsonToStr.getBytes().length;
// Write data into given buffer
buffer.appendInt(length);
buffer.appendString(jsonToStr);
}
代码示例来源:origin: eclipse-vertx/vert.x
private NetSocket handshake(HttpServerRequest req) {
NetSocket so = req.netSocket();
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
byte[] inputBytes = (req.getHeader("Sec-WebSocket-Key") + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").getBytes();
digest.update(inputBytes);
byte[] hashedBytes = digest.digest();
byte[] accept = Base64.getEncoder().encode(hashedBytes);
Buffer data = Buffer.buffer();
data.appendString("HTTP/1.1 101 Switching Protocols\r\n");
data.appendString("Upgrade: websocket\r\n");
data.appendString("Connection: upgrade\r\n");
data.appendString("Sec-WebSocket-Accept: " + new String(accept) + "\r\n");
data.appendString("\r\n");
so.write(data);
return so;
} catch (NoSuchAlgorithmException e) {
req.response().setStatusCode(500).end();
fail(e.getMessage());
return null;
}
}
代码示例来源: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
buff.appendByte((byte) 0x01); // Incomplete Text frame
buff.appendByte((byte) firstFrame.length());
buff.appendString(firstFrame);
sock.write(buff);
buff.appendByte((byte) (0x00 | 0x80)); // Complete continuation frame
buff.appendByte((byte) continuationFrame.length());
buff.appendString(continuationFrame);
sock.write(buff);
});
代码示例来源: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
} else {
count.incrementAndGet();
expected.appendString(chunk);
req.write(chunk);
sent.addAndGet(chunk.length());
代码示例来源:origin: apache/servicecomb-java-chassis
private Buffer boundaryEndInfo(String boundary) {
return Buffer.buffer()
.appendString("\r\n")
.appendString("--" + boundary + "--\r\n");
}
代码示例来源: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 testServerPipeliningConnectionConcurrency() throws Exception {
int n = 5;
boolean[] processing = {false};
int[] count = {0};
server.requestHandler(req -> {
count[0]++;
assertFalse(processing[0]);
processing[0] = true;
vertx.setTimer(20, id -> {
processing[0] = false;
HttpServerResponse resp = req.response();
resp.end();
if (count[0] == n) {
resp.close();
}
});
});
startServer();
Buffer requests = Buffer.buffer();
for (int i = 0;i < n;i++) {
requests.appendString("GET " + DEFAULT_TEST_URI + " HTTP/1.1\r\n\r\n");
}
NetClient client = vertx.createNetClient();
client.connect(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(so -> {
so.closeHandler(v -> testComplete());
so.write(requests);
}));
await();
}
代码示例来源: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
Buffer buffer = Buffer.buffer();
buffer.appendString("framework=" + URLEncoder.encode("vert x", "UTF-8") + "&runson=jvm", "UTF-8");
req.headers().set("content-length", String.valueOf(buffer.length()));
req.headers().set("content-type", "application/x-www-form-urlencoded");
代码示例来源:origin: eclipse-vertx/vert.x
}));
Buffer buffer = Buffer.buffer();
buffer.appendString("origin=junit-testUserAlias&login=admin%40foo.bar&pass+word=admin");
req.headers().set("content-length", String.valueOf(buffer.length()));
req.headers().set("content-type", "application/x-www-form-urlencoded");
代码示例来源: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: eclipse-vertx/vert.x
log.debug("got request: " + toHex(buffer3));
Buffer authReply = Buffer.buffer(new byte[] { 1, (byte) username.length() });
authReply.appendString(username);
authReply.appendByte((byte) username.length());
authReply.appendString(username);
if (!buffer3.equals(authReply)) {
log.debug("expected " + toHex(authReply) + ", got " + toHex(buffer3));
代码示例来源:origin: eclipse-vertx/vert.x
"--" + boundary + "--\r\n";
buffer.appendString(body);
req.headers().set("content-length", String.valueOf(buffer.length()));
req.headers().set("content-type", "multipart/form-data; boundary=" + boundary);
代码示例来源:origin: io.vertx/vertx-core
@Override
public void writeToBuffer(Buffer buffer) {
buffer.appendInt(str.length());
buffer.appendString(str);
}
代码示例来源: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());
}
内容来源于网络,如有侵权,请联系作者删除!