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
34 public class DefaultReadFuture extends DefaultIoFuture implements ReadFuture {
35
36 private static final Object CLOSED = new Object();
37
38
39
40
41 public DefaultReadFuture(IoSession session) {
42 super(session);
43 }
44
45 public Object getMessage() {
46 if (isDone()) {
47 Object v = getValue();
48 if (v == CLOSED) {
49 return null;
50 }
51
52 if (v instanceof ExceptionHolder) {
53 v = ((ExceptionHolder) v).exception;
54 if (v instanceof RuntimeException) {
55 throw (RuntimeException) v;
56 }
57 if (v instanceof Error) {
58 throw (Error) v;
59 }
60 if (v instanceof IOException || v instanceof Exception) {
61 throw new RuntimeIoException((Exception) v);
62 }
63 }
64
65 return v;
66 }
67
68 return null;
69 }
70
71
72 public boolean isRead() {
73 if (isDone()) {
74 Object v = getValue();
75 return (v != CLOSED && !(v instanceof ExceptionHolder));
76 }
77 return false;
78 }
79
80 public boolean isClosed() {
81 if (isDone()) {
82 return getValue() == CLOSED;
83 }
84 return false;
85 }
86
87 public Throwable getException() {
88 if (isDone()) {
89 Object v = getValue();
90 if (v instanceof ExceptionHolder) {
91 return ((ExceptionHolder) v).exception;
92 }
93 }
94 return null;
95 }
96
97 public void setClosed() {
98 setValue(CLOSED);
99 }
100
101 public void setRead(Object message) {
102 if (message == null) {
103 throw new NullPointerException("message");
104 }
105 setValue(message);
106 }
107
108 public void setException(Throwable exception) {
109 if (exception == null) {
110 throw new NullPointerException("exception");
111 }
112
113 setValue(new ExceptionHolder(exception));
114 }
115
116 @Override
117 public ReadFuture await() throws InterruptedException {
118 return (ReadFuture) super.await();
119 }
120
121 @Override
122 public ReadFuture awaitUninterruptibly() {
123 return (ReadFuture) super.awaitUninterruptibly();
124 }
125
126 @Override
127 public ReadFuture addListener(IoFutureListener<?> listener) {
128 return (ReadFuture) super.addListener(listener);
129 }
130
131 @Override
132 public ReadFuture removeListener(IoFutureListener<?> listener) {
133 return (ReadFuture) super.removeListener(listener);
134 }
135
136 private static class ExceptionHolder {
137 private final Throwable exception;
138
139 private ExceptionHolder(Throwable exception) {
140 this.exception = exception;
141 }
142 }
143 }