1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.ByteBufHolder;
20
21
22
23
24
25 public final class DefaultMessageSizeEstimator implements MessageSizeEstimator {
26
27 private static final class HandleImpl implements Handle {
28 private final int unknownSize;
29
30 private HandleImpl(int unknownSize) {
31 this.unknownSize = unknownSize;
32 }
33
34 @Override
35 public int size(Object msg) {
36 if (msg instanceof ByteBuf) {
37 return ((ByteBuf) msg).readableBytes();
38 }
39 if (msg instanceof ByteBufHolder) {
40 return ((ByteBufHolder) msg).content().readableBytes();
41 }
42 if (msg instanceof FileRegion) {
43 return 0;
44 }
45 return unknownSize;
46 }
47 }
48
49
50
51
52 public static final MessageSizeEstimator DEFAULT = new DefaultMessageSizeEstimator(8);
53
54 private final Handle handle;
55
56
57
58
59
60
61 public DefaultMessageSizeEstimator(int unknownSize) {
62 if (unknownSize < 0) {
63 throw new IllegalArgumentException("unknownSize: " + unknownSize + " (expected: >= 0)");
64 }
65 handle = new HandleImpl(unknownSize);
66 }
67
68 @Override
69 public Handle newHandle() {
70 return handle;
71 }
72 }