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