1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.river.jeri.internal.mux;
20
21 import java.io.IOException;
22
23 /**
24 * An IOFuture represents an I/O operation that may or may not have
25 * completed yet.
26 *
27 * @author Sun Microsystems, Inc.
28 *
29 */
30 final class IOFuture {
31
32 private boolean done = false;
33 boolean data = false;
34 private int position = -1;
35 private IOException exception = null;
36
37 IOFuture() { }
38
39 /**
40 * Waits until the I/O operation has completed. If this method
41 * returns normally, then the I/O operation has completed
42 * successfully. If this method throws IOException, then the
43 * I/O operation failed.
44 *
45 * REMIND: Maybe we should support a timeout here, as a paranoid
46 * escape hatch; if this wait really takes a long time, something
47 * has gone dreadfully wrong. To a large extent, we're really
48 * depending on someone's finally clause to make sure that pending
49 * instances of this class always get notified somehow.
50 *
51 * @throws IOException if the I/O operation failed
52 *
53 * @throws InterruptedException if the current thread was
54 * interrupted while waiting for the I/O to complete.
55 * @return true if data remaining.
56 */
57 synchronized boolean waitUntilDone()
58 throws IOException, InterruptedException
59 {
60 while (!done) {
61 wait();
62 }
63 if (exception != null) {
64 exception.fillInStackTrace();
65 throw exception;
66 }
67 return data;
68 }
69
70 synchronized int getPosition(){
71 return position;
72 }
73
74 /**
75 * Signals that this I/O operation has completed successfully.
76 */
77 synchronized void done() {
78 assert !done;
79 data = false;
80 done = true;
81 notifyAll();
82 }
83
84 /**
85 * Signals that this I/O operation has remaining data.
86 * @param position
87 */
88 synchronized void done(int position){
89 assert !done;
90 done = true;
91 data = true;
92 this.position = position;
93 notifyAll();
94 }
95
96 /**
97 * Signals that this I/O operation has failed (with details of the
98 * failure in the given IOException).
99 *
100 * @param e detail of the I/O operation's failure
101 */
102 synchronized void done(IOException e) {
103 if (done) {
104 /*
105 * This shouldn't normally happen, but it's difficult to prevent
106 * in bizarre failure scenarios (like an OutOfMemoryError).
107 */
108 return;
109 }
110 if (e == null) {
111 throw new NullPointerException();
112 }
113 this.exception = e;
114 done = true;
115 notifyAll();
116 }
117 }