fix:bytebuf数据合并

上级 16c40b32
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_05 {
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.writeInt(7);//int占四个字节
log(buffer);
/**
* read index:0 write index:12 capacity:16
* +-------------------------------------------------+
* | 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 00 00 00 07 |............ |
* +--------+-------------------------------------------------+----------------+
*/
}
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_06 {
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.writeInt(7);//int占四个字节
log(buffer);
/**
* read index:0 write index:12 capacity:16
* +-------------------------------------------------+
* | 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 00 00 00 07 |............ |
* +--------+-------------------------------------------------+----------------+
*/
System.out.println(buffer.readByte());
System.out.println(buffer.readByte());
System.out.println(buffer.readByte());
System.out.println(buffer.readByte());
log(buffer);
/**
* 1
* 2
* 3
* 4
* read index:4 write index:12 capacity:16
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 00 00 00 05 00 00 00 07 |........ |
* +--------+-------------------------------------------------+----------------+
*/
}
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_07 {
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.writeInt(7);//int占四个字节
log(buffer);
/**
* read index:0 write index:12 capacity:16
* +-------------------------------------------------+
* | 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 00 00 00 07 |............ |
* +--------+-------------------------------------------------+----------------+
*/
System.out.println(buffer.readByte());
System.out.println(buffer.readByte());
System.out.println(buffer.readByte());
System.out.println(buffer.readByte());
log(buffer);
/**
* 1
* 2
* 3
* 4
* read index:4 write index:12 capacity:16
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 00 00 00 05 00 00 00 07 |........ |
* +--------+-------------------------------------------------+----------------+
*/
buffer.markReaderIndex();
buffer.readInt();
log(buffer);
buffer.resetReaderIndex();//读之前读过的数据
log(buffer);
/**
* read index:8 write index:12 capacity:16
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 00 00 00 07 |.... |
* +--------+-------------------------------------------------+----------------+
* read index:4 write index:12 capacity:16
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 00 00 00 05 00 00 00 07 |........ |
* +--------+-------------------------------------------------+----------------+
*/
}
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的slice
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/27 09:34
*/
@Slf4j
public class ByteBuf_08 {
public static void main(String[] args) {
ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(10);
buffer.writeBytes(new byte[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'});
log(buffer);
final ByteBuf slice1 = buffer.slice(0, 5);
slice1.retain();
final ByteBuf slice2 = buffer.slice(0, 5);
slice2.retain();
log(slice1);
log(slice2);
/**
* read index:0 write index:10 capacity:10
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 61 62 63 64 65 66 67 68 69 6a |abcdefghij |
* +--------+-------------------------------------------------+----------------+
* read index:0 write index:5 capacity:5
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 61 62 63 64 65 |abcde |
* +--------+-------------------------------------------------+----------------+
* read index:0 write index:5 capacity:5
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 61 62 63 64 65 |abcde |
* +--------+-------------------------------------------------+----------------+
*/
slice1.setByte(0, 'b');
log(buffer);
log(slice1);
/**
* read index:0 write index:10 capacity:10
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 62 62 63 64 65 66 67 68 69 6a |bbcdefghij |
* +--------+-------------------------------------------------+----------------+
* read index:0 write index:5 capacity:5
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 62 62 63 64 65 |bbcde |
* +--------+-------------------------------------------------+----------------+
*/
buffer.release();
log(slice1);
/**
* Exception in thread "main" io.netty.util.IllegalReferenceCountException: refCnt: 0
* at io.netty.buffer.AbstractByteBuf.ensureAccessible(AbstractByteBuf.java:1454)
* at io.netty.buffer.AbstractByteBuf.checkIndex(AbstractByteBuf.java:1383)
* at io.netty.buffer.AbstractByteBuf.checkIndex(AbstractByteBuf.java:1379)
* at io.netty.buffer.AbstractByteBuf.getByte(AbstractByteBuf.java:355)
* at io.netty.buffer.AbstractUnpooledSlicedByteBuf.getByte(AbstractUnpooledSlicedByteBuf.java:120)
* at io.netty.buffer.AbstractByteBuf.getUnsignedByte(AbstractByteBuf.java:368)
* at io.netty.buffer.ByteBufUtil$HexUtil.appendPrettyHexDump(ByteBufUtil.java:1580)
* at io.netty.buffer.ByteBufUtil$HexUtil.access$500(ByteBufUtil.java:1420)
* at io.netty.buffer.ByteBufUtil.appendPrettyHexDump(ByteBufUtil.java:1416)
* at io.netty.buffer.ByteBufUtil.appendPrettyHexDump(ByteBufUtil.java:1407)
* at com.kwan.shuyu.heima.netty_07_bytebuf.ByteBuf_08.log(ByteBuf_08.java:79)
* at com.kwan.shuyu.heima.netty_07_bytebuf.ByteBuf_08.main(ByteBuf_08.java:67)
*/
}
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 io.netty.buffer.ByteBufUtil;
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_09 {
public static void main(String[] args) {
ByteBuf buf1 = ByteBufAllocator.DEFAULT.buffer(5);
buf1.writeBytes(new byte[]{1, 2, 3, 4, 5});
ByteBuf buf2 = ByteBufAllocator.DEFAULT.buffer(5);
buf2.writeBytes(new byte[]{6, 7, 8, 9, 10});
System.out.println(ByteBufUtil.prettyHexDump(buf1));
System.out.println(ByteBufUtil.prettyHexDump(buf2));
/**
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 05 |..... |
* +--------+-------------------------------------------------+----------------+
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 06 07 08 09 0a |..... |
* +--------+-------------------------------------------------+----------------+
*/
ByteBuf buf3 = ByteBufAllocator.DEFAULT
.buffer(buf1.readableBytes() + buf2.readableBytes());
buf3.writeBytes(buf1);
buf3.writeBytes(buf2);
System.out.println(ByteBufUtil.prettyHexDump(buf3));
/**
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 05 06 07 08 09 0a |.......... |
* +--------+-------------------------------------------------+----------------+
*/
}
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 io.netty.buffer.ByteBufUtil;
import io.netty.buffer.CompositeByteBuf;
import lombok.extern.slf4j.Slf4j;
import static io.netty.buffer.ByteBufUtil.appendPrettyHexDump;
import static io.netty.util.internal.StringUtil.NEWLINE;
/**
* ByteBuf使用CompositeByteBuf方式合并
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/27 09:34
*/
@Slf4j
public class ByteBuf_10 {
public static void main(String[] args) {
ByteBuf buf1 = ByteBufAllocator.DEFAULT.buffer(5);
buf1.writeBytes(new byte[]{1, 2, 3, 4, 5});
ByteBuf buf2 = ByteBufAllocator.DEFAULT.buffer(5);
buf2.writeBytes(new byte[]{6, 7, 8, 9, 10});
System.out.println(ByteBufUtil.prettyHexDump(buf1));
System.out.println(ByteBufUtil.prettyHexDump(buf2));
/**
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 05 |..... |
* +--------+-------------------------------------------------+----------------+
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 06 07 08 09 0a |..... |
* +--------+-------------------------------------------------+----------------+
*/
CompositeByteBuf buf4 = ByteBufAllocator.DEFAULT.compositeBuffer();
// true 表示增加新的 ByteBuf 自动递增 write index, 否则 write index 会始终为 0
buf4.addComponents(true, buf1, buf2);
log(buf4);
/**
* +-------------------------------------------------+
* | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
* +--------+-------------------------------------------------+----------------+
* |00000000| 01 02 03 04 05 06 07 08 09 0a |.......... |
* +--------+-------------------------------------------------+----------------+
*/
}
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());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册