1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.marshalling;
17
18 import static io.netty.util.internal.ObjectUtil.checkPositive;
19
20 import org.jboss.marshalling.ByteInput;
21
22 import java.io.IOException;
23
24
25
26
27
28 class LimitingByteInput implements ByteInput {
29
30
31 private static final TooBigObjectException EXCEPTION = new TooBigObjectException();
32
33 private final ByteInput input;
34 private final long limit;
35 private long read;
36
37 LimitingByteInput(ByteInput input, long limit) {
38 this.input = input;
39 this.limit = checkPositive(limit, "limit");
40 }
41
42 @Override
43 public void close() throws IOException {
44
45 }
46
47 @Override
48 public int available() throws IOException {
49 return readable(input.available());
50 }
51
52 @Override
53 public int read() throws IOException {
54 int readable = readable(1);
55 if (readable > 0) {
56 int b = input.read();
57 read++;
58 return b;
59 } else {
60 throw EXCEPTION;
61 }
62 }
63
64 @Override
65 public int read(byte[] array) throws IOException {
66 return read(array, 0, array.length);
67 }
68
69 @Override
70 public int read(byte[] array, int offset, int length) throws IOException {
71 int readable = readable(length);
72 if (readable > 0) {
73 int i = input.read(array, offset, readable);
74 read += i;
75 return i;
76 } else {
77 throw EXCEPTION;
78 }
79 }
80
81 @Override
82 public long skip(long bytes) throws IOException {
83 int readable = readable((int) bytes);
84 if (readable > 0) {
85 long i = input.skip(readable);
86 read += i;
87 return i;
88 } else {
89 throw EXCEPTION;
90 }
91 }
92
93 private int readable(int length) {
94 return (int) Math.min(length, limit - read);
95 }
96
97
98
99
100
101 static final class TooBigObjectException extends IOException {
102 private static final long serialVersionUID = 1L;
103 }
104 }