當前位置: 首頁>>代碼示例>>Java>>正文


Java ByteBuf.internalNioBuffer方法代碼示例

本文整理匯總了Java中io.netty.buffer.ByteBuf.internalNioBuffer方法的典型用法代碼示例。如果您正苦於以下問題:Java ByteBuf.internalNioBuffer方法的具體用法?Java ByteBuf.internalNioBuffer怎麽用?Java ByteBuf.internalNioBuffer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在io.netty.buffer.ByteBuf的用法示例。


在下文中一共展示了ByteBuf.internalNioBuffer方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: encode

import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
@Override
public ByteBuf encode(Object in) throws IOException {
    ByteBuf bytes = null;
    try {
        LZ4Compressor compressor = factory.fastCompressor();
        bytes = innerCodec.getValueEncoder().encode(in);
        ByteBuffer srcBuf = bytes.internalNioBuffer(bytes.readerIndex(), bytes.readableBytes());
        
        int outMaxLength = compressor.maxCompressedLength(bytes.readableBytes());
        ByteBuf out = ByteBufAllocator.DEFAULT.buffer(outMaxLength + DECOMPRESSION_HEADER_SIZE);
        out.writeInt(bytes.readableBytes());
        ByteBuffer outBuf = out.internalNioBuffer(out.writerIndex(), out.writableBytes());
        int pos = outBuf.position();
        
        compressor.compress(srcBuf, outBuf);
        
        int compressedLength = outBuf.position() - pos;
        out.writerIndex(out.writerIndex() + compressedLength);
        return out;
    } finally {
        if (bytes != null) {
            bytes.release();
        }
    }
}
 
開發者ID:qq1588518,項目名稱:JRediClients,代碼行數:26,代碼來源:LZ4Codec.java

示例2: hashToBase64

import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
public static String hashToBase64(ByteBuf objectState) {
    ByteBuffer bf = objectState.internalNioBuffer(objectState.readerIndex(), objectState.readableBytes());
    long h1 = LongHashFunction.farmUo().hashBytes(bf);
    long h2 = LongHashFunction.xx().hashBytes(bf);

    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer((2 * Long.SIZE) / Byte.SIZE);
    try {
        buf.writeLong(h1).writeLong(h2);
        ByteBuf b = Base64.encode(buf);
        try {
            String s = b.toString(CharsetUtil.UTF_8);
            return s.substring(0, s.length() - 2);
        } finally {
            b.release();
        }
    } finally {
        buf.release();
    }
}
 
開發者ID:qq1588518,項目名稱:JRediClients,代碼行數:20,代碼來源:Hash.java

示例3: decode

import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
@Override
public Object decode(ByteBuf buf, State state) throws IOException {
    int decompressSize = buf.readInt();
    ByteBuf out = ByteBufAllocator.DEFAULT.buffer(decompressSize);
    try {
        LZ4SafeDecompressor decompressor = factory.safeDecompressor();
        ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes());
        int pos = outBuffer.position();
        decompressor.decompress(buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()), outBuffer);
        int compressedLength = outBuffer.position() - pos;
        out.writerIndex(compressedLength);
        return innerCodec.getValueDecoder().decode(out, state);
    } finally {
        out.release();
    }
}
 
開發者ID:qq1588518,項目名稱:JRediClients,代碼行數:17,代碼來源:LZ4Codec.java

示例4: doReadMessages

import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    DatagramChannel ch = javaChannel();
    ChannelConfig config = config();
    RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();

    ByteBuf data = allocHandle.allocate(config.getAllocator());
    allocHandle.attemptedBytesRead(data.writableBytes());
    boolean free = true;
    try {
        ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes());
        int pos = nioData.position();
        int read = ch.read(nioData);
        if (read <= 0) {
            return read;
        }

        allocHandle.lastBytesRead(nioData.position() - pos);
        buf.add(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead()));
        free = false;
        return 1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    } finally {
        if (free) {
            data.release();
        }
    }
}
 
開發者ID:szhnet,項目名稱:kcp-netty,代碼行數:31,代碼來源:UkcpClientUdpChannel.java

示例5: doReadMessages

import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    DatagramChannel ch = javaChannel();
    UkcpServerChannelConfig config = config();
    RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();

    ByteBuf data = allocHandle.allocate(config.getAllocator());
    allocHandle.attemptedBytesRead(data.writableBytes());
    boolean free = true;
    try {
        ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes());
        int pos = nioData.position();
        InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData);
        if (remoteAddress == null) {
            return 0;
        }

        allocHandle.lastBytesRead(nioData.position() - pos);
        buf.add(UkcpPacket.newInstance(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead()),
                remoteAddress));
        free = false;
        return 1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    } finally {
        if (free) {
            data.release();
        }
    }
}
 
開發者ID:szhnet,項目名稱:kcp-netty,代碼行數:32,代碼來源:UkcpServerChannel.java

示例6: doWriteMessage

import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
@Override
protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception {
    UkcpPacket packet = (UkcpPacket) msg;
    InetSocketAddress remoteAddress = packet.remoteAddress();
    ByteBuf data = packet.content();

    final int dataLen = data.readableBytes();
    if (dataLen == 0) {
        return true;
    }

    final ByteBuffer nioData = data.internalNioBuffer(data.readerIndex(), dataLen);
    final int writtenBytes;
    writtenBytes = javaChannel().send(nioData, remoteAddress);
    return writtenBytes > 0;
}
 
開發者ID:szhnet,項目名稱:kcp-netty,代碼行數:17,代碼來源:UkcpServerChannel.java

示例7: hash

import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
public static byte[] hash(ByteBuf objectState) {
    ByteBuffer b = objectState.internalNioBuffer(objectState.readerIndex(), objectState.readableBytes());
    long h1 = LongHashFunction.farmUo().hashBytes(b);
    long h2 = LongHashFunction.xx().hashBytes(b);

    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer((2 * Long.SIZE) / Byte.SIZE);
    try {
        buf.writeLong(h1).writeLong(h2);
        byte[] dst = new byte[buf.readableBytes()];
        buf.readBytes(dst);
        return dst;
    } finally {
        buf.release();
    }
}
 
開發者ID:qq1588518,項目名稱:JRediClients,代碼行數:16,代碼來源:Hash.java

示例8: doWriteMessage

import io.netty.buffer.ByteBuf; //導入方法依賴的package包/類
@Override
protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception {
    ByteBuf data = (ByteBuf) msg;

    final int dataLen = data.readableBytes();
    if (dataLen == 0) {
        return true;
    }

    final ByteBuffer nioData = data.internalNioBuffer(data.readerIndex(), dataLen);
    final int writtenBytes;
    writtenBytes = javaChannel().write(nioData);
    return writtenBytes > 0;
}
 
開發者ID:szhnet,項目名稱:kcp-netty,代碼行數:15,代碼來源:UkcpClientUdpChannel.java


注:本文中的io.netty.buffer.ByteBuf.internalNioBuffer方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。