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.nio.Buffer;
|
|
28 |
import java.nio.ByteBuffer;
|
|
29 |
import java.nio.CharBuffer;
|
|
30 |
import java.util.Objects;
|
|
31 |
import java.util.concurrent.atomic.AtomicBoolean;
|
|
32 |
|
|
33 |
//
|
|
34 |
// +-----------+---------------+------------ ~ ------+
|
|
35 |
// | shared#1 | shared#2 | non-shared |
|
|
36 |
// +-----------+---------------+------------ ~ ------+
|
|
37 |
// | |
|
|
38 |
// |<------------------ shared0 ---------- ~ ----->|
|
|
39 |
//
|
|
40 |
//
|
|
41 |
// Objects of the type are not thread-safe. It's the responsibility of the
|
|
42 |
// client to access shared buffers safely between threads.
|
|
43 |
//
|
|
44 |
// It would be perfect if we could extend java.nio.Buffer, but it's not an
|
|
45 |
// option since Buffer and all its descendants have package-private
|
|
46 |
// constructors.
|
|
47 |
//
|
|
48 |
abstract class WSShared<T extends Buffer> implements WSDisposable {
|
|
49 |
|
|
50 |
protected final AtomicBoolean disposed = new AtomicBoolean();
|
|
51 |
protected final T buffer;
|
|
52 |
|
|
53 |
protected WSShared(T buffer) {
|
|
54 |
this.buffer = Objects.requireNonNull(buffer);
|
|
55 |
}
|
|
56 |
|
|
57 |
static <T extends Buffer> WSShared<T> wrap(T buffer) {
|
|
58 |
return new WSShared<>(buffer) {
|
|
59 |
@Override
|
|
60 |
WSShared<T> share(int pos, int limit) {
|
|
61 |
throw new UnsupportedOperationException();
|
|
62 |
}
|
|
63 |
};
|
|
64 |
}
|
|
65 |
|
|
66 |
// TODO: should be a terminal operation as after it returns the buffer might
|
|
67 |
// have escaped (we can't protect it any more)
|
|
68 |
public T buffer() {
|
|
69 |
checkDisposed();
|
|
70 |
return buffer;
|
|
71 |
}
|
|
72 |
|
|
73 |
abstract WSShared<T> share(final int pos, final int limit);
|
|
74 |
|
|
75 |
WSShared<T> select(final int pos, final int limit) {
|
|
76 |
checkRegion(pos, limit, buffer());
|
|
77 |
select(pos, limit, buffer());
|
|
78 |
return this;
|
|
79 |
}
|
|
80 |
|
|
81 |
@Override
|
|
82 |
public void dispose() {
|
|
83 |
if (!disposed.compareAndSet(false, true)) {
|
|
84 |
throw new IllegalStateException("Has been disposed previously");
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
int limit() {
|
|
89 |
return buffer().limit();
|
|
90 |
}
|
|
91 |
|
|
92 |
WSShared<T> limit(int newLimit) {
|
|
93 |
buffer().limit(newLimit);
|
|
94 |
return this;
|
|
95 |
}
|
|
96 |
|
|
97 |
int position() {
|
|
98 |
return buffer().position();
|
|
99 |
}
|
|
100 |
|
|
101 |
WSShared<T> position(int newPosition) {
|
|
102 |
buffer().position(newPosition);
|
|
103 |
return this;
|
|
104 |
}
|
|
105 |
|
|
106 |
int remaining() {
|
|
107 |
return buffer().remaining();
|
|
108 |
}
|
|
109 |
|
|
110 |
boolean hasRemaining() {
|
|
111 |
return buffer().hasRemaining();
|
|
112 |
}
|
|
113 |
|
|
114 |
WSShared<T> flip() {
|
|
115 |
buffer().flip();
|
|
116 |
return this;
|
|
117 |
}
|
|
118 |
|
|
119 |
WSShared<T> rewind() {
|
|
120 |
buffer().rewind();
|
|
121 |
return this;
|
|
122 |
}
|
|
123 |
|
|
124 |
WSShared<T> put(WSShared<? extends T> src) {
|
|
125 |
put(this.buffer(), src.buffer());
|
|
126 |
return this;
|
|
127 |
}
|
|
128 |
|
|
129 |
static void checkRegion(int position, int limit, Buffer buffer) {
|
|
130 |
if (position < 0 || position > buffer.capacity()) {
|
|
131 |
throw new IllegalArgumentException("position: " + position);
|
|
132 |
}
|
|
133 |
if (limit < 0 || limit > buffer.capacity()) {
|
|
134 |
throw new IllegalArgumentException("limit: " + limit);
|
|
135 |
}
|
|
136 |
if (limit < position) {
|
|
137 |
throw new IllegalArgumentException
|
|
138 |
("limit < position: limit=" + limit + ", position=" + position);
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
|
142 |
void select(int newPos, int newLim, Buffer buffer) {
|
|
143 |
int oldPos = buffer.position();
|
|
144 |
int oldLim = buffer.limit();
|
|
145 |
assert 0 <= oldPos && oldPos <= oldLim && oldLim <= buffer.capacity();
|
|
146 |
if (oldLim <= newPos) {
|
|
147 |
buffer().limit(newLim).position(newPos);
|
|
148 |
} else {
|
|
149 |
buffer.position(newPos).limit(newLim);
|
|
150 |
}
|
|
151 |
}
|
|
152 |
|
|
153 |
// The same as dst.put(src)
|
|
154 |
static <T extends Buffer> T put(T dst, T src) {
|
|
155 |
if (dst instanceof ByteBuffer) {
|
|
156 |
((ByteBuffer) dst).put((ByteBuffer) src);
|
|
157 |
} else if (dst instanceof CharBuffer) {
|
|
158 |
((CharBuffer) dst).put((CharBuffer) src);
|
|
159 |
} else {
|
|
160 |
// We don't work with buffers of other types
|
|
161 |
throw new IllegalArgumentException();
|
|
162 |
}
|
|
163 |
return dst;
|
|
164 |
}
|
|
165 |
|
|
166 |
// TODO: Remove when JDK-8150785 has been done
|
|
167 |
@SuppressWarnings("unchecked")
|
|
168 |
static <T extends Buffer> T slice(T buffer) {
|
|
169 |
if (buffer instanceof ByteBuffer) {
|
|
170 |
return (T) ((ByteBuffer) buffer).slice();
|
|
171 |
} else if (buffer instanceof CharBuffer) {
|
|
172 |
return (T) ((CharBuffer) buffer).slice();
|
|
173 |
} else {
|
|
174 |
// We don't work with buffers of other types
|
|
175 |
throw new IllegalArgumentException();
|
|
176 |
}
|
|
177 |
}
|
|
178 |
|
|
179 |
// TODO: Remove when JDK-8150785 has been done
|
|
180 |
@SuppressWarnings("unchecked")
|
|
181 |
static <T extends Buffer> T duplicate(T buffer) {
|
|
182 |
if (buffer instanceof ByteBuffer) {
|
|
183 |
return (T) ((ByteBuffer) buffer).duplicate();
|
|
184 |
} else if (buffer instanceof CharBuffer) {
|
|
185 |
return (T) ((CharBuffer) buffer).duplicate();
|
|
186 |
} else {
|
|
187 |
// We don't work with buffers of other types
|
|
188 |
throw new IllegalArgumentException();
|
|
189 |
}
|
|
190 |
}
|
|
191 |
|
|
192 |
@Override
|
|
193 |
public String toString() {
|
|
194 |
return super.toString() + "[" + WSUtils.toString(buffer()) + "]";
|
|
195 |
}
|
|
196 |
|
|
197 |
private void checkDisposed() {
|
|
198 |
if (disposed.get()) {
|
|
199 |
throw new IllegalStateException("Has been disposed previously");
|
|
200 |
}
|
|
201 |
}
|
|
202 |
}
|