18045010223
7 天以前 afe371d39a054b2f2a9e5875b945584eec8a8141
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
package cn.org.hentai.jtt1078.util;
 
import io.netty.buffer.ByteBuf;
 
import java.nio.charset.StandardCharsets;
/**
 * author:zhouyili (11861744@qq.com)
 */
public class ByteBufUtils {
 
    public static byte[] readReadableBytes(ByteBuf msg) {
       byte[] content = new byte[msg.readableBytes()];
       msg.readBytes(content);
       return content;
    }
 
    public static byte[] getReadableBytes(ByteBuf msg) {
        byte[] content = new byte[msg.readableBytes()];
        int start = msg.readerIndex();
        msg.getBytes(start,content);
        return content;
    }
    public static byte[] readBytes(ByteBuf buf, int length) {
        validLength(buf, length);
        byte[] content = new byte[length];
        buf.readBytes(content);
        return content;
    }
 
    private static void validLength(ByteBuf buf, int length) {
        int readableLength = buf.readableBytes();
        if (readableLength<length){
            throw new RuntimeException("可读数据长度小于设定值");
        }
    }
 
    public static String toString(ByteBuf buf, int length) {
        validLength(buf,length);
        return buf.readCharSequence(length, StandardCharsets.ISO_8859_1).toString();
    }
 
    public static String toString(ByteBuf buf) {
        return toString(buf,buf.readableBytes());
    }
}