io.netty.buffer.ByteBuf.writableBytes()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(199)

本文整理了Java中io.netty.buffer.ByteBuf.writableBytes()方法的一些代码示例,展示了ByteBuf.writableBytes()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuf.writableBytes()方法的具体详情如下:
包路径:io.netty.buffer.ByteBuf
类名称:ByteBuf
方法名:writableBytes

ByteBuf.writableBytes介绍

[英]Returns the number of writable bytes which is equal to (this.capacity - this.writerIndex).
[中]返回等于(this.capacity-this.writerIndex)的可写字节数。

代码示例

代码示例来源:origin: netty/netty

@Override
public ByteBuf readBytes(ByteBuf dst, int length) {
  if (checkBounds) {
    if (length > dst.writableBytes()) {
      throw new IndexOutOfBoundsException(String.format(
          "length(%d) exceeds dst.writableBytes(%d) where dst is: %s", length, dst.writableBytes(), dst));
    }
  }
  readBytes(dst, dst.writerIndex(), length);
  dst.writerIndex(dst.writerIndex() + length);
  return this;
}

代码示例来源:origin: netty/netty

private void deflate(ByteBuf out) {
  int numBytes;
  do {
    int writerIndex = out.writerIndex();
    numBytes = deflater.deflate(
        out.array(), out.arrayOffset() + writerIndex, out.writableBytes(), Deflater.SYNC_FLUSH);
    out.writerIndex(writerIndex + numBytes);
  } while (numBytes > 0);
}

代码示例来源:origin: redisson/redisson

@Override
public ByteBuf readBytes(ByteBuf dst, int length) {
  if (checkBounds) {
    if (length > dst.writableBytes()) {
      throw new IndexOutOfBoundsException(String.format(
          "length(%d) exceeds dst.writableBytes(%d) where dst is: %s", length, dst.writableBytes(), dst));
    }
  }
  readBytes(dst, dst.writerIndex(), length);
  dst.writerIndex(dst.writerIndex() + length);
  return this;
}

代码示例来源:origin: redisson/redisson

private void deflate(ByteBuf out) {
  int numBytes;
  do {
    int writerIndex = out.writerIndex();
    numBytes = deflater.deflate(
        out.array(), out.arrayOffset() + writerIndex, out.writableBytes(), Deflater.SYNC_FLUSH);
    out.writerIndex(writerIndex + numBytes);
  } while (numBytes > 0);
}

代码示例来源:origin: wildfly/wildfly

@Override
public ByteBuf readBytes(ByteBuf dst, int length) {
  if (length > dst.writableBytes()) {
    throw new IndexOutOfBoundsException(String.format(
        "length(%d) exceeds dst.writableBytes(%d) where dst is: %s", length, dst.writableBytes(), dst));
  }
  readBytes(dst, dst.writerIndex(), length);
  dst.writerIndex(dst.writerIndex() + length);
  return this;
}

代码示例来源:origin: line/armeria

@Override
protected int read(ByteChannel src, ByteBuf dst) throws IOException {
  if (src instanceof ScatteringByteChannel) {
    return dst.writeBytes((ScatteringByteChannel) src, dst.writableBytes());
  }
  final int readBytes = src.read(dst.nioBuffer(dst.writerIndex(), dst.writableBytes()));
  if (readBytes > 0) {
    dst.writerIndex(dst.writerIndex() + readBytes);
  }
  return readBytes;
}

代码示例来源:origin: apache/pulsar

private static void deflate(Deflater deflater, ByteBuf out) {
  int numBytes;
  do {
    int writerIndex = out.writerIndex();
    numBytes = deflater.deflate(out.array(), out.arrayOffset() + writerIndex, out.writableBytes(),
        Deflater.SYNC_FLUSH);
    out.writerIndex(writerIndex + numBytes);
  } while (numBytes > 0);
}

代码示例来源:origin: org.mongodb/mongo-java-driver

@Override
public ByteBuffer asNIO() {
  if (isWriting) {
    return proxied.nioBuffer(proxied.writerIndex(), proxied.writableBytes());
  } else {
    return proxied.nioBuffer(proxied.readerIndex(), proxied.readableBytes());
  }
}

代码示例来源:origin: wildfly/wildfly

private boolean compressInto(ByteBuf compressed) {
  byte[] out = compressed.array();
  int off = compressed.arrayOffset() + compressed.writerIndex();
  int toWrite = compressed.writableBytes();
  int numBytes = compressor.deflate(out, off, toWrite, Deflater.SYNC_FLUSH);
  compressed.writerIndex(compressed.writerIndex() + numBytes);
  return numBytes == toWrite;
}

代码示例来源:origin: wildfly/wildfly

private void deflate(ByteBuf out) {
  int numBytes;
  do {
    int writerIndex = out.writerIndex();
    numBytes = deflater.deflate(
        out.array(), out.arrayOffset() + writerIndex, out.writableBytes(), Deflater.SYNC_FLUSH);
    out.writerIndex(writerIndex + numBytes);
  } while (numBytes > 0);
}

代码示例来源:origin: traccar/traccar

private void requestPhotoFragment(Channel channel) {
  if (channel != null) {
    int offset = photo.writerIndex();
    int size = Math.min(photo.writableBytes(), MAX_CHUNK_SIZE);
    channel.writeAndFlush(new NetworkMessage("#PHD" + offset + "," + size + "\r\n", channel.remoteAddress()));
  }
}

代码示例来源:origin: traccar/traccar

private void sendPhotoRequest(Channel channel, int pictureId) {
  ByteBuf photo = photos.get(pictureId);
  ByteBuf content = Unpooled.buffer();
  content.writeInt(pictureId);
  content.writeInt(photo.writerIndex());
  content.writeShort(Math.min(photo.writableBytes(), 1024));
  sendResponse(channel, false, MSG_X1_PHOTO_DATA, 0, content);
}

代码示例来源:origin: fengjiachun/Jupiter

private static ByteBuffer newNioByteBuffer(ByteBuf byteBuf, int writableBytes) {
    return byteBuf
        .ensureWritable(writableBytes)
        .nioBuffer(byteBuf.writerIndex(), byteBuf.writableBytes());
  }
}

代码示例来源:origin: fengjiachun/Jupiter

private static ByteBuffer newNioByteBuffer(ByteBuf byteBuf, int writableBytes) {
    return byteBuf
        .ensureWritable(writableBytes)
        .nioBuffer(byteBuf.writerIndex(), byteBuf.writableBytes());
  }
}

代码示例来源:origin: fengjiachun/Jupiter

private static ByteBuffer newNioByteBuffer(ByteBuf byteBuf, int writableBytes) {
    return byteBuf
        .ensureWritable(writableBytes)
        .nioBuffer(byteBuf.writerIndex(), byteBuf.writableBytes());
  }
}

代码示例来源:origin: apache/drill

public static String bufferState(final ByteBuf buf) {
 final int cap = buf.capacity();
 final int mcap = buf.maxCapacity();
 final int ri = buf.readerIndex();
 final int rb = buf.readableBytes();
 final int wi = buf.writerIndex();
 final int wb = buf.writableBytes();
 return String.format("cap/max: %d/%d, ri: %d, rb: %d, wi: %d, wb: %d",
   cap, mcap, ri, rb, wi, wb);
}

代码示例来源:origin: redisson/redisson

@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();
    }
  }
};

代码示例来源:origin: redisson/redisson

@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();
    }
  }
};

代码示例来源:origin: redisson/redisson

@Override
SSLEngineResult unwrap(SslHandler handler, ByteBuf in, int readerIndex, int len, ByteBuf out)
    throws SSLException {
  int nioBufferCount = in.nioBufferCount();
  int writerIndex = out.writerIndex();
  final SSLEngineResult result;
  if (nioBufferCount > 1) {
    /*
     * Use a special unwrap method without additional memory copies.
     */
    try {
      handler.singleBuffer[0] = toByteBuffer(out, writerIndex, out.writableBytes());
      result = ((ConscryptAlpnSslEngine) handler.engine).unwrap(
          in.nioBuffers(readerIndex, len),
          handler.singleBuffer);
    } finally {
      handler.singleBuffer[0] = null;
    }
  } else {
    result = handler.engine.unwrap(toByteBuffer(in, readerIndex, len),
        toByteBuffer(out, writerIndex, out.writableBytes()));
  }
  out.writerIndex(writerIndex + result.bytesProduced());
  return result;
}

代码示例来源:origin: redisson/redisson

@Override
SSLEngineResult unwrap(SslHandler handler, ByteBuf in, int readerIndex, int len, ByteBuf out)
    throws SSLException {
  int nioBufferCount = in.nioBufferCount();
  int writerIndex = out.writerIndex();
  final SSLEngineResult result;
  if (nioBufferCount > 1) {
    /*
     * If {@link OpenSslEngine} is in use,
     * we can use a special {@link OpenSslEngine#unwrap(ByteBuffer[], ByteBuffer[])} method
     * that accepts multiple {@link ByteBuffer}s without additional memory copies.
     */
    ReferenceCountedOpenSslEngine opensslEngine = (ReferenceCountedOpenSslEngine) handler.engine;
    try {
      handler.singleBuffer[0] = toByteBuffer(out, writerIndex,
        out.writableBytes());
      result = opensslEngine.unwrap(in.nioBuffers(readerIndex, len), handler.singleBuffer);
    } finally {
      handler.singleBuffer[0] = null;
    }
  } else {
    result = handler.engine.unwrap(toByteBuffer(in, readerIndex, len),
      toByteBuffer(out, writerIndex, out.writableBytes()));
  }
  out.writerIndex(writerIndex + result.bytesProduced());
  return result;
}

相关文章

ByteBuf类方法