本文整理汇总了Java中io.netty.buffer.Unpooled.copiedBuffer方法的典型用法代码示例。如果您正苦于以下问题:Java Unpooled.copiedBuffer方法的具体用法?Java Unpooled.copiedBuffer怎么用?Java Unpooled.copiedBuffer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.buffer.Unpooled
的用法示例。
在下文中一共展示了Unpooled.copiedBuffer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testEncodings
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
@Test
public void testEncodings() throws Exception {
final PatternChunkSplitter splitter = new PatternChunkSplitter("^(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)");
// "Feb 20 17:05:18 Hällö Wörld\nFeb 20 17:05:18 Büe\n" in ISO-8859-1 encoding
final byte[] bytes = new byte[]{
0x46, 0x65, 0x62, 0x20, 0x32, 0x30, 0x20, 0x31, 0x37, 0x3a, 0x30, 0x35, 0x3a, 0x31, 0x38, 0x20,
0x48, (byte) 0xe4, 0x6c, 0x6c, (byte) 0xf6, 0x20, 0x57, (byte) 0xf6, 0x72, 0x6c, 0x64, 0x0a,
0x46, 0x65, 0x62, 0x20, 0x32, 0x30, 0x20, 0x31, 0x37, 0x3a, 0x30, 0x35, 0x3a, 0x31, 0x38, 0x20,
0x42, (byte) 0xfc, 0x65, 0x0a
};
// With correct encoding
final ByteBuf buffer = Unpooled.copiedBuffer(bytes);
final Iterator<String> iterator = splitter.splitRemaining(buffer, ISO_8859_1).iterator();
assertEquals("Feb 20 17:05:18 Hällö Wörld\n", iterator.next());
assertEquals("Feb 20 17:05:18 Büe\n", iterator.next());
// With wrong encoding
final ByteBuf buffer2 = Unpooled.copiedBuffer(bytes);
final Iterator<String> iterator2 = splitter.splitRemaining(buffer2, UTF_8).iterator();
assertNotEquals("Feb 20 17:05:18 Hällö Wörld\n", iterator2.next());
assertNotEquals("Feb 20 17:05:18 Büe\n", iterator2.next());
}
示例2: writeResponse
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
private boolean writeResponse(HttpObject currentObj, ChannelHandlerContext ctx) {
// Decide whether to close the connection or not.
boolean keepAlive = HttpHeaders.isKeepAlive(request);
// Build the response object.
FullHttpResponse response = new DefaultFullHttpResponse(
HTTP_1_1, currentObj.getDecoderResult().isSuccess() ? OK : BAD_REQUEST,
Unpooled.copiedBuffer(buf.toString(), CharsetUtil.UTF_8));
response.headers().set(CONTENT_TYPE, "application/json");
if (keepAlive) {
// Add 'Content-Length' header only for a keep-alive connection.
response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
// Add keep alive header as per:
// - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
response.headers().set(CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}
// Write the response.
ctx.write(response);
return keepAlive;
}
示例3: sendHttpResponse
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
private static void sendHttpResponse(ChannelHandlerContext ctx,
HttpRequest req, FullHttpResponse res) {
// Generate an error page if response getStatus code is not OK (200).
if (res.getStatus().code() != 200) {
ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(),
CharsetUtil.UTF_8);
res.content().writeBytes(buf);
buf.release();
setContentLength(res, res.content().readableBytes());
}
// Send the response and close the connection if necessary.
ChannelFuture f = ctx.writeAndFlush(res);
if (!isKeepAlive(req) || res.getStatus().code() != 200) {
f.addListener(ChannelFutureListener.CLOSE);
}
}
示例4: readUTF8String
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
public static String readUTF8String(byte[] f) {
val from = Unpooled.copiedBuffer(f);
from.readByte();
int len = readVarInt(from, 2);
//from.readerIndex(from.readerIndex() + len);
return from.toString(StandardCharsets.UTF_8);
}
示例5: offer
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
public void offer(FramePacket frame) {
Preconditions.checkState(frame.fragmented, "Only accept fragmented frame packet");
int fragmentIndex = frame.fragmentIndex;
int fragmentID = frame.fragmentID;
int fragmentCount = frame.fragmentCount;
ByteBuf[] fragments;
//TODO: Add pool size checking
if (!fragmentPool.containsKey(fragmentID)) {
fragmentPool.put(fragmentID, fragments = new ByteBuf[fragmentCount]);
} else {
fragments = fragmentPool.get(fragmentID);
}
fragments[fragmentIndex] = frame.fragment;
//Check if all fragments arrived
for (ByteBuf buf : fragments) {
if (buf == null) {
return;
}
}
fragmentPool.remove(fragmentID);
ByteBuf fullBuf = Unpooled.copiedBuffer(fragments);
session.handle(SessionPacket.from(fullBuf));
}
示例6: sendError
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
FullHttpResponse response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, status, Unpooled.copiedBuffer("Failure: " + status + "\r\n", CharsetUtil.UTF_8));
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
// Close the connection as soon as the error message is sent.
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
示例7: onMessage
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
@Override
public void onMessage(MalmoMessageType messageType, Map<String, String> data)
{
String bufstring = data.get("message");
ByteBuf buf = Unpooled.copiedBuffer(DatatypeConverter.parseBase64Binary(bufstring));
ItemStack itemStack = ByteBufUtils.readItemStack(buf);
if (itemStack != null && itemStack.getItem() != null)
{
accumulateReward(this.params.getDimension(), itemStack);
}
else
{
System.out.println("Error - couldn't understand the itemstack we received.");
}
}
示例8: sendListing
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
private void sendListing(ChannelHandlerContext ctx, File dir) throws IOException {
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");
ByteBuf buffer = Unpooled.copiedBuffer(Pages.getDirectory(dir), CharsetUtil.UTF_8);
response.content().writeBytes(buffer);
buffer.release();
// Close the connection as soon as the error message is sent.
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
示例9: itIndicatesTheFinalChunkWhenChunking
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
@Test
public void itIndicatesTheFinalChunkWhenChunking() {
ByteBuf buffer = Unpooled.copiedBuffer(new byte[1000]);
session.sendChunk(buffer, true);
verify(channel).write(new DefaultSmtpRequest(SmtpCommand.valueOf("BDAT"), Integer.toString(buffer.readableBytes()), "LAST"));
verify(channel).write(buffer);
verify(channel).flush();
}
示例10: testSingleBuffer
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
@Test
public void testSingleBuffer() {
final Path logFile = Paths.get("/tmp/file"); // this is never actually materialized
// the Buffer subclass to check the messages that are generated
final ChunkProcessor processor = setupProcessor(logFile, new Buffer() {
int messagenumber = 0;
@Override
public void insert(Message message) {
log.debug("Received message {}", message);
messagenumber++;
assertEquals("test", message.getSource());
switch (messagenumber) {
case 1:
assertEquals("some line", message.getMessage());
break;
case 2:
assertEquals("another line", message.getMessage());
break;
}
}
@Override
public Message remove() {
return null;
}
});
final FileChunk chunk = new FileChunk(logFile, Unpooled.copiedBuffer("some line\nanother line\n", UTF_8), 1);
processor.process(chunk);
}
示例11: successfulSplitLF
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
@Test
public void successfulSplitLF() throws Exception {
final NewlineChunkSplitter splitter = new NewlineChunkSplitter();
final String logLines = "Feb 20 17:05:18 otter kernel[0]: CODE SIGNING: cs_invalid_page(0x1000): p=32696[GoogleSoftwareUp] final status 0x0, allow (remove VALID)ing page\n" +
"Feb 20 17:05:18 otter GoogleSoftwareUpdateDaemon[32697]: -[KeystoneDaemon logServiceState] GoogleSoftwareUpdate daemon (1.1.0.3659) vending:\n" +
"Feb 20 17:05:18 otter GoogleSoftwareUpdateDaemon[32697]: -[KSUpdateEngine updateProductID:] KSUpdateEngine updating product ID: \"com.google.Keystone\"\n";
final ByteBuf buffer = Unpooled.copiedBuffer(logLines, UTF_8);
final Iterable<String> firstTwoChunks = splitter.split(buffer, UTF_8);
final Iterable<String> remainingChunk = splitter.splitRemaining(buffer, UTF_8);
int messageNum = 0;
for (String chunk : Iterables.concat(firstTwoChunks, remainingChunk)) {
switch (++messageNum) {
case 1:
assertEquals("Feb 20 17:05:18 otter kernel[0]: CODE SIGNING: cs_invalid_page(0x1000): p=32696[GoogleSoftwareUp] final status 0x0, allow (remove VALID)ing page", chunk);
break;
case 2:
assertEquals("Feb 20 17:05:18 otter GoogleSoftwareUpdateDaemon[32697]: -[KeystoneDaemon logServiceState] GoogleSoftwareUpdate daemon (1.1.0.3659) vending:", chunk);
break;
case 3:
assertEquals("Feb 20 17:05:18 otter GoogleSoftwareUpdateDaemon[32697]: -[KSUpdateEngine updateProductID:] KSUpdateEngine updating product ID: \"com.google.Keystone\"", chunk);
break;
}
}
assertEquals("the last chunk should have triggered a message (no follow mode active)", 3, messageNum);
}
示例12: successfulSplitCRLF
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
@Test
public void successfulSplitCRLF() throws Exception {
final NewlineChunkSplitter splitter = new NewlineChunkSplitter();
final String logLines = "Feb 20 17:05:18 otter kernel[0]: CODE SIGNING: cs_invalid_page(0x1000): p=32696[GoogleSoftwareUp] final status 0x0, allow (remove VALID)ing page\r\n" +
"Feb 20 17:05:18 otter GoogleSoftwareUpdateDaemon[32697]: -[KeystoneDaemon logServiceState] GoogleSoftwareUpdate daemon (1.1.0.3659) vending:\r\n" +
"Feb 20 17:05:18 otter GoogleSoftwareUpdateDaemon[32697]: -[KSUpdateEngine updateProductID:] KSUpdateEngine updating product ID: \"com.google.Keystone\"\r\n";
final ByteBuf buffer = Unpooled.copiedBuffer(logLines, UTF_8);
final Iterable<String> firstTwoChunks = splitter.split(buffer, UTF_8);
final Iterable<String> remainingChunk = splitter.splitRemaining(buffer, UTF_8);
int messageNum = 0;
for (String chunk : Iterables.concat(firstTwoChunks, remainingChunk)) {
switch (++messageNum) {
case 1:
assertEquals("Feb 20 17:05:18 otter kernel[0]: CODE SIGNING: cs_invalid_page(0x1000): p=32696[GoogleSoftwareUp] final status 0x0, allow (remove VALID)ing page", chunk);
break;
case 2:
assertEquals("Feb 20 17:05:18 otter GoogleSoftwareUpdateDaemon[32697]: -[KeystoneDaemon logServiceState] GoogleSoftwareUpdate daemon (1.1.0.3659) vending:", chunk);
break;
case 3:
assertEquals("Feb 20 17:05:18 otter GoogleSoftwareUpdateDaemon[32697]: -[KSUpdateEngine updateProductID:] KSUpdateEngine updating product ID: \"com.google.Keystone\"", chunk);
break;
}
}
assertEquals("the last chunk should have triggered a message (no follow mode active)", 3, messageNum);
}
示例13: sendError
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
ByteBuf content = Unpooled.copiedBuffer("Failure: " + status + "\r\n",
CharsetUtil.UTF_8);
FullHttpResponse response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, status, content);
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
// Close the connection as soon as the error message is sent.
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
示例14: prepareServerIcon
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
private void prepareServerIcon()
{
if (this.server.getBase64EncodedIconData() == null)
{
this.mc.getTextureManager().deleteTexture(this.serverIcon);
this.icon = null;
}
else
{
ByteBuf bytebuf = Unpooled.copiedBuffer((CharSequence)this.server.getBase64EncodedIconData(), Charsets.UTF_8);
ByteBuf bytebuf1 = null;
BufferedImage bufferedimage;
label103:
{
try
{
bytebuf1 = Base64.decode(bytebuf);
bufferedimage = TextureUtil.readBufferedImage(new ByteBufInputStream(bytebuf1));
Validate.validState(bufferedimage.getWidth() == 64, "Must be 64 pixels wide", new Object[0]);
Validate.validState(bufferedimage.getHeight() == 64, "Must be 64 pixels high", new Object[0]);
break label103;
}
catch (Throwable throwable)
{
LOGGER.error("Invalid icon for server {} ({})", new Object[] {this.server.serverName, this.server.serverIP, throwable});
this.server.setBase64EncodedIconData((String)null);
}
finally
{
bytebuf.release();
if (bytebuf1 != null)
{
bytebuf1.release();
}
}
return;
}
if (this.icon == null)
{
this.icon = new DynamicTexture(bufferedimage.getWidth(), bufferedimage.getHeight());
this.mc.getTextureManager().loadTexture(this.serverIcon, this.icon);
}
bufferedimage.getRGB(0, 0, bufferedimage.getWidth(), bufferedimage.getHeight(), this.icon.getTextureData(), 0, bufferedimage.getWidth());
this.icon.updateDynamicTexture();
}
}
示例15: buildDefaultFullHttpResponse
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
/**
* convert response to DefaultFullHttpResponse
*/
public HttpResponse buildDefaultFullHttpResponse() throws IOException {
DefaultHttpResponse fullHttpResponse = null;
int length = 0;
//set body
if (this.body() == null && this.file == null) {
fullHttpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, this.responseStatus());
} else {
if (this.body() != null) {
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, this.responseStatus(), Unpooled.copiedBuffer(JSON.toJSONString(body()).getBytes()));
length = response.content().readableBytes();
fullHttpResponse = response;
} else {
//http file
fullHttpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, this.responseStatus());
}
}
//set headers
for (String s : this.headers().keySet()) {
fullHttpResponse.headers().add(s, headers.get(s));
}
//set content type if has not been done
if (!fullHttpResponse.headers().contains("Content-type")) {
if (body instanceof String) {
fullHttpResponse.headers().set(HttpHeader.CONTENT_TYPE, "text/plain");
} else {
fullHttpResponse.headers().set(HttpHeader.CONTENT_TYPE, "application/json;charset=utf-8");
}
}
//set set-cookie header
if (cookies() != null) {
fullHttpResponse.headers().add(HttpHeader.SET_COOKIE, cookies().stream()
.map(Cookie::toString)
.collect(Collectors.toList()));
}
fullHttpResponse.headers().set(HttpHeader.CONNECTION, "keep-alive");
fullHttpResponse.headers().set(HttpHeader.CONTENT_LENGTH, length + fileLength());
fullHttpResponse.headers().set(HttpHeader.SERVER, "Ink");
return fullHttpResponse;
}