当前位置: 首页>>代码示例>>Java>>正文


Java Unpooled.wrappedBuffer方法代码示例

本文整理汇总了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);
}
 
开发者ID:naver,项目名称:hadoop,代码行数:22,代码来源:FSImageHandler.java

示例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();
      }
    }
  }
}
 
开发者ID:spafka,项目名称:spark_deep,代码行数:19,代码来源:SparkSaslServer.java

示例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);
  }
}
 
开发者ID:netifi,项目名称:proteus-java,代码行数:20,代码来源:RequestHandlingRSocket.java

示例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());
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:19,代码来源:ByteBufBytesReferenceTests.java

示例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());
    }
 
开发者ID:warlock-china,项目名称:azeroth,代码行数:23,代码来源:FileOperationEncoder.java

示例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;
}
 
开发者ID:wso2,项目名称:message-broker,代码行数:24,代码来源:MessageDaoImpl.java

示例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();
    }
}
 
开发者ID:bridgeli,项目名称:netty-socketio-demo,代码行数:22,代码来源:HttpCheckServer.java

示例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]);
  }
}
 
开发者ID:dremio,项目名称:dremio-oss,代码行数:30,代码来源:TestRemoteNodeFileSystem.java

示例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));
}
 
开发者ID:F1r3w477,项目名称:CustomWorldGen,代码行数:13,代码来源:NetworkDispatcher.java

示例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);
}
 
开发者ID:apache,项目名称:incubator-servicecomb-java-chassis,代码行数:9,代码来源:TestVertxUtils.java

示例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);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:14,代码来源:ByteBufBytesReferenceTests.java

示例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));
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:15,代码来源:ByteBufBytesReferenceTests.java

示例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;
}
 
开发者ID:jordanabrahambaws,项目名称:Quavo,代码行数:15,代码来源:ByteBufUtils.java

示例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);
}
 
开发者ID:airlift,项目名称:drift,代码行数:40,代码来源:HeaderMessageEncoding.java

示例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);
}
 
开发者ID:linyuhe,项目名称:london,代码行数:8,代码来源:MqttHandler.java


注:本文中的io.netty.buffer.Unpooled.wrappedBuffer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。