當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。