1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.compression;
17
18 import io.netty.buffer.ByteBuf;
19 import io.netty.util.internal.ObjectUtil;
20
21
22
23
24
25 final class DefensiveDecompressor implements Decompressor {
26 private final Decompressor delegate;
27 private Status status;
28 private boolean closed;
29 private boolean failed;
30
31 DefensiveDecompressor(Decompressor delegate) {
32 this.delegate = ObjectUtil.checkNotNull(delegate, "delegate");
33 }
34
35 @Override
36 public Status status() throws DecompressionException {
37 checkReady();
38 try {
39 status = delegate.status();
40 } catch (Exception e) {
41 failed = true;
42 throw e;
43 }
44 return status;
45 }
46
47 @Override
48 public void addInput(ByteBuf buf) throws DecompressionException {
49 try {
50 checkReady();
51 checkState(Status.NEED_INPUT);
52 } catch (Throwable t) {
53 buf.release();
54 throw t;
55 }
56 try {
57 delegate.addInput(buf);
58 } catch (Exception e) {
59 failed = true;
60 throw e;
61 }
62 status = null;
63 }
64
65 @Override
66 public void endOfInput() throws DecompressionException {
67 checkReady();
68 checkState(Status.NEED_INPUT);
69 try {
70 delegate.endOfInput();
71 } catch (Exception e) {
72 failed = true;
73 throw e;
74 }
75 status = null;
76 }
77
78 @Override
79 public ByteBuf takeOutput() throws DecompressionException {
80 checkReady();
81 checkState(Status.NEED_OUTPUT);
82 ByteBuf out;
83 try {
84 out = delegate.takeOutput();
85 } catch (Exception e) {
86 failed = true;
87 throw e;
88 }
89 status = null;
90 return out;
91 }
92
93 @Override
94 public void close() {
95 closed = true;
96 delegate.close();
97 }
98
99 private void checkReady() {
100 if (closed) {
101 throw new IllegalStateException("Already closed");
102 }
103 if (failed) {
104 throw new IllegalStateException("Previous call failed");
105 }
106 }
107
108 private void checkState(Status expected) {
109 if (this.status != expected) {
110 throw new IllegalStateException("Not in expected state " + expected + ", was " + this.status);
111 }
112 }
113 }