1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.common;
21
22 import java.io.IOException;
23
24
25
26
27
28
29
30
31 public class DefaultReadFuture extends DefaultIoFuture implements ReadFuture {
32
33 private static final Object CLOSED = new Object();
34
35
36
37
38 public DefaultReadFuture(IoSession session) {
39 super(session);
40 }
41
42 public Object getMessage() {
43 if (isReady()) {
44 Object v = getValue();
45 if (v == CLOSED) {
46 return null;
47 }
48
49 if (v instanceof ExceptionHolder) {
50 v = ((ExceptionHolder) v).exception;
51 if (v instanceof RuntimeException) {
52 throw (RuntimeException) v;
53 }
54 if (v instanceof Error) {
55 throw (Error) v;
56 }
57 if (v instanceof IOException || v instanceof Exception) {
58 throw new RuntimeIoException((Exception) v);
59 }
60 }
61
62 return v;
63 }
64
65 return null;
66 }
67
68
69 public boolean isRead() {
70 if (isReady()) {
71 Object v = getValue();
72 return (v != CLOSED && !(v instanceof ExceptionHolder));
73 }
74 return false;
75 }
76
77 public boolean isClosed() {
78 if (isReady()) {
79 return getValue() == CLOSED;
80 }
81 return false;
82 }
83
84 public Throwable getException() {
85 if (isReady()) {
86 Object v = getValue();
87 if (v instanceof ExceptionHolder) {
88 return ((ExceptionHolder) v).exception;
89 }
90 }
91 return null;
92 }
93
94 public void setClosed() {
95 setValue(CLOSED);
96 }
97
98 public void setRead(Object message) {
99 if (message == null) {
100 throw new NullPointerException("message");
101 }
102 setValue(message);
103 }
104
105 public void setException(Throwable exception) {
106 if (exception == null) {
107 throw new NullPointerException("exception");
108 }
109
110 setValue(new ExceptionHolder(exception));
111 }
112
113 @Override
114 public ReadFuture await() throws InterruptedException {
115 return (ReadFuture) super.await();
116 }
117
118 @Override
119 public ReadFuture awaitUninterruptibly() {
120 return (ReadFuture) super.awaitUninterruptibly();
121 }
122
123 @Override
124 public ReadFuture addListener(IoFutureListener<?> listener) {
125 return (ReadFuture) super.addListener(listener);
126 }
127
128 @Override
129 public ReadFuture removeListener(IoFutureListener<?> listener) {
130 return (ReadFuture) super.removeListener(listener);
131 }
132
133 private static class ExceptionHolder {
134 private final Throwable exception;
135
136 private ExceptionHolder(Throwable exception) {
137 this.exception = exception;
138 }
139 }
140 }