37874
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
package java.net.http;
|
|
26 |
|
|
27 |
import java.io.IOException;
|
|
28 |
import java.nio.ByteBuffer;
|
|
29 |
import java.nio.channels.SelectionKey;
|
|
30 |
import java.util.function.Consumer;
|
|
31 |
|
|
32 |
import static java.util.Objects.requireNonNull;
|
|
33 |
|
|
34 |
/*
|
|
35 |
* Writes ByteBuffer[] to the channel in a non-blocking, asynchronous fashion.
|
|
36 |
*
|
|
37 |
* A client attempts to write data by calling
|
|
38 |
*
|
|
39 |
* boolean tryWriteFully(ByteBuffer[] buffers)
|
|
40 |
*
|
|
41 |
* If the attempt was successful and all the data has been written, then the
|
|
42 |
* method returns `true`.
|
|
43 |
*
|
|
44 |
* If the data has been written partially, then the method returns `false`, and
|
|
45 |
* the writer (this object) attempts to complete the write asynchronously by
|
|
46 |
* calling, possibly more than once
|
|
47 |
*
|
|
48 |
* boolean tryCompleteWrite()
|
|
49 |
*
|
|
50 |
* in its own threads.
|
|
51 |
*
|
|
52 |
* When the write has been completed asynchronously, the callback is signalled
|
|
53 |
* with `null`.
|
|
54 |
*
|
|
55 |
* If an error occurs in any of these stages it will NOT be thrown from the
|
|
56 |
* method. Instead `false` will be returned and the exception will be signalled
|
|
57 |
* to the callback. This is done in order to handle all exceptions in a single
|
|
58 |
* place.
|
|
59 |
*/
|
|
60 |
final class WSWriter {
|
|
61 |
|
|
62 |
private final RawChannel channel;
|
|
63 |
private final RawChannel.NonBlockingEvent writeReadinessHandler;
|
|
64 |
private final Consumer<Throwable> completionCallback;
|
|
65 |
private ByteBuffer[] buffers;
|
|
66 |
private int offset;
|
|
67 |
|
|
68 |
WSWriter(RawChannel channel, Consumer<Throwable> completionCallback) {
|
|
69 |
this.channel = channel;
|
|
70 |
this.completionCallback = completionCallback;
|
|
71 |
this.writeReadinessHandler = createHandler();
|
|
72 |
}
|
|
73 |
|
|
74 |
boolean tryWriteFully(ByteBuffer[] buffers) {
|
|
75 |
synchronized (this) {
|
|
76 |
this.buffers = requireNonNull(buffers);
|
|
77 |
this.offset = 0;
|
|
78 |
}
|
|
79 |
return tryCompleteWrite();
|
|
80 |
}
|
|
81 |
|
|
82 |
private final boolean tryCompleteWrite() {
|
|
83 |
try {
|
|
84 |
return writeNow();
|
|
85 |
} catch (IOException e) {
|
|
86 |
completionCallback.accept(e);
|
|
87 |
return false;
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
private boolean writeNow() throws IOException {
|
|
92 |
synchronized (this) {
|
|
93 |
for (; offset != -1; offset = nextUnwrittenIndex(buffers, offset)) {
|
|
94 |
long bytesWritten = channel.write(buffers, offset, buffers.length - offset);
|
|
95 |
if (bytesWritten == 0) {
|
|
96 |
channel.registerEvent(writeReadinessHandler);
|
|
97 |
return false;
|
|
98 |
}
|
|
99 |
}
|
|
100 |
return true;
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
private static int nextUnwrittenIndex(ByteBuffer[] buffers, int offset) {
|
|
105 |
for (int i = offset; i < buffers.length; i++) {
|
|
106 |
if (buffers[i].hasRemaining()) {
|
|
107 |
return i;
|
|
108 |
}
|
|
109 |
}
|
|
110 |
return -1;
|
|
111 |
}
|
|
112 |
|
|
113 |
private RawChannel.NonBlockingEvent createHandler() {
|
|
114 |
return new RawChannel.NonBlockingEvent() {
|
|
115 |
|
|
116 |
@Override
|
|
117 |
public int interestOps() {
|
|
118 |
return SelectionKey.OP_WRITE;
|
|
119 |
}
|
|
120 |
|
|
121 |
@Override
|
|
122 |
public void handle() {
|
|
123 |
if (tryCompleteWrite()) {
|
|
124 |
completionCallback.accept(null);
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
@Override
|
|
129 |
public String toString() {
|
|
130 |
return "Write readiness event [" + channel + "]";
|
|
131 |
}
|
|
132 |
};
|
|
133 |
}
|
|
134 |
}
|