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


Java ByteBuf.capacity方法代码示例

本文整理汇总了Java中io.netty.buffer.ByteBuf.capacity方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuf.capacity方法的具体用法?Java ByteBuf.capacity怎么用?Java ByteBuf.capacity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在io.netty.buffer.ByteBuf的用法示例。


在下文中一共展示了ByteBuf.capacity方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: encodeHeader

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
/**
 * encode报文头
 *
 * @param header
 *         MessageHeader
 * @param byteBuf
 *         报文
 * @see JSFProtocol
 */
public static short encodeHeader(MessageHeader header, ByteBuf byteBuf) {
    short headLength = 8; // 没有map 长度是8
    if( byteBuf.capacity() < 8 ) byteBuf.capacity(8);
    int writeIndex = byteBuf.writerIndex();
    byteBuf.writeShort(headLength);
    byteBuf.writeByte(header.getProtocolType());
    byteBuf.writeByte(header.getCodecType());
    byteBuf.writeByte(header.getMsgType());
    byteBuf.writeByte(header.getCompressType());
    byteBuf.writeInt(header.getMsgId());
    if (header.getAttrMapSize() > 0) {
        headLength += map2bytes(header.getAttrMap(), byteBuf);
        byteBuf.setBytes(writeIndex, short2bytes(headLength)); // 替换head长度的两位
    }
    return headLength;
}
 
开发者ID:tiglabs,项目名称:jsf-sdk,代码行数:26,代码来源:CodecUtils.java

示例2: decode

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) throws Exception {
    ByteBuf buf = frame.content().order(ByteOrder.LITTLE_ENDIAN);
    if (buf.capacity() < 1) {
        // Discard empty messages
        return;
    }

    buf.resetReaderIndex();
    int packetId = buf.readUnsignedByte();
    Packet packet = reg.SERVERBOUND.constructPacket(packetId);

    if (packet == null) {
        throw new UnknownPacketException("Unknown packet ID: " + packetId);
    }

    Server.log.finest("Received packet ID " + packetId + " (" + packet.getClass().getSimpleName() + ") from " + ctx.channel().remoteAddress());

    packet.readData(buf);
    out.add(packet);
}
 
开发者ID:AlexMog,项目名称:SurvivalMMO,代码行数:22,代码来源:PacketDecoder.java

示例3: PduAttachData

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
public PduAttachData(ByteString bytes) {
    
    buffer.writeBytes(bytes.toByteArray());
    
    this.type = buffer.readInt();
    this.handle = buffer.readLong();
    this.serviceType = buffer.readInt();
    this.pduLength = buffer.readInt();
    ByteBuf buf = buffer.readBytes(this.pduLength);
    
    if (buf.hasArray()) {
        this.pdu = ByteString.copyFrom(buf.array());
    } else {
        byte[] content = new byte[buf.capacity()];
        buf.readBytes(content);
        this.pdu = ByteString.copyFrom(content);
    }
}
 
开发者ID:ccfish86,项目名称:sctalk,代码行数:19,代码来源:PduAttachData.java

示例4: writeToStream

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
public void writeToStream(FSDataOutputStream stream) throws IOException {
  Stopwatch watch = new Stopwatch();
  watch.start();
  available = false;
  check = ThreadLocalRandom.current().nextLong();
  start = stream.getPos();
  logger.debug("Writing check value {} at position {}", check, start);
  stream.writeLong(check);
  batch.getHeader().writeDelimitedTo(stream);
  ByteBuf buf = batch.getBody();
  if (buf != null) {
    bodyLength = buf.capacity();
  } else {
    bodyLength = 0;
  }
  if (bodyLength > 0) {
    buf.getBytes(0, stream, bodyLength);
  }
  stream.hsync();
  FileStatus status = fs.getFileStatus(path);
  long len = status.getLen();
  logger.debug("After spooling batch, stream at position {}. File length {}", stream.getPos(), len);
  batch.sendOk();
  latch.countDown();
  long t = watch.elapsed(TimeUnit.MICROSECONDS);
  logger.debug("Took {} us to spool {} to disk. Rate {} mb/s", t, bodyLength, bodyLength / t);
  if (buf != null) {
    buf.release();
  }
}
 
开发者ID:skhalifa,项目名称:QDrill,代码行数:31,代码来源:SpoolingRawBatchBuffer.java

示例5: ByteBufBytesInput

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
public ByteBufBytesInput(ByteBuf buf, int offset, int length) {
  super();
  if(buf.capacity() == length && offset == 0){
    this.buf = buf;
  }else{
    this.buf = buf.slice(offset, length);
  }

  this.length = length;
}
 
开发者ID:skhalifa,项目名称:QDrill,代码行数:11,代码来源:DirectCodecFactory.java

示例6: byteBufRelativeAccess

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
/**
 * Listing 5.6
 */
public static void byteBufRelativeAccess(ByteBuf buffer) {
    for (int i = 0; i < buffer.capacity(); i++) {
        byte b = buffer.getByte(i);
        System.out.println((char) b);
    }

}
 
开发者ID:zy416548283,项目名称:NettyStudy,代码行数:11,代码来源:ByteBufExamples.java

示例7: write

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
/**
 * 复制数据
 *
 * @param data
 *         序列化后的数据
 * @param out
 *         回传的数据
 * @see JSFProtocol
 */
private void write(ByteBuf data, ByteBuf out) {
    int totalLength = 2 + 4 + data.readableBytes();
    if(out.capacity() < totalLength) out.capacity(totalLength);
    out.writeBytes(Constants.MAGICCODEBYTE); // 写入magiccode
    int length = totalLength - 2 ; //  data.readableBytes() + 4  (4指的是FULLLENGTH)
    out.writeInt(length);   //4 for Length Field
    out.writeBytes(data, data.readerIndex(), data.readableBytes());
    //logger.trace("out length:{}",out.readableBytes());
}
 
开发者ID:tiglabs,项目名称:jsf-sdk,代码行数:19,代码来源:JSFEncoder.java

示例8: channelRead0

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
public void channelRead0(final ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception {
    webSocketServerThread.log(Level.FINEST, "channel read, frame="+frame);
    // TODO: log at INFO level if this the first data we received from a client (new first connection), to
    // help detect clients connecting but not sending authentication commands (in newPlayer)

    if (this.checkIPBans) {
        String ip = webSocketServerThread.getRemoteIP(ctx.channel());
        if (this.ipBans.contains(ip)) {
            webSocketServerThread.sendLine(ctx.channel(), "T,Banned from server"); // TODO: show reason, getBanList
            return;
        }
    }

    if (frame instanceof BinaryWebSocketFrame) {
        ByteBuf content = frame.content();

        byte[] bytes = new byte[content.capacity()];
        content.getBytes(0, bytes);

        final String string = new String(bytes);
        webSocketServerThread.log(Level.FINEST, "received "+content.capacity()+" bytes: "+string);

        this.webSocketServerThread.scheduleSyncTask(new Runnable() {
            @Override
            public void run() {
                webSocketServerThread.handle(string, ctx);
            }
        });
    } else {
        String message = "unsupported frame type: " + frame.getClass().getName();
        throw new UnsupportedOperationException(message);
    }
}
 
开发者ID:satoshinm,项目名称:WebSandboxMC,代码行数:35,代码来源:WebSocketFrameHandler.java

示例9: writeToStream

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
public void writeToStream(FSDataOutputStream stream) throws IOException {
  Stopwatch watch = Stopwatch.createStarted();
  ByteBuf buf = null;
  try {
    check = ThreadLocalRandom.current().nextLong();
    start = stream.getPos();
    logger.debug("Writing check value {} at position {}", check, start);
    stream.writeLong(check);
    batch.getHeader().writeDelimitedTo(stream);
    buf = batch.getBody();
    if (buf != null) {
      bodyLength = buf.capacity();
    } else {
      bodyLength = 0;
    }
    if (bodyLength > 0) {
      buf.getBytes(0, stream, bodyLength);
    }
    stream.hsync();
    FileStatus status = spillFile.getFileStatus();
    long len = status.getLen();
    logger.debug("After spooling batch, stream at position {}. File length {}", stream.getPos(), len);
    long t = watch.elapsed(TimeUnit.MICROSECONDS);
    logger.debug("Took {} us to spool {} to disk. Rate {} mb/s", t, bodyLength, bodyLength / t);
  } finally {
    // even if the try block throws an exception we still want to send an ACK and release the lock
    // the caller will add the exception to deferred attribute and it will be thrown when the poll() method is called
    try {
      batch.sendOk(); // this can also throw an exception
    } finally {
      state = BatchState.SPILLED;
      batch = null;
      if (buf != null) {
        buf.release();
      }
    }
  }
}
 
开发者ID:dremio,项目名称:dremio-oss,代码行数:39,代码来源:SpoolingRawBatchBuffer.java

示例10: decode

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws Exception {

    try {

        logger.debug("Protobuf decode started.");
        in.markReaderIndex();
        if (in.readableBytes() < 4) {
            logger.info("Readable Bytes length less than 4 bytes, ignored");
            in.resetReaderIndex();
            return;
        }

        DataBuffer dataBuf = new DataBuffer(in);

        IMHeader header = new IMHeader();
        header.decode(dataBuf);

        if (header.getLength() < 0) {
            ctx.close();
            logger.error("message length less than 0, channel closed");
            return;
        }

        ByteBuf byteBuf = ctx.alloc().buffer(header.getLength() - SysConstant.PROTOCOL_HEADER_LENGTH);

        in.readBytes(byteBuf);
        byte[] body;
        if (byteBuf.hasArray()) {
            body = byteBuf.array();
        } else {
            body = new byte[byteBuf.capacity()];
            byteBuf.readBytes(body);
        }

        MessageLite msg = ProtobufParseMap.getMessage(header.getServiceId(), header.getCommandId(), body);

        IMProtoMessage<MessageLite> protoMessage = new IMProtoMessage<>(header, msg);
        out.add(protoMessage);
        
        logger.debug("Received protobuf : length={}, commandId={}", header.getLength(), header.getCommandId());
    } catch (Exception e) {
        logger.error(ctx.channel().remoteAddress() + ",decode failed.", e);
    } finally {
        logger.debug("Protobuf decode finished.");
    }
}
 
开发者ID:ccfish86,项目名称:sctalk,代码行数:48,代码来源:PacketDecoder.java

示例11: encode

import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override public void encode(ByteBuf buffer) {
    ByteBuf buf = Unpooled.buffer();
    encodables.forEach(encodable -> encodable.encode(buf));

    int index = buf.writerIndex();

    buf.capacity(index);

    new Data(buf.array()).encode(buffer);
}
 
开发者ID:PumpkinDB,项目名称:pumpkindb-java,代码行数:11,代码来源:Closure.java


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