0%

java | bytebuf 调试方法

bytebuf 的调试方法。

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.redisc;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

import static io.netty.buffer.ByteBufUtil.appendPrettyHexDump;
import static io.netty.util.internal.StringUtil.NEWLINE;


@Slf4j
public class Run {

public static void main(String[] args) throws IOException {
ByteBuf byteBuf = ByteBufAllocator.DEFAULT.directBuffer(); // 默认长度 256
System.out.println(byteBuf);

StringBuilder s = new StringBuilder();
for (int i = 0; i < 300; i++) {
s.append(i);
}

byteBuf.writeBytes(s.toString().getBytes());
log(byteBuf);
}

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

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
         +-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 30 31 32 33 34 35 36 37 38 39 31 30 31 31 31 32 |0123456789101112|
|00000010| 31 33 31 34 31 35 31 36 31 37 31 38 31 39 32 30 |1314151617181920|
|00000020| 32 31 32 32 32 33 32 34 32 35 32 36 32 37 32 38 |2122232425262728|
|00000030| 32 39 33 30 33 31 33 32 33 33 33 34 33 35 33 36 |2930313233343536|
|00000040| 33 37 33 38 33 39 34 30 34 31 34 32 34 33 34 34 |3738394041424344|
|00000050| 34 35 34 36 34 37 34 38 34 39 35 30 35 31 35 32 |4546474849505152|
|00000060| 35 33 35 34 35 35 35 36 35 37 35 38 35 39 36 30 |5354555657585960|
|00000070| 36 31 36 32 36 33 36 34 36 35 36 36 36 37 36 38 |6162636465666768|
|00000080| 36 39 37 30 37 31 37 32 37 33 37 34 37 35 37 36 |6970717273747576|
|00000090| 37 37 37 38 37 39 38 30 38 31 38 32 38 33 38 34 |7778798081828384|
|000000a0| 38 35 38 36 38 37 38 38 38 39 39 30 39 31 39 32 |8586878889909192|
|000000b0| 39 33 39 34 39 35 39 36 39 37 39 38 39 39 31 30 |9394959697989910|
|000000c0| 30 31 30 31 31 30 32 31 30 33 31 30 34 31 30 35 |0101102103104105|
|000000d0| 31 30 36 31 30 37 31 30 38 31 30 39 31 31 30 31 |1061071081091101|
|000000e0| 31 31 31 31 32 31 31 33 31 31 34 31 31 35 31 31 |1111211311411511|
|000000f0| 36 31 31 37 31 31 38 31 31 39 31 32 30 31 32 31 |6117118119120121|
|00000100| 31 32 32 31 32 33 31 32 34 31 32 35 31 32 36 31 |1221231241251261|
|00000110| 32 37 31 32 38 31 32 39 31 33 30 31 33 31 31 33 |2712812913013113|
|00000120| 32 31 33 33 31 33 34 31 33 35 31 33 36 31 33 37 |2133134135136137|
|00000130| 31 33 38 31 33 39 31 34 30 31 34 31 31 34 32 31 |1381391401411421|
|00000140| 34 33 31 34 34 31 34 35 31 34 36 31 34 37 31 34 |4314414514614714|
|00000150| 38 31 34 39 31 35 30 31 35 31 31 35 32 31 35 33 |8149150151152153|
|00000160| 31 35 34 31 35 35 31 35 36 31 35 37 31 35 38 31 |1541551561571581|
|00000170| 35 39 31 36 30 31 36 31 31 36 32 31 36 33 31 36 |5916016116216316|
|00000180| 34 31 36 35 31 36 36 31 36 37 31 36 38 31 36 39 |4165166167168169|
|00000190| 31 37 30 31 37 31 31 37 32 31 37 33 31 37 34 31 |1701711721731741|
|000001a0| 37 35 31 37 36 31 37 37 31 37 38 31 37 39 31 38 |7517617717817918|
|000001b0| 30 31 38 31 31 38 32 31 38 33 31 38 34 31 38 35 |0181182183184185|
|000001c0| 31 38 36 31 38 37 31 38 38 31 38 39 31 39 30 31 |1861871881891901|
|000001d0| 39 31 31 39 32 31 39 33 31 39 34 31 39 35 31 39 |9119219319419519|
|000001e0| 36 31 39 37 31 39 38 31 39 39 32 30 30 32 30 31 |6197198199200201|
|000001f0| 32 30 32 32 30 33 32 30 34 32 30 35 32 30 36 32 |2022032042052062|
|00000200| 30 37 32 30 38 32 30 39 32 31 30 32 31 31 32 31 |0720820921021121|
|00000210| 32 32 31 33 32 31 34 32 31 35 32 31 36 32 31 37 |2213214215216217|
|00000220| 32 31 38 32 31 39 32 32 30 32 32 31 32 32 32 32 |2182192202212222|
|00000230| 32 33 32 32 34 32 32 35 32 32 36 32 32 37 32 32 |2322422522622722|
|00000240| 38 32 32 39 32 33 30 32 33 31 32 33 32 32 33 33 |8229230231232233|
|00000250| 32 33 34 32 33 35 32 33 36 32 33 37 32 33 38 32 |2342352362372382|
|00000260| 33 39 32 34 30 32 34 31 32 34 32 32 34 33 32 34 |3924024124224324|
|00000270| 34 32 34 35 32 34 36 32 34 37 32 34 38 32 34 39 |4245246247248249|
|00000280| 32 35 30 32 35 31 32 35 32 32 35 33 32 35 34 32 |2502512522532542|
|00000290| 35 35 32 35 36 32 35 37 32 35 38 32 35 39 32 36 |5525625725825926|
|000002a0| 30 32 36 31 32 36 32 32 36 33 32 36 34 32 36 35 |0261262263264265|
|000002b0| 32 36 36 32 36 37 32 36 38 32 36 39 32 37 30 32 |2662672682692702|
|000002c0| 37 31 32 37 32 32 37 33 32 37 34 32 37 35 32 37 |7127227327427527|
|000002d0| 36 32 37 37 32 37 38 32 37 39 32 38 30 32 38 31 |6277278279280281|
|000002e0| 32 38 32 32 38 33 32 38 34 32 38 35 32 38 36 32 |2822832842852862|
|000002f0| 38 37 32 38 38 32 38 39 32 39 30 32 39 31 32 39 |8728828929029129|
|00000300| 32 32 39 33 32 39 34 32 39 35 32 39 36 32 39 37 |2293294295296297|
|00000310| 32 39 38 32 39 39 |298299 |
+--------+-------------------------------------------------+----------------+
请我喝杯咖啡吧~