fix:数据写入

上级 77c126bf
...@@ -52,6 +52,7 @@ public class Server { ...@@ -52,6 +52,7 @@ public class Server {
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.info("3"); log.info("3");
log.info("h3结果{},class={}", msg, msg.getClass()); log.info("h3结果{},class={}", msg, msg.getClass());
//入栈处理器最后的写入,供出栈处理器读取
ch.writeAndFlush(ctx.alloc().buffer().writeBytes("server...".getBytes())); ch.writeAndFlush(ctx.alloc().buffer().writeBytes("server...".getBytes()));
} }
}); });
......
package com.kwan.shuyu.heima.netty_06_pipeline;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.channel.embedded.EmbeddedChannel;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.Charset;
/**
* netty提供的 EmbeddedChannel 测试方法
* 可以 绑定 很多的 handler 进行测试
* <p>
* <p>
* 入站:服务端 处理 客户端 write事件
*/
@Slf4j
public class TestEmbeddedChannel {
public static void main(String[] args) {
// 1. 入站
ChannelInboundHandlerAdapter h1 = new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.info("1");
super.channelRead(ctx, msg);
}
};
// 2. 入站
ChannelInboundHandlerAdapter h2 = new ChannelInboundHandlerAdapter() {// 当前handler起名 h2
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.info("2");
super.channelRead(ctx, msg);
}
};
// 3. 入站
ChannelInboundHandlerAdapter h3 = new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.info("3333333333333333");
ctx.channel().writeAndFlush(msg); // 【最后一个handler往前找】
// ctx.writeAndFlush(msg); // 【当前节点往上找 出站处理器】
}
};
// 4. 出站
ChannelOutboundHandlerAdapter h4 = new ChannelOutboundHandlerAdapter() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
log.info("4");
super.write(ctx, msg, promise);
}
};
// 5. 出站
ChannelOutboundHandlerAdapter h5 = new ChannelOutboundHandlerAdapter() {
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
log.info("5");
super.write(ctx, msg, promise);
}
};
final EmbeddedChannel channel = new EmbeddedChannel(h1, h2, h3, h4, h5);
// 模拟 入站
channel.writeInbound(ByteBufAllocator.DEFAULT.buffer().writeBytes("hello".getBytes()));
System.out.println(((ByteBuf) channel.readOutbound()).toString(Charset.defaultCharset()));
/**
* 输出结果
* [main] INFO com.kwan.shuyu.heima.netty_06_pipeline.TestEmbeddedChannel - 1
* [main] INFO com.kwan.shuyu.heima.netty_06_pipeline.TestEmbeddedChannel - 2
* [main] INFO com.kwan.shuyu.heima.netty_06_pipeline.TestEmbeddedChannel - 3333333333333333
* [main] INFO com.kwan.shuyu.heima.netty_06_pipeline.TestEmbeddedChannel - 5
* [main] INFO com.kwan.shuyu.heima.netty_06_pipeline.TestEmbeddedChannel - 4
* hello
*/
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_07_bytebuf;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import static io.netty.buffer.ByteBufUtil.appendPrettyHexDump;
import static io.netty.util.internal.StringUtil.NEWLINE;
/**
* 测试 ByteBuf 扩容
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/27 09:34
*/
public class ByteBuf_01 {
public static void main(String[] args) {
ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
log(buf);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 300; i++) {
sb.append("a");
}
buf.writeBytes(sb.toString().getBytes());
log(buf);
/**
* PooledUnsafeDirectByteBuf(ridx: 0, widx: 0, cap: 256)
* read index:0 write index:300 capacity:512
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000010| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000020| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000030| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000040| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000050| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000060| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000070| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000080| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000090| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |000000a0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |000000b0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |000000c0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |000000d0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |000000e0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |000000f0| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000100| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000110| 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaaaaaa|
* |00000120| 61 61 61 61 61 61 61 61 61 61 61 61 |aaaaaaaaaaaa |
* +--------+-------------------------------------------------+----------------+
*/
}
private static void log(ByteBuf buffer) {
int length = buffer.readableBytes();
int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
StringBuilder buf = new StringBuilder(rows * 80 * 2)
.append("read index:").append(buffer.readerIndex())
.append(" write index:").append(buffer.writerIndex())
.append(" capacity:").append(buffer.capacity())
.append(NEWLINE);
appendPrettyHexDump(buf, buffer);
System.out.println(buf.toString());
}
}
package com.kwan.shuyu.heima.netty_07_bytebuf;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import static io.netty.buffer.ByteBufUtil.appendPrettyHexDump;
import static io.netty.util.internal.StringUtil.NEWLINE;
/**
* ByteBuf 创建
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/27 09:34
*/
public class ByteBuf_02 {
public static void main(String[] args) {
ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(10);
log(buffer);
//上面代码创建了一个默认的ByteBuf(池化基于直接内存的 ByteBuf),初始容量是10
/**
* read index:0 write index:0 capacity:10
*/
}
private static void log(ByteBuf buffer) {
int length = buffer.readableBytes();
int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
StringBuilder buf = new StringBuilder(rows * 80 * 2)
.append("read index:").append(buffer.readerIndex())
.append(" write index:").append(buffer.writerIndex())
.append(" capacity:").append(buffer.capacity())
.append(NEWLINE);
appendPrettyHexDump(buf, buffer);
System.out.println(buf.toString());
}
}
package com.kwan.shuyu.heima.netty_07_bytebuf;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import lombok.extern.slf4j.Slf4j;
import static io.netty.buffer.ByteBufUtil.appendPrettyHexDump;
import static io.netty.util.internal.StringUtil.NEWLINE;
/**
* ByteBuf 的类型打印
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/27 09:34
*/
@Slf4j
public class ByteBuf_03 {
public static void main(String[] args) {
ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(10);
ByteBuf buffer1 = ByteBufAllocator.DEFAULT.heapBuffer(10);
ByteBuf buffer2 = ByteBufAllocator.DEFAULT.directBuffer(10);
log.info(buffer.getClass().getSimpleName());
log.info(buffer1.getClass().getSimpleName());
log.info(buffer2.getClass().getSimpleName());
log(buffer);
//上面代码创建了一个默认的ByteBuf(池化基于直接内存的 ByteBuf),初始容量是10
/**
[main] INFO com.kwan.shuyu.heima.netty_07_bytebuf.ByteBuf_03 - PooledUnsafeDirectByteBuf
[main] INFO com.kwan.shuyu.heima.netty_07_bytebuf.ByteBuf_03 - PooledUnsafeHeapByteBuf
[main] INFO com.kwan.shuyu.heima.netty_07_bytebuf.ByteBuf_03 - PooledUnsafeDirectByteBuf
read index:0 write index:0 capacity:10
*/
}
private static void log(ByteBuf buffer) {
int length = buffer.readableBytes();
int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
StringBuilder buf = new StringBuilder(rows * 80 * 2)
.append("read index:").append(buffer.readerIndex())
.append(" write index:").append(buffer.writerIndex())
.append(" capacity:").append(buffer.capacity())
.append(NEWLINE);
appendPrettyHexDump(buf, buffer);
System.out.println(buf.toString());
}
}
package com.kwan.shuyu.heima.netty_07_bytebuf;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import lombok.extern.slf4j.Slf4j;
import static io.netty.buffer.ByteBufUtil.appendPrettyHexDump;
import static io.netty.util.internal.StringUtil.NEWLINE;
/**
* ByteBuf 数据写入
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/27 09:34
*/
@Slf4j
public class ByteBuf_04 {
public static void main(String[] args) {
ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(10);
buffer.writeBytes(new byte[]{1, 2, 3, 4});
log(buffer);
//上面代码创建了一个默认的ByteBuf(池化基于直接内存的 ByteBuf),初始容量是10
/**
* read index:0 write index:4 capacity:10
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 |.... |
* +--------+-------------------------------------------------+----------------+
*/
buffer.writeInt(5);//int占四个字节
log(buffer);
/**
* read index:0 write index:8 capacity:10
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 00 00 00 05 |........ |
* +--------+-------------------------------------------------+----------------+
*/
buffer.setInt(0, 5);//int占四个字节,不会改变写指针,会出现数据覆盖
log(buffer);
/**
* read index:0 write index:8 capacity:10
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 00 00 00 05 00 00 00 05 |........ |
* +--------+-------------------------------------------------+----------------+
*/
}
private static void log(ByteBuf buffer) {
int length = buffer.readableBytes();
int rows = length / 16 + (length % 15 == 0 ? 0 : 1) + 4;
StringBuilder buf = new StringBuilder(rows * 80 * 2)
.append("read index:").append(buffer.readerIndex())
.append(" write index:").append(buffer.writerIndex())
.append(" capacity:").append(buffer.capacity())
.append(NEWLINE);
appendPrettyHexDump(buf, buffer);
System.out.println(buf.toString());
}
}
package com.kwan.shuyu.until; package com.kwan.shuyu.until;
import io.netty.buffer.ByteBuf;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import static io.netty.util.internal.MathUtil.isOutOfBounds; import static io.netty.util.internal.MathUtil.isOutOfBounds;
import static io.netty.util.internal.StringUtil.NEWLINE;
/** /**
* ByteBuffer工具类 * ByteBuffer工具类
...@@ -167,4 +169,6 @@ public class ByteBufferUtil { ...@@ -167,4 +169,6 @@ public class ByteBufferUtil {
public static short getUnsignedByte(ByteBuffer buffer, int index) { public static short getUnsignedByte(ByteBuffer buffer, int index) {
return (short) (buffer.get(index) & 0xFF); return (short) (buffer.get(index) & 0xFF);
} }
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册