本文整理汇总了Java中io.netty.buffer.ByteBuf.readBytes方法的典型用法代码示例。如果您正苦于以下问题:Java ByteBuf.readBytes方法的具体用法?Java ByteBuf.readBytes怎么用?Java ByteBuf.readBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.buffer.ByteBuf
的用法示例。
在下文中一共展示了ByteBuf.readBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (!hasHeader) {
if (in.readableBytes() >= 7) {
short id = in.readShort();
int payloadLength = in.readMedium();
short version = in.readShort();
hasHeader = true;
header.id = id;
header.payload = new byte[payloadLength];
}
} else {
if (in.readableBytes() >= header.payload.length) {
in.readBytes(header.payload);
hasHeader = false;
crypto.decryptPacket(header);
ClientMessage message = processPacket();
if (message != null) {
out.add(message);
}
}
}
}
示例2: channelRead0
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception {
while (true) {
if (byteBuf.readableBytes() < FrameSetting.FRAME_HEAD_LENGTH) {
return;
}
if (byteBuf.readByte() != FrameSetting.MAJOR_FRAME_HEAD_1
|| byteBuf.readByte() != FrameSetting.MAJOR_FRAME_HEAD_2) {
return;
}
int groupId = byteBuf.readByte() & 0xFF;
int msgId = byteBuf.readByte() & 0xFF;
int deviceId = byteBuf.readByte() & 0xFF;
int backupMsg = byteBuf.readByte() & 0xFF;
int dataLength = byteBuf.readShort() & 0xFF;
FrameMajorHeader headMsg = new FrameMajorHeader(msgId, groupId, deviceId, dataLength, backupMsg);
ByteBuf subBuf = ctx.alloc().buffer(dataLength);
byteBuf.readBytes(subBuf, dataLength);
ctx.fireChannelRead(new FrameMajor(headMsg, subBuf));
}
}
示例3: decode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
if (in.readableBytes() < AbstractMessageDecoder.MESSAGE_LENGTH) {
return;
}
in.markReaderIndex();
int messageLength = in.readInt();
if (messageLength < 0) {
ctx.close();
}
if (in.readableBytes() < messageLength) {
in.resetReaderIndex();
} else {
byte[] messageBody = new byte[messageLength];
in.readBytes(messageBody);
try {
Object obj = util.decode(messageBody);
out.add(obj);
} catch (IOException ex) {
Logger.getLogger(AbstractMessageDecoder.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
示例4: decode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (in.readableBytes() < 4) {
return;
}
in.markReaderIndex();
// System.out.println(in.order() + "in.readableBytes()" + in.readableBytes());
// byte[] bs = new byte[in.readableBytes()];
// in.readBytes(bs);
// String fullHexDump = ByteUtils.fullHexDump(bs);
// System.out.println(fullHexDump);
// System.out.println(new String(bs));
// in.resetReaderIndex();
// in.markReaderIndex();
// byte[] bs=new byte[in.capacity()];
// in.readBytes(bs);
//// System.out.println(in.readIntLE());
// ByteUtils.fullHexDump(bs);
// in.resetReaderIndex();
int dataLength = in.readInt();
// System.out.println(dataLength + "======");
if (in.readableBytes() < dataLength) {
in.resetReaderIndex();
return;
}
int opcode = in.readInt();
// System.out.println("opcode=" + opcode);
byte[] decoded = new byte[dataLength - 4];
in.readBytes(decoded);
BaseByteArrayPacket messagePack = new BaseByteArrayPacket(opcode, decoded);
out.add(messagePack);
}
示例5: writePackets
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
private void writePackets(Packet[] packets) {
for (Packet packet : packets) {
packetQueue.remove(packet);
}
if (packets.length == 0) {
return;
}
writer.writePackets(packets);
byte[] batch = writer.getPacketBytes();
if (client.getProtocolEncryption() != null) {
batch = client.getProtocolEncryption().encryptInputForClient(batch);
}
ByteBuf buf = ByteBufAllocator.DEFAULT.directBuffer(batch.length + 1);
buf.writeByte((byte) PACKET_BATCH_ID);
buf.writeBytes(batch);
byte[] result = new byte[buf.readableBytes()];
buf.readBytes(result);
writer.reset();
log.trace("Sending batch packet");
client.getConnection().send(result);
}
示例6: readInetAddr
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
@Override
public InetAddress readInetAddr(ByteBuf source) {
int len = readByte(source);
byte[] addr = new byte[len];
source.readBytes(addr);
try {
return InetAddress.getByAddress(addr);
} catch (UnknownHostException uhe) {
throw new IllegalArgumentException(uhe);
}
}
示例7: extractSingleMapping
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
private Map<RtTag, byte[]> extractSingleMapping(ByteBuf msg) {
long uintTag = msg.readUnsignedInt();
RtTag tag = RtTag.fromUnsignedInt((int) uintTag);
byte[] value = new byte[msg.readableBytes()];
msg.readBytes(value);
return Collections.singletonMap(tag, value);
}
示例8: insertText
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
public static ByteBuf insertText(ByteBuf byteBuf, int index, String str) {
byte[] begin = new byte[index + 1];
byte[] end = new byte[byteBuf.readableBytes() - begin.length];
byteBuf.readBytes(begin);
byteBuf.readBytes(end);
byteBuf.writeBytes(begin);
byteBuf.writeBytes(stringToBytes(str));
byteBuf.writeBytes(end);
return byteBuf;
}
示例9: decodeString
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
private static Result<String> decodeString(ByteBuf buffer, int minBytes, int maxBytes) {
final Result<Integer> decodedSize = decodeMsbLsb(buffer);
int size = decodedSize.value;
int numberOfBytesConsumed = decodedSize.numberOfBytesConsumed;
if (size < minBytes || size > maxBytes) {
buffer.skipBytes(size);
numberOfBytesConsumed += size;
return new Result<>(null, numberOfBytesConsumed);
}
ByteBuf buf = buffer.readBytes(size);
numberOfBytesConsumed += size;
return new Result<>(buf.toString(CharsetUtil.UTF_8), numberOfBytesConsumed);
}
示例10: encode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
protected void encode(ChannelHandlerContext p_encode_1_, ByteBuf p_encode_2_, ByteBuf p_encode_3_) throws Exception
{
int i = p_encode_2_.readableBytes();
PacketBuffer packetbuffer = new PacketBuffer(p_encode_3_);
if (i < this.treshold)
{
packetbuffer.writeVarIntToBuffer(0);
packetbuffer.writeBytes(p_encode_2_);
}
else
{
byte[] abyte = new byte[i];
p_encode_2_.readBytes(abyte);
packetbuffer.writeVarIntToBuffer(abyte.length);
this.deflater.setInput(abyte, 0, i);
this.deflater.finish();
while (!this.deflater.finished())
{
int j = this.deflater.deflate(this.buffer);
packetbuffer.writeBytes((byte[])this.buffer, 0, j);
}
this.deflater.reset();
}
}
示例11: func_150502_a
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
private byte[] func_150502_a(ByteBuf p_150502_1_)
{
int i = p_150502_1_.readableBytes();
if (this.field_150505_b.length < i)
{
this.field_150505_b = new byte[i];
}
p_150502_1_.readBytes((byte[])this.field_150505_b, 0, i);
return this.field_150505_b;
}
示例12: encode
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
protected void encode(ChannelHandlerContext p_encode_1_, ByteBuf p_encode_2_, ByteBuf p_encode_3_) throws Exception
{
int i = p_encode_2_.readableBytes();
PacketBuffer packetbuffer = new PacketBuffer(p_encode_3_);
if (i < this.threshold)
{
packetbuffer.writeVarIntToBuffer(0);
packetbuffer.writeBytes(p_encode_2_);
}
else
{
byte[] abyte = new byte[i];
p_encode_2_.readBytes(abyte);
packetbuffer.writeVarIntToBuffer(abyte.length);
this.deflater.setInput(abyte, 0, i);
this.deflater.finish();
while (!this.deflater.finished())
{
int j = this.deflater.deflate(this.buffer);
packetbuffer.writeBytes((byte[])this.buffer, 0, j);
}
this.deflater.reset();
}
}
示例13: decodeMessage
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
/**
* We decode the message in the same context as the length decoding to better
* manage running out of memory. We decode the rpc header using heap memory
* (expected to always be available) so that we can propagate a error message
* with correct coordination id to sender rather than failing the channel.
*
* @param ctx The channel context.
* @param frame The Frame of the message we're processing.
* @param length The length of the frame.
* @throws Exception Code should only throw corrupt channel messages, causing the channel to close.
*/
private InboundRpcMessage decodeMessage(final ChannelHandlerContext ctx, final ByteBuf frame, final int length) throws Exception {
// now, we know the entire message is in the buffer and the buffer is constrained to this message. Additionally,
// this process should avoid reading beyond the end of this buffer so we inform the ByteBufInputStream to throw an
// exception if be go beyond readable bytes (as opposed to blocking).
final ByteBufInputStream is = new ByteBufInputStream(frame, length);
// read the rpc header, saved in delimited format.
checkTag(is, RpcEncoder.HEADER_TAG);
final RpcHeader header = RpcHeader.parseDelimitedFrom(is);
// read the protobuf body into a buffer.
checkTag(is, RpcEncoder.PROTOBUF_BODY_TAG);
final int pBodyLength = readRawVarint32(is);
final byte[] pBody = new byte[pBodyLength];
frame.readBytes(pBody);
ByteBuf dBody = null;
// read the data body.
if (frame.readableBytes() > 0) {
if (RpcConstants.EXTRA_DEBUGGING) {
logger.debug("Reading raw body, buffer has {} bytes available.", frame.readableBytes());
}
checkTag(is, RpcEncoder.RAW_BODY_TAG);
final int dBodyLength = readRawVarint32(is);
if (frame.readableBytes() != dBodyLength) {
throw new CorruptedFrameException(String.format("Expected to receive a raw body of %d bytes but received a buffer with %d bytes.", dBodyLength, frame.readableBytes()));
}
try {
dBody = allocator.buffer(dBodyLength);
// need to make buffer copy, otherwise netty will try to refill this buffer if we move the readerIndex forward...
// TODO: Can we avoid this copy?
dBody.writeBytes(frame, frame.readerIndex(), dBodyLength);
} catch (OutOfMemoryException e) {
sendOutOfMemory(e, ctx, header.getCoordinationId());
return null;
}
if (RpcConstants.EXTRA_DEBUGGING) {
logger.debug("Read raw body of length ", dBodyLength);
}
}else{
if (RpcConstants.EXTRA_DEBUGGING) {
logger.debug("No need to read raw body, no readable bytes left.");
}
}
InboundRpcMessage m = new InboundRpcMessage(header.getMode(), header.getRpcType(), header.getCoordinationId(),
pBody, dBody);
return m;
}
示例14: readUUID
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
public static UUID readUUID(ByteBuf cb)
{
byte[] bytes = new byte[16];
cb.readBytes(bytes);
return UUIDGen.getUUID(ByteBuffer.wrap(bytes));
}
示例15: receivePacket
import io.netty.buffer.ByteBuf; //导入方法依赖的package包/类
private boolean receivePacket() throws Exception {
DatagramPacket datagramPacket = this.socket.readPacket();
if (datagramPacket != null) {
// Check this early
try {
String source = datagramPacket.sender().getHostString();
currentSource = source; //in order to block address
if (this.block.containsKey(source)) {
return true;
}
if (this.ipSec.containsKey(source)) {
this.ipSec.put(source, this.ipSec.get(source) + 1);
} else {
this.ipSec.put(source, 1);
}
ByteBuf byteBuf = datagramPacket.content();
if (byteBuf.readableBytes() == 0) {
// Exit early to process another packet
return true;
}
byte[] buffer = new byte[byteBuf.readableBytes()];
byteBuf.readBytes(buffer);
int len = buffer.length;
int port = datagramPacket.sender().getPort();
this.receiveBytes += len;
byte pid = buffer[0];
if (pid == UNCONNECTED_PONG.ID) {
return false;
}
Packet packet = this.getPacketFromPool(pid);
if (packet != null) {
packet.buffer = buffer;
this.getSession(source, port).handlePacket(packet);
return true;
} else if (pid == UNCONNECTED_PING.ID) {
packet = new UNCONNECTED_PING();
packet.buffer = buffer;
packet.decode();
UNCONNECTED_PONG pk = new UNCONNECTED_PONG();
pk.serverID = this.getID();
pk.pingID = ((UNCONNECTED_PING) packet).pingID;
pk.serverName = this.getName();
this.sendPacket(pk, source, port);
} else if (buffer.length != 0) {
this.streamRAW(source, port, buffer);
return true;
} else {
return false;
}
} finally {
datagramPacket.release();
}
}
return false;
}