Skip to content

Commit

Permalink
Fix the JDK8/JDK11 mixed env for ByteBuffer
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Supol <jan.supol@oracle.com>
  • Loading branch information
jansupol committed Jul 8, 2020
1 parent dcd3936 commit b7b9b0d
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.text.ParseException;
import java.util.ArrayList;
Expand Down Expand Up @@ -694,7 +695,7 @@ public void handle(ByteBuffer data) {
+ 1) * BUFFER_STEP_SIZE : newSize;
final ByteBuffer result = ByteBuffer.allocate(roundedSize > incomingBufferSize ? newSize
: roundedSize);
result.flip();
((Buffer) result).flip();
data = Utils.appendBuffers(result, data, incomingBufferSize, BUFFER_STEP_SIZE);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.glassfish.tyrus.container.jdk.client;

import java.io.UnsupportedEncodingException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -44,7 +45,7 @@ class HttpResponseParser {

HttpResponseParser() {
buffer = ByteBuffer.allocate(1024);
buffer.flip(); //buffer created for read
((Buffer) buffer).flip(); //buffer created for read
}

TyrusUpgradeResponse parseUpgradeResponse() throws ParseException {
Expand Down Expand Up @@ -84,11 +85,11 @@ void appendData(ByteBuffer data) throws ParseException {
}

int limit = data.limit();
data.limit(responseEndPosition + 1);
((Buffer) data).limit(responseEndPosition + 1);
checkResponseSize(data);
buffer = Utils.appendBuffers(buffer, data, BUFFER_MAX_SIZE, BUFFER_STEP_SIZE);
data.limit(limit);
data.position(responseEndPosition + 1);
((Buffer) data).limit(limit);
((Buffer) data).position(responseEndPosition + 1);
complete = true;
}

Expand Down Expand Up @@ -160,8 +161,8 @@ void destroy() {
}

void clear() {
buffer.clear();
buffer.flip();
((Buffer) buffer).clear();
((Buffer) buffer).flip();
complete = false;
findEndState = State.INIT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.glassfish.tyrus.container.jdk.client;

import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.LinkedList;
import java.util.Queue;
Expand Down Expand Up @@ -172,7 +173,7 @@ synchronized void write(final ByteBuffer applicationData, final CompletionHandle
private void handleWrite(final ByteBuffer applicationData, final CompletionHandler<ByteBuffer> completionHandler) {
try {
// transport buffer always writes all data, so there are not leftovers in the networkOutputBuffer
networkOutputBuffer.clear();
((Buffer) networkOutputBuffer).clear();
SSLEngineResult result = sslEngine.wrap(applicationData, networkOutputBuffer);

switch (result.getStatus()) {
Expand Down Expand Up @@ -202,7 +203,7 @@ BUFFER_UNDERFLOW can occur only after unwrap(), but to be 100% sure we handle al
state = State.REHANDSHAKING;
}

networkOutputBuffer.flip();
((Buffer) networkOutputBuffer).flip();
// write only if something was written to the output buffer
if (networkOutputBuffer.hasRemaining()) {
writeQueue.write(networkOutputBuffer, new CompletionHandler<ByteBuffer>() {
Expand Down Expand Up @@ -300,7 +301,7 @@ BUFFER_UNDERFLOW can occur only after unwrap(), but to be 100% sure we handle al

if (lazyBuffer.isAllocated()) {
ByteBuffer buffer = lazyBuffer.get();
buffer.flip();
((Buffer) buffer).flip();
writeQueue.write(buffer, new CompletionHandler<ByteBuffer>() {

@Override
Expand Down Expand Up @@ -348,7 +349,7 @@ boolean processRead(ByteBuffer networkData) {

case CLOSED: {
// drop any data that arrive after the SSL has been closed
networkData.clear();
((Buffer) networkData).clear();
readMore = false;
}
}
Expand All @@ -359,7 +360,7 @@ boolean processRead(ByteBuffer networkData) {

private boolean handleRead(ByteBuffer networkData) {
try {
applicationInputBuffer.clear();
((Buffer) applicationInputBuffer).clear();
SSLEngineResult result = sslEngine.unwrap(networkData, applicationInputBuffer);

switch (result.getStatus()) {
Expand All @@ -379,7 +380,7 @@ private boolean handleRead(ByteBuffer networkData) {
case CLOSED:
case OK: {
if (result.bytesProduced() > 0) {
applicationInputBuffer.flip();
((Buffer) applicationInputBuffer).flip();
upstreamFilter.onRead(applicationInputBuffer);
applicationInputBuffer.compact();
}
Expand Down Expand Up @@ -487,7 +488,7 @@ that BUFFER_UNDERFLOW can occur only after unwrap(), but to be 100% sure we hand

SSLEngineResult result = sslEngine.unwrap(networkData, applicationInputBuffer);

applicationInputBuffer.flip();
((Buffer) applicationInputBuffer).flip();
if (applicationInputBuffer.hasRemaining()) {
// data can flow during re-handshake
inputBuffer.append(applicationInputBuffer);
Expand Down Expand Up @@ -537,7 +538,7 @@ that BUFFER_UNDERFLOW can occur only after unwrap(), but to be 100% sure we hand
// now write the stored wrap() results
if (outputBuffer.isAllocated()) {
ByteBuffer buffer = outputBuffer.get();
buffer.flip();
((Buffer) buffer).flip();
writeQueue.write(buffer, null);
}

Expand Down Expand Up @@ -660,16 +661,16 @@ void resize() {
int increment = sslEngine.getSession().getPacketBufferSize();
int newSize = buffer.position() + increment;
ByteBuffer newBuffer = ByteBuffer.allocate(newSize);
buffer.flip();
newBuffer.flip();
((Buffer) buffer).flip();
((Buffer) newBuffer).flip();
buffer = Utils.appendBuffers(newBuffer, buffer, newBuffer.limit(), 50);
buffer.compact();
}

void append(ByteBuffer b) {
if (buffer == null) {
buffer = ByteBuffer.allocate(b.remaining());
buffer.flip();
((Buffer) buffer).flip();
}
int newSize = buffer.limit() + b.remaining();
buffer = Utils.appendBuffers(buffer, b, newSize, 50);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousChannelGroup;
import java.nio.channels.AsynchronousCloseException;
Expand Down Expand Up @@ -290,7 +291,7 @@ public void completed(Integer bytesRead, Void result) {
return;
}

inputBuffer.flip();
((Buffer) inputBuffer).flip();
onRead(inputBuffer);
inputBuffer.compact();
_read(inputBuffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.glassfish.tyrus.servlet;

import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
Expand Down Expand Up @@ -171,7 +172,7 @@ private int fillBuf(int length) throws IOException {
if (buf == null) {
LOGGER.finest("No Buffer. Allocating new one");
buf = ByteBuffer.wrap(data);
buf.limit(len);
((Buffer) buf).limit(len);
} else {
int limit = buf.limit();
int capacity = buf.capacity();
Expand All @@ -180,26 +181,26 @@ private int fillBuf(int length) throws IOException {
if (capacity - limit >= len) {
// Remaining data need not be changed. New data is just appended
LOGGER.finest("Remaining data need not be moved. New data is just appended");
buf.mark();
buf.position(limit);
buf.limit(capacity);
((Buffer) buf).mark();
((Buffer) buf).position(limit);
((Buffer) buf).limit(capacity);
buf.put(data, 0, len);
buf.limit(limit + len);
buf.reset();
((Buffer) buf).limit(limit + len);
((Buffer) buf).reset();
} else if (remaining + len < capacity) {
// Remaining data is moved to left. Then new data is appended
LOGGER.finest("Remaining data is moved to left. Then new data is appended");
buf.compact();
buf.put(data, 0, len);
buf.flip();
((Buffer) buf).flip();
} else {
// Remaining data + new > capacity. So allocate new one
LOGGER.finest("Remaining data + new > capacity. So allocate new one");
byte[] array = new byte[remaining + len];
buf.get(array, 0, remaining);
System.arraycopy(data, 0, array, remaining, len);
buf = ByteBuffer.wrap(array);
buf.limit(remaining + len);
((Buffer) buf).limit(remaining + len);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.glassfish.tyrus.core;

import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -68,7 +69,7 @@ ByteBuffer getBufferedContent() {
b.put(buffered);
}

b.flip();
((Buffer) b).flip();
resetBuffer(0);
return b;
}
Expand Down
26 changes: 13 additions & 13 deletions core/src/main/java/org/glassfish/tyrus/core/StrictUtf8.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ private static char lowSurrogate(int codePoint) {
}

private static void updatePositions(ByteBuffer src, int sp, CharBuffer dst, int dp) {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
((Buffer) src).position(sp - src.arrayOffset());
((Buffer) dst).position(dp - dst.arrayOffset());
}

private static void updatePositions(CharBuffer src, int sp, ByteBuffer dst, int dp) {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
((Buffer) src).position(sp - src.arrayOffset());
((Buffer) dst).position(dp - dst.arrayOffset());
}

private static class Decoder extends CharsetDecoder {
Expand Down Expand Up @@ -166,17 +166,17 @@ private static CoderResult malformedN(ByteBuffer src, int nb) {
}

private static CoderResult malformed(ByteBuffer src, int sp, CharBuffer dst, int dp, int nb) {
src.position(sp - src.arrayOffset());
((Buffer) src).position(sp - src.arrayOffset());
CoderResult cr = malformedN(src, nb);
updatePositions(src, sp, dst, dp);
return cr;
}


private static CoderResult malformed(ByteBuffer src, int mark, int nb) {
src.position(mark);
((Buffer) src).position(mark);
CoderResult cr = malformedN(src, nb);
src.position(mark);
((Buffer) src).position(mark);
return cr;
}

Expand All @@ -186,7 +186,7 @@ private static CoderResult malformedForLength(ByteBuffer src, int sp, CharBuffer
}

private static CoderResult malformedForLength(ByteBuffer src, int mark, int malformedNB) {
src.position(mark);
((Buffer) src).position(mark);
return CoderResult.malformedForLength(malformedNB);
}

Expand All @@ -197,7 +197,7 @@ private static CoderResult xflow(ByteBuffer src, int sp, int sl, CharBuffer dst,
}

private static CoderResult xflow(Buffer src, int mark, int nb) {
src.position(mark);
((Buffer) src).position(mark);
return (nb == 0 || src.remaining() < nb) ? CoderResult.UNDERFLOW : CoderResult.OVERFLOW;
}

Expand Down Expand Up @@ -378,7 +378,7 @@ private static ByteBuffer getByteBuffer(ByteBuffer bb, byte[] ba, int sp) {
if (bb == null) {
bb = ByteBuffer.wrap(ba);
}
bb.position(sp);
((Buffer) bb).position(sp);
return bb;
}

Expand Down Expand Up @@ -529,7 +529,7 @@ private static CoderResult overflow(CharBuffer src, int sp, ByteBuffer dst, int
}

private static CoderResult overflow(CharBuffer src, int mark) {
src.position(mark);
((Buffer) src).position(mark);
return CoderResult.OVERFLOW;
}

Expand Down Expand Up @@ -621,7 +621,7 @@ private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
}
int uc = sgp.parse(c, src);
if (uc < 0) {
src.position(mark);
((Buffer) src).position(mark);
return sgp.error();
}
if (dst.remaining() < 4) {
Expand All @@ -643,7 +643,7 @@ private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
}
mark++;
}
src.position(mark);
((Buffer) src).position(mark);
return CoderResult.UNDERFLOW;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package org.glassfish.tyrus.core;

import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -507,7 +507,7 @@ public void handle(ByteBuffer data) {
? ((newSize / BUFFER_STEP_SIZE) + 1) * BUFFER_STEP_SIZE : newSize;
final ByteBuffer result =
ByteBuffer.allocate(roundedSize > incomingBufferSize ? newSize : roundedSize);
result.flip();
((Buffer) result).flip();
data = Utils.appendBuffers(result, data, incomingBufferSize, BUFFER_STEP_SIZE);
}
}
Expand Down
15 changes: 8 additions & 7 deletions core/src/main/java/org/glassfish/tyrus/core/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.glassfish.tyrus.core;

import java.net.URI;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -303,18 +304,18 @@ public static ByteBuffer appendBuffers(ByteBuffer buffer, ByteBuffer buffer1, in
// buffer1 will be appended to buffer
if (len < (capacity - limit)) {

buffer.mark();
buffer.position(limit);
buffer.limit(capacity);
((Buffer) buffer).mark();
((Buffer) buffer).position(limit);
((Buffer) buffer).limit(capacity);
buffer.put(buffer1);
buffer.limit(limit + len);
buffer.reset();
((Buffer) buffer).limit(limit + len);
((Buffer) buffer).reset();
return buffer;
// Remaining data is moved to left. Then new data is appended
} else if (remaining + len < capacity) {
buffer.compact();
buffer.put(buffer1);
buffer.flip();
((Buffer) buffer).flip();
return buffer;
// create new buffer
} else {
Expand All @@ -328,7 +329,7 @@ public static ByteBuffer appendBuffers(ByteBuffer buffer, ByteBuffer buffer1, in
final ByteBuffer result = ByteBuffer.allocate(roundedSize > incomingBufferSize ? newSize : roundedSize);
result.put(buffer);
result.put(buffer1);
result.flip();
((Buffer) result).flip();
return result;
}
}
Expand Down
Loading

0 comments on commit b7b9b0d

Please sign in to comment.