下面列出了java.nio.channels.NotYetConnectedException#io.netty.buffer.ByteBuf 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public ByteBuf getRequest() {
Pair<String, Long> requestInfo = getRequestMasterInfo();
replIdRequest = requestInfo.getKey();
offsetRequest = requestInfo.getValue();
if (replIdRequest == null) {
replIdRequest = "?";
offsetRequest = -1;
}
RequestStringParser requestString = new RequestStringParser(getName(), replIdRequest,
String.valueOf(offsetRequest));
if (getLogger().isDebugEnabled()) {
getLogger().debug("[doRequest]{}, {}", this, StringUtil.join(" ", requestString.getPayload()));
}
return requestString.format();
}
@Override
void receiveIndividualMessagesFromBatch(MessageMetadata msgMetadata, int redeliveryCount,
List<Long> ackSet,
ByteBuf uncompressedPayload, MessageIdData messageId, ClientCnx cnx) {
log.warn(
"Closing consumer [{}]-[{}] due to unsupported received batch-message with zero receiver queue size",
subscription, consumerName);
// close connection
closeAsync().handle((ok, e) -> {
// notify callback with failure result
notifyPendingReceivedCallback(null,
new PulsarClientException.InvalidMessageException(
format("Unsupported Batch message with 0 size receiver queue for [%s]-[%s] ",
subscription, consumerName)));
return null;
});
}
@Test
void requestResponseData() {
ByteBuf request =
RequestResponseFrameCodec.encode(
ByteBufAllocator.DEFAULT,
1,
false,
null,
Unpooled.copiedBuffer("d", StandardCharsets.UTF_8));
String data = RequestResponseFrameCodec.data(request).toString(StandardCharsets.UTF_8);
ByteBuf metadata = RequestResponseFrameCodec.metadata(request);
assertFalse(FrameHeaderCodec.hasMetadata(request));
assertEquals("d", data);
assertNull(metadata);
request.release();
}
private ByteBuf buildCompressedFrame(byte[] payload, int compressionLevel) {
final Deflater deflater = new Deflater(compressionLevel);
deflater.setInput(payload);
deflater.finish();
final byte[] compressedPayload = new byte[1024];
final int compressedPayloadLength = deflater.deflate(compressedPayload);
deflater.end();
final ByteBuf buffer = Unpooled.buffer(6 + compressedPayloadLength);
buffer.writeByte('2');
buffer.writeByte('C');
// Compressed payload length
buffer.writeInt(compressedPayloadLength);
// Compressed payload
buffer.writeBytes(compressedPayload, 0, compressedPayloadLength);
return buffer;
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> list) throws Exception {
try {
int readAble = in.readableBytes();
if (readAble < headSize) {
return;
}
in.markReaderIndex();
int size = in.readInt();
if (readAble < (size + headSize)) {
in.resetReaderIndex();
return;
}
// FIXME wjm 最好改了
try (ObjectInputStream oin = new ObjectInputStream(new ByteBufInputStream(in.readBytes(size), true))) {
list.add(oin.readObject());
}
} catch (Throwable e) {
logger.error("decode error ", e);
}
}
void protectFlush(
List<ByteBuf> unprotectedBufs, Consumer<ByteBuf> ctxWrite, ByteBufAllocator alloc)
throws GeneralSecurityException {
checkState(crypter != null, "Cannot protectFlush after destroy.");
ByteBuf protectedBuf;
try {
protectedBuf = handleUnprotected(unprotectedBufs, alloc);
} finally {
for (ByteBuf buf : unprotectedBufs) {
buf.release();
}
}
if (protectedBuf != null) {
ctxWrite.accept(protectedBuf);
}
}
@Override
public void onConnected(Ukcp ukcp) {
future = scheduleSrv.scheduleWithFixedDelay(() -> {
ByteBuf byteBuf = rttMsg(++count);
ukcp.writeOrderedReliableMessage(byteBuf);
byteBuf.release();
if (count >= rtts.length) {
// finish
future.cancel(true);
byteBuf = rttMsg(-1);
ukcp.writeOrderedReliableMessage(byteBuf);
byteBuf.release();
}
}, 20, 20, TimeUnit.MILLISECONDS);
}
@Override
public ByteBuf write(ByteBuf buffer) {
buffer.writeBytes(brokerID);
buffer.writeBytes(investorID);
buffer.writeInt(orderActionRef);
buffer.writeBytes(orderRef);
buffer.writeInt(requestID);
buffer.writeInt(frontID);
buffer.writeInt(sessionID);
buffer.writeBytes(exchangeID);
buffer.writeBytes(orderSysID);
buffer.writeBytes(actionFlag);
buffer.writeDouble(limitPrice);
buffer.writeInt(volumeChange);
buffer.writeBytes(userID);
buffer.writeBytes(instrumentID);
buffer.writeBytes(investUnitID);
buffer.writeBytes(iPAddress);
buffer.writeBytes(macAddress);
return buffer;
}
@Test
public void testGetType() {
final AbstractTwoOctetAsExtendedCommunity abstractTwoOctetAsExtendedCommunity =
new AbstractTwoOctetAsExtendedCommunity() {
@Override
public void serializeExtendedCommunity(final ExtendedCommunity extendedCommunity,
final ByteBuf byteAggregator) {
}
@Override
public int getSubType() {
return 0;
}
@Override
public ExtendedCommunity parseExtendedCommunity(final ByteBuf buffer) {
return null;
}
};
Assert.assertEquals(0, abstractTwoOctetAsExtendedCommunity.getType(true));
Assert.assertEquals(64, abstractTwoOctetAsExtendedCommunity.getType(false));
}
public static boolean readUntil(char what, @Nonnull ByteBuf buffer, @Nonnull StringBuilder builder) {
int i = buffer.readerIndex();
//noinspection ForLoopThatDoesntUseLoopVariable
for (int n = buffer.writerIndex(); i < n; i++) {
char c = (char)buffer.getByte(i);
if (c == what) {
buffer.readerIndex(i + 1);
return true;
}
else {
builder.append(c);
}
}
buffer.readerIndex(i);
return false;
}
private static void assertWrite(ChannelHandler handler, int count) {
final ByteBuf buffer = Unpooled.copiedBuffer("Test", CharsetUtil.US_ASCII);
final EmbeddedChannel channel = new EmbeddedChannel(handler);
channel.config().setWriteBufferLowWaterMark(1);
channel.config().setWriteBufferHighWaterMark(3);
ByteBuf[] buffers = new ByteBuf[count];
for (int i = 0; i < buffers.length; i++) {
buffers[i] = buffer.retainedDuplicate();
}
assertTrue(channel.writeOutbound(buffers));
assertTrue(channel.finish());
channel.closeFuture().syncUninterruptibly();
for (int i = 0; i < buffers.length; i++) {
assertBuffer(channel, buffer);
}
buffer.release();
assertNull(channel.readOutbound());
}
@Override
public void serialize(ByteBuf buffer, AddPlayerPacket packet) {
BedrockUtils.writeUuid(buffer, packet.getUuid());
BedrockUtils.writeString(buffer, packet.getUsername());
VarInts.writeLong(buffer, packet.getUniqueEntityId());
VarInts.writeUnsignedLong(buffer, packet.getRuntimeEntityId());
BedrockUtils.writeString(buffer, packet.getPlatformChatId());
BedrockUtils.writeVector3f(buffer, packet.getPosition());
BedrockUtils.writeVector3f(buffer, packet.getMotion());
BedrockUtils.writeVector3f(buffer, packet.getRotation());
BedrockUtils.writeItemData(buffer, packet.getHand());
BedrockUtils.writeEntityData(buffer, packet.getMetadata());
AdventureSettingsSerializer_v361.INSTANCE.serialize(buffer, packet.getAdventureSettings());
BedrockUtils.writeArray(buffer, packet.getEntityLinks(), BedrockUtils::writeEntityLink);
BedrockUtils.writeString(buffer, packet.getDeviceId());
}
@Override
public CompletableFuture<Void> readFully(long offset, ByteBuf dst, int dstOffset, int len) {
if(len == 0) {
throw new IllegalArgumentException("Empty reads not allowed.");
}
logger.debug("Starting read of {}.{} for range {}..{}", bucket, path, offset, offset + len);
Stopwatch w = Stopwatch.createStarted();
numOutstandingReads.incrementAndGet();
CompletableFuture<Void> future = client.getObject(
GetObjectRequest.builder()
.range(range(offset, len))
.bucket(bucket)
.key(path)
.build(),
new ByteRangeReader(dst, dstOffset));
return future.whenComplete((a,b) -> {
int numOutstanding = numOutstandingReads.decrementAndGet();
if (b == null) {
// no exception
logger.debug("Finished read of {}.{} for range {}..{} in {}ms.", bucket, path, offset, offset + len, w.elapsed(TimeUnit.MILLISECONDS));
return;
}
// exception
logger.warn("Async read of {}.{} for length {} failed in {}ms when there are {} outstanding reads. Error {}", bucket, path, len, w.elapsed(TimeUnit.MILLISECONDS), numOutstanding, b);
});
}
@Override
public Collection<ByteBuf> toData(ClientStatus packet) {
if (packet.getPayload() == 1) {
ByteBuf data = Allocator.allocateBuffer();
data.writeByte(LegacyPacketId.Serverbound.LOGIN_PLAY_CLIENT_COMMAND);
data.writeByte(packet.getPayload());
return Collections.singletonList(data);
} else {
return Collections.emptyList();
}
}
@Override
public ByteBuf getContent() {
List<String> tags = new ArrayList<>();
tags.add(assembleRoutingKey());
//method included in routing key
/*if (method != null && !method.isEmpty()) {
tags.add("m=" + method);
}*/
if (endpoint != null && !endpoint.isEmpty()) {
tags.add("e=" + endpoint);
}
return TaggingMetadataFlyweight.createTaggingContent(PooledByteBufAllocator.DEFAULT, tags);
}
private ByteBuf encodeWriteSingleCoil(WriteSingleCoilResponse response, ByteBuf buffer) {
buffer.writeByte(response.getFunctionCode().getCode());
buffer.writeShort(response.getAddress());
buffer.writeShort(response.getValue());
return buffer;
}
@Override
public void serialize(ByteBuf buffer, AddEntityPacket packet) {
VarInts.writeLong(buffer, packet.getUniqueEntityId());
VarInts.writeUnsignedLong(buffer, packet.getRuntimeEntityId());
BedrockUtils.writeString(buffer, packet.getIdentifier());
BedrockUtils.writeVector3f(buffer, packet.getPosition());
BedrockUtils.writeVector3f(buffer, packet.getMotion());
BedrockUtils.writeVector3f(buffer, packet.getRotation());
BedrockUtils.writeArray(buffer, packet.getAttributes(), BedrockUtils::writeEntityAttribute);
BedrockUtils.writeEntityData(buffer, packet.getMetadata());
BedrockUtils.writeArray(buffer, packet.getEntityLinks(), BedrockUtils::writeEntityLink);
}
public final ByteBuf encodePayload(@NotNull ByteBufAllocator allocator, @NotNull List<ByteBuf> args) {
ByteBuf payload = CodecUtils.writeArgs(allocator, encodeHeader(allocator), args);
if (args.isEmpty()) {
this.flags = 0;
payload.setByte(0, 0);
} else {
this.flags = 1;
payload.setByte(0, 1);
}
this.payload = payload;
return payload;
}
/**
* Get expected size encoded as offset + 4 bytes of data.
*/
private int getSize(final byte[] data, final int offset) {
if (data.length - offset < INT_SIZE) {
return 0;
}
final ByteBuf intBuffer = Unpooled.wrappedBuffer(data, offset, INT_SIZE).order(Unpooled.LITTLE_ENDIAN);
final int ret = intBuffer.readInt();
intBuffer.release();
return ret;
}
private ByteBuf mockJsonObjectByteBufValue(final List<JsonEntry> jsonEntries, final boolean isSmall) {
ByteBuf result = Unpooled.buffer();
writeInt(result, jsonEntries.size(), isSmall);
writeInt(result, 0, isSmall);
int startOffset = isSmall
? 1 + SMALL_JSON_INT_LENGTH + SMALL_JSON_INT_LENGTH + jsonEntries.size() * SMALL_JSON_KEY_META_DATA_LENGTH + jsonEntries.size() * SMALL_JSON_VALUE_META_DATA_LENGTH - 1
: 1 + LARGE_JSON_INT_LENGTH + LARGE_JSON_INT_LENGTH + jsonEntries.size() * LARGE_JSON_KEY_META_DATA_LENGTH + jsonEntries.size() * LARGE_JSON_VALUE_META_DATA_LENGTH - 1;
ByteBuf keyByteBuf = writeKeys(result, jsonEntries, startOffset, isSmall);
startOffset += keyByteBuf.readableBytes();
ByteBuf valueByteBuf = writeValues(result, jsonEntries, startOffset, isSmall);
result.writeBytes(keyByteBuf);
result.writeBytes(valueByteBuf);
return result;
}
@Test
public void testRemaining() {
ByteBuf buffer = Unpooled.buffer(1024);
NettyWritable writable = new NettyWritable(buffer);
assertEquals(buffer.maxCapacity(), writable.remaining());
writable.put((byte) 0);
assertEquals(buffer.maxCapacity() - 1, writable.remaining());
}
/**
* Load the field value or file data from a Multipart request
*
* @return {@code true} if the last chunk is loaded (boundary delimiter found),
* {@code false} if need more chunks
* @throws ErrorDataDecoderException
*/
private static boolean loadDataMultipartStandard(ByteBuf undecodedChunk, String delimiter, HttpData httpData) {
final int startReaderIndex = undecodedChunk.readerIndex();
final int delimeterLength = delimiter.length();
int index = 0;
int lastPosition = startReaderIndex;
byte prevByte = HttpConstants.LF;
boolean delimiterFound = false;
while (undecodedChunk.isReadable()) {
final byte nextByte = undecodedChunk.readByte();
// Check the delimiter
if (prevByte == HttpConstants.LF && nextByte == delimiter.codePointAt(index)) {
index++;
if (delimeterLength == index) {
delimiterFound = true;
break;
}
continue;
}
lastPosition = undecodedChunk.readerIndex();
if (nextByte == HttpConstants.LF) {
index = 0;
lastPosition -= (prevByte == HttpConstants.CR) ? 2 : 1;
}
prevByte = nextByte;
}
if (prevByte == HttpConstants.CR) {
lastPosition--;
}
ByteBuf content = undecodedChunk.copy(startReaderIndex, lastPosition - startReaderIndex);
try {
httpData.addContent(content, delimiterFound);
} catch (IOException e) {
throw new ErrorDataDecoderException(e);
}
undecodedChunk.readerIndex(lastPosition);
return delimiterFound;
}
/**
* Helper method to return the failover logs for the given partitions (vbids).
* <p>
* If the list is empty, the failover logs for all partitions will be returned. Note that the returned
* ByteBufs can be analyzed using the {@link DcpFailoverLogResponse} flyweight.
*
* @param vbids the partitions to return the failover logs from.
* @return an {@link Observable} containing all failover logs.
*/
public Observable<ByteBuf> failoverLogs(Short... vbids) {
List<Short> partitions = partitionsForVbids(numPartitions(), vbids);
LOGGER.debug("Asking for failover logs on partitions {}", partitions);
return Observable
.from(partitions)
.flatMapSingle(new Func1<Short, Single<ByteBuf>>() {
@Override
public Single<ByteBuf> call(Short p) {
return conductor.getFailoverLog(p);
}
});
}
@Override
public CNextHop parseNextHop(final ByteBuf buffer) throws BGPParsingException {
Preconditions.checkArgument(buffer.readableBytes() == (this.ipAddrLength + RouteDistinguisherUtil.RD_LENGTH),
"Length of byte array for NEXT_HOP should be %s, but is %s",
this.ipAddrLength + RouteDistinguisherUtil.RD_LENGTH, buffer.readableBytes());
buffer.readBytes(RouteDistinguisherUtil.RD_LENGTH);
return NextHopUtil.parseNextHop(buffer.readBytes(this.ipAddrLength));
}
private static MessageId readId(final ByteBuf data, final int size) {
if (size != 16) {
throw new IllegalArgumentException();
}
final long seq = data.readLong();
final long timestamp = data.readLong();
return MessageId.from(seq, timestamp);
}
@SuppressWarnings("fallthrough")
private void decodeFrame(ByteBufAllocator alloc, List<Object> out)
throws GeneralSecurityException {
switch (state) {
case READ_HEADER:
if (unhandledBytes < HEADER_BYTES) {
return;
}
handleHeader();
// fall through
case READ_PROTECTED_PAYLOAD:
if (unhandledBytes < requiredProtectedBytes) {
return;
}
ByteBuf unprotectedBuf;
try {
unprotectedBuf = handlePayload(alloc);
} finally {
clearState();
}
if (unprotectedBuf != null) {
out.add(unprotectedBuf);
}
break;
default:
throw new AssertionError("impossible enum value");
}
}
@Test
public void testReadHoldingRegistersResponse() {
ReadHoldingRegistersResponse response = new ReadHoldingRegistersResponse(Unpooled.buffer().writeByte(1).writeByte(2));
response.retain().content().markReaderIndex();
ByteBuf encoded = encoder.encode(response, Unpooled.buffer());
ReadHoldingRegistersResponse decoded = (ReadHoldingRegistersResponse) decoder.decode(encoded);
response.content().resetReaderIndex();
assertEquals(response.getFunctionCode(), decoded.getFunctionCode());
assertEquals(response.getRegisters(), decoded.getRegisters());
}
default void readFully(byte[] dst, int dstOffset, int dstLen) throws IOException {
final ByteBuf buf1 = Unpooled.buffer(dstLen);
try {
readFully(buf1, dstLen);
buf1.getBytes(0, dst, dstOffset, dstLen);
} finally {
buf1.release();
}
}
@Override
protected String format(ChannelHandlerContext ctx, String eventName, Object arg) {
if (arg instanceof ByteBuf) {
return formatDcpPacket(ctx, eventName, (ByteBuf) arg);
} else if (arg instanceof ByteBufHolder) {
return formatDcpPacket(ctx, eventName, ((ByteBufHolder) arg).content());
} else {
return super.format(ctx, eventName, arg);
}
}
private ConsumeManageRequest deserialize(ByteBuf buf) {
String subject = PayloadHolderUtils.readString(buf);
String consumerGroup = PayloadHolderUtils.readString(buf);
int code = buf.readInt();
ConsumeManageRequest request = new ConsumeManageRequest();
request.setSubject(subject);
request.setGroup(consumerGroup);
request.setConsumerFromWhere(code);
return request;
}