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 public class DefaultReadFuture extends DefaultIoFuture implements ReadFuture {
33
34 private static final Object CLOSED = new Object();
35
36
37
38
39 public DefaultReadFuture(IoSession session) {
40 super(session);
41 }
42
43 public Object getMessage() {
44 if (isDone()) {
45 Object v = getValue();
46 if (v == CLOSED) {
47 return null;
48 }
49
50 if (v instanceof ExceptionHolder) {
51 v = ((ExceptionHolder) v).exception;
52 if (v instanceof RuntimeException) {
53 throw (RuntimeException) v;
54 }
55 if (v instanceof Error) {
56 throw (Error) v;
57 }
58 if (v instanceof IOException || v instanceof Exception) {
59 throw new RuntimeIoException((Exception) v);
60 }
61 }
62
63 return v;
64 }
65
66 return null;
67 }
68
69 public boolean isRead() {
70 if (isDone()) {
71 Object v = getValue();
72 return (v != CLOSED && !(v instanceof ExceptionHolder));
73 }
74 return false;
75 }
76
77 public boolean isClosed() {
78 if (isDone()) {
79 return getValue() == CLOSED;
80 }
81 return false;
82 }
83
84 public Throwable getException() {
85 if (isDone()) {
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 IllegalArgumentException("message");
101 }
102 setValue(message);
103 }
104
105 public void setException(Throwable exception) {
106 if (exception == null) {
107 throw new IllegalArgumentException("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 }