本文整理汇总了Java中io.netty.buffer.Unpooled.wrappedBuffer方法的典型用法代码示例。如果您正苦于以下问题:Java Unpooled.wrappedBuffer方法的具体用法?Java Unpooled.wrappedBuffer怎么用?Java Unpooled.wrappedBuffer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.buffer.Unpooled
的用法示例。
在下文中一共展示了Unpooled.wrappedBuffer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: exceptionCaught
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
Exception e = cause instanceof Exception ? (Exception) cause : new
Exception(cause);
final String output = JsonUtil.toJsonString(e);
ByteBuf content = Unpooled.wrappedBuffer(output.getBytes(Charsets.UTF_8));
final DefaultFullHttpResponse resp = new DefaultFullHttpResponse(
HTTP_1_1, INTERNAL_SERVER_ERROR, content);
resp.headers().set(CONTENT_TYPE, APPLICATION_JSON_UTF8);
if (e instanceof IllegalArgumentException) {
resp.setStatus(BAD_REQUEST);
} else if (e instanceof FileNotFoundException) {
resp.setStatus(NOT_FOUND);
}
resp.headers().set(CONTENT_LENGTH, resp.content().readableBytes());
resp.headers().set(CONNECTION, CLOSE);
ctx.write(resp).addListener(ChannelFutureListener.CLOSE);
}
示例2: getBase64EncodedString
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
/** Return a Base64-encoded string. */
private static String getBase64EncodedString(String str) {
ByteBuf byteBuf = null;
ByteBuf encodedByteBuf = null;
try {
byteBuf = Unpooled.wrappedBuffer(str.getBytes(StandardCharsets.UTF_8));
encodedByteBuf = Base64.encode(byteBuf);
return encodedByteBuf.toString(StandardCharsets.UTF_8);
} finally {
// The release is called to suppress the memory leak error messages raised by netty.
if (byteBuf != null) {
byteBuf.release();
if (encodedByteBuf != null) {
encodedByteBuf.release();
}
}
}
}
示例3: fireAndForget
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
@Override
public Mono<Void> fireAndForget(Payload payload) {
try {
ByteBuf metadata = Unpooled.wrappedBuffer(payload.getMetadata());
int namespaceId = ProteusMetadata.namespaceId(metadata);
int serviceId = ProteusMetadata.serviceId(metadata);
ProteusService proteusService = getService(namespaceId, serviceId);
if (proteusService == null) {
return Mono.error(new ServiceNotFound(namespaceId, serviceId));
}
return proteusService.fireAndForget(payload);
} catch (Throwable t) {
return Mono.error(t);
}
}
示例4: testImmutable
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
public void testImmutable() throws IOException {
BytesReference bytesReference = newBytesReference(randomIntBetween(10, 3 * PAGE_SIZE));
BytesRef bytesRef = BytesRef.deepCopyOf(bytesReference.toBytesRef());
ByteBuf channelBuffer = Unpooled.wrappedBuffer(bytesRef.bytes, bytesRef.offset,
bytesRef.length);
ByteBufBytesReference byteBufBytesReference = new ByteBufBytesReference(channelBuffer, bytesRef.length);
assertEquals(byteBufBytesReference, bytesReference);
channelBuffer.readInt(); // this advances the index of the channel buffer
assertEquals(byteBufBytesReference, bytesReference);
assertEquals(bytesRef, byteBufBytesReference.toBytesRef());
BytesRef unicodeBytes = new BytesRef(randomUnicodeOfCodepointLength(100));
channelBuffer = Unpooled.wrappedBuffer(unicodeBytes.bytes, unicodeBytes.offset, unicodeBytes.length);
byteBufBytesReference = new ByteBufBytesReference(channelBuffer, unicodeBytes.length);
String utf8ToString = byteBufBytesReference.utf8ToString();
channelBuffer.readInt(); // this advances the index of the channel buffer
assertEquals(utf8ToString, byteBufBytesReference.utf8ToString());
}
示例5: toContent
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
private static Object toContent(Object content) {
if (content instanceof File) {
File file = (File) content;
return new DefaultFileRegion(file, 0, file.length());
}
if (content instanceof InputStream) {
return new ChunkedStream((InputStream) content);
}
if (content instanceof ReadableByteChannel) {
return new ChunkedNioStream((ReadableByteChannel) content);
}
if (content instanceof byte[]) {
return Unpooled.wrappedBuffer((byte[]) content);
}
throw new IllegalArgumentException(
"unknown content type : " + content.getClass().getName());
}
示例6: prepareMetadata
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
private Metadata prepareMetadata(PreparedStatement metadataStmt, Message message) throws SQLException {
Metadata metadata = message.getMetadata();
metadataStmt.setLong(1, metadata.getInternalId());
metadataStmt.setString(2, metadata.getExchangeName());
metadataStmt.setString(3, metadata.getRoutingKey());
metadataStmt.setLong(4, metadata.getContentLength());
long size = metadata.getProperties().getSize() + metadata.getHeaders().getSize();
byte[] bytes = new byte[(int) size];
ByteBuf buffer = Unpooled.wrappedBuffer(bytes);
try {
buffer.resetWriterIndex();
metadata.getProperties().write(buffer);
metadata.getHeaders().write(buffer);
metadataStmt.setBytes(5, bytes);
metadataStmt.addBatch();
} finally {
buffer.release();
}
return metadata;
}
示例7: channelRead
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpRequest) {
HttpRequest request = (HttpRequest) msg;
logger.info("check");
}
if (msg instanceof HttpContent) {
HttpContent httpContent = (HttpContent) msg;
ByteBuf content = httpContent.content();
content.release();
ByteBuf byteBuf = Unpooled.wrappedBuffer("ok".getBytes("UTF-8"));
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, byteBuf);
response.headers().set("Content-Type", "text/html; charset=UTF-8");
response.headers().set("Content-Length", byteBuf.readableBytes());
ctx.channel().write(response);
ctx.channel().flush();
}
}
示例8: testInputStream
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
@Test
public void testInputStream() throws Exception {
Path filePath = new Path("/foo/bar");
byte[] data = new byte[100];
for (int i = 0; i < 100; ++i) {
data[i] = (byte)i;
}
byte [] readBuf = new byte[1000];
ByteBuf byteBuf = Unpooled.wrappedBuffer(data, 0, 100);
setupRPC(
DFS.RpcType.GET_FILE_DATA_REQUEST, DFS.GetFileDataRequest.newBuilder().setPath(filePath.toString()).setStart(0).setLength(100).build(),
DFS.RpcType.GET_FILE_DATA_RESPONSE, DFS.GetFileDataResponse.newBuilder().setRead(100).build(), byteBuf);
FileSystem fs = newRemoteNodeFileSystem();
FSDataInputStream inputStream = fs.open(filePath, 100);
int read = inputStream.read(readBuf, 0, 50); // read first 50 bytes to trigger first rpc
assertEquals(50, read);
setupRPC(
DFS.RpcType.GET_FILE_DATA_REQUEST, DFS.GetFileDataRequest.newBuilder().setPath(filePath.toString()).setStart(100).setLength(100).build(),
DFS.RpcType.GET_FILE_DATA_RESPONSE, DFS.GetFileDataResponse.newBuilder().setRead(-1).build());
read = inputStream.read(readBuf, 50, 1000); // read rest of the data
assertEquals(50, read);
for (int i = 0; i < 100; ++i) {
assertEquals((byte)i, readBuf[i]);
}
}
示例9: MultiPartCustomPayload
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
private MultiPartCustomPayload(PacketBuffer preamble) throws IOException
{
channel = preamble.readStringFromBuffer(20);
part_count = preamble.readUnsignedByte();
int length = preamble.readInt();
if (length <= 0 || length >= FMLProxyPacket.MAX_LENGTH)
{
throw new IOException("The received FML MultiPart packet outside of valid length bounds, Max: " + FMLProxyPacket.MAX_LENGTH + ", Received: " + length);
}
data = new byte[length];
data_buf = new PacketBuffer(Unpooled.wrappedBuffer(data));
}
示例10: testgetBytesFastByteBufHasArray
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
@Test
public void testgetBytesFastByteBufHasArray() {
byte[] bytes = new byte[] {1};
ByteBuf byteBuf = Unpooled.wrappedBuffer(bytes);
byte[] result = VertxUtils.getBytesFast(byteBuf);
Assert.assertSame(bytes, result);
}
示例11: newBytesReference
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
@Override
protected BytesReference newBytesReference(int length) throws IOException {
ReleasableBytesStreamOutput out = new ReleasableBytesStreamOutput(length, bigarrays);
for (int i = 0; i < length; i++) {
out.writeByte((byte) random().nextInt(1 << 8));
}
assertEquals(out.size(), length);
BytesReference ref = out.bytes();
assertEquals(ref.length(), length);
BytesRef bytesRef = ref.toBytesRef();
final ByteBuf buffer = Unpooled.wrappedBuffer(bytesRef.bytes, bytesRef.offset, bytesRef.length);
return Netty4Utils.toBytesReference(buffer);
}
示例12: testSliceOnAdvancedBuffer
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
public void testSliceOnAdvancedBuffer() throws IOException {
BytesReference bytesReference = newBytesReference(randomIntBetween(10, 3 * PAGE_SIZE));
BytesRef bytesRef = bytesReference.toBytesRef();
ByteBuf channelBuffer = Unpooled.wrappedBuffer(bytesRef.bytes, bytesRef.offset,
bytesRef.length);
int numBytesToRead = randomIntBetween(1, 5);
for (int i = 0; i < numBytesToRead; i++) {
channelBuffer.readByte();
}
BytesReference other = Netty4Utils.toBytesReference(channelBuffer);
BytesReference slice = bytesReference.slice(numBytesToRead, bytesReference.length() - numBytesToRead);
assertEquals(other, slice);
assertEquals(other.slice(3, 1), slice.slice(3, 1));
}
示例13: decipherXTEA
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
/**
* Deciphers the specified {@link ByteBuf} with the given key.
*
* @param buffer The {@link ByteBuf}.
* @param key The key.
* @return The new {@link ByteBuf}.
*/
public static ByteBuf decipherXTEA(ByteBuf buffer, int[] key) {
byte[] bytes = new byte[buffer.readableBytes()];
buffer.readBytes(bytes);
ByteBuf xteaBuffer = Unpooled.wrappedBuffer(bytes);
decipherXTEA(xteaBuffer, 0, bytes.length, key);
return xteaBuffer;
}
示例14: writeRequest
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
@Override
public ByteBuf writeRequest(ByteBufAllocator allocator, int sequenceId, MethodMetadata method, List<Object> parameters, Map<String, String> headers)
throws Exception
{
ByteBuf message = MessageEncoding.encodeRequest(protocolFactory, allocator, sequenceId, method, parameters);
//
// describe the encoding (Thrift protocol, compression info)
ByteBuf encodingInfo = Unpooled.buffer(3);
encodingInfo.writeByte(protocolId);
// number of "transforms"
encodingInfo.writeByte(gzip ? 1 : 0);
if (gzip) {
// Note this is actually a vint, but there are only 3 headers for now
encodingInfo.writeByte(0x01);
}
// headers
ByteBuf encodedHeaders = encodeHeaders(headers);
// Padding - header size must be a multiple of 4
int headerSize = encodingInfo.readableBytes() + encodedHeaders.readableBytes();
ByteBuf padding = getPadding(headerSize);
headerSize += padding.readableBytes();
// frame header (magic, flags, sequenceId, headerSize
ByteBuf frameHeader = Unpooled.buffer(12);
frameHeader.writeShort(HEADER_MAGIC);
frameHeader.writeShort(FLAG_SUPPORT_OUT_OF_ORDER);
frameHeader.writeInt(sequenceId);
frameHeader.writeShort(headerSize >> 2);
return Unpooled.wrappedBuffer(
frameHeader,
encodingInfo,
encodedHeaders,
padding,
message);
}
示例15: directSend
import io.netty.buffer.Unpooled; //导入方法依赖的package包/类
private void directSend(Message msg) {
MqttFixedHeader fixed = new MqttFixedHeader(MqttMessageType.PUBLISH, false, MqttQoS.AT_LEAST_ONCE, false, 2 + msg.topic.length() + 2 + msg.payload.length);
MqttPublishVariableHeader variable = new MqttPublishVariableHeader(msg.topic, (int) msg.msgId);
MqttPublishMessage publish = new MqttPublishMessage(fixed, variable, Unpooled.wrappedBuffer(msg.payload));
this.toAcks.add(new ToAck((int) msg.msgId, msg.topic, Calendar.getInstance().getTime()));
this.channel.writeAndFlush(publish);
}