Skip to content

Commit c811da6

Browse files
authored
Fix compatibility with Java 8 for ByteBuffer (spring-projects#3545)
When we build with Java 9+ and target for Java 8, we get an incompatible bytecode around these method for `ByteBuffer`: ``` position(int) limit(int) mark() reset() clear() flip() rewind() ``` The recommendation is to cast to `Buffer` when we call these methods * Fix all the production code using `ByteBuffer` for recommended cast to `Buffer` Related to https://build.spring.io/browse/INTEXT-AWS-306 See more info in this Jetty issue: jetty/jetty.project#3244
1 parent 3c27241 commit c811da6

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

spring-integration-core/src/main/java/org/springframework/integration/support/json/EmbeddedJsonHeadersMessageMapper.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2020 the original author or authors.
2+
* Copyright 2017-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020
import java.io.UncheckedIOException;
21+
import java.nio.Buffer;
2122
import java.nio.ByteBuffer;
2223
import java.util.Arrays;
2324
import java.util.Collection;
@@ -248,18 +249,18 @@ private Message<?> decodeNativeFormat(byte[] bytes, @Nullable Map<String, Object
248249
if (buffer.remaining() > 4) { // NOSONAR
249250
int headersLen = buffer.getInt();
250251
if (headersLen >= 0 && headersLen < buffer.remaining() - 4) { // NOSONAR
251-
buffer.position(headersLen + 4); // NOSONAR
252+
((Buffer) buffer).position(headersLen + 4); // NOSONAR
252253
int payloadLen = buffer.getInt();
253254
if (payloadLen != buffer.remaining()) {
254255
return null;
255256
}
256257
else {
257-
buffer.position(4); // NOSONAR
258+
((Buffer) buffer).position(4); // NOSONAR
258259
@SuppressWarnings("unchecked")
259260
Map<String, Object> headers = this.objectMapper.readValue(bytes, buffer.position(), headersLen,
260261
Map.class);
261262

262-
buffer.position(buffer.position() + headersLen);
263+
((Buffer) buffer).position(buffer.position() + headersLen);
263264
buffer.getInt();
264265
Object payload;
265266
byte[] payloadBytes = new byte[payloadLen];

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioSSLConnection.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.ip.tcp.connection;
1818

1919
import java.io.IOException;
20+
import java.nio.Buffer;
2021
import java.nio.ByteBuffer;
2122
import java.nio.channels.SocketChannel;
2223
import java.util.concurrent.Semaphore;
@@ -135,7 +136,7 @@ protected void sendToPipe(final ByteBuffer networkBuffer) throws IOException {
135136
networkBuffer.compact();
136137
}
137138
else {
138-
networkBuffer.clear();
139+
((Buffer) networkBuffer).clear();
139140
}
140141
if (logger.isDebugEnabled()) {
141142
logger.debug("sendToPipe.x " + resultToString(result) + ", remaining: " + networkBuffer.remaining());
@@ -160,7 +161,7 @@ private SSLEngineResult decode(ByteBuffer networkBuffer) throws IOException {
160161
case NEED_UNWRAP:
161162
case FINISHED:
162163
case NOT_HANDSHAKING:
163-
this.decoded.clear();
164+
((Buffer) this.decoded).clear();
164165
result = this.sslEngine.unwrap(networkBuffer, this.decoded);
165166
if (logger.isDebugEnabled()) {
166167
logger.debug("After unwrap: " + resultToString(result));
@@ -171,13 +172,13 @@ private SSLEngineResult decode(ByteBuffer networkBuffer) throws IOException {
171172
this.allocateEncryptionBuffer(this.sslEngine.getSession().getApplicationBufferSize());
172173
}
173174
if (result.bytesProduced() > 0) {
174-
this.decoded.flip();
175+
((Buffer) this.decoded).flip();
175176
super.sendToPipe(this.decoded);
176177
}
177178
break;
178179
case NEED_WRAP:
179180
if (!resumeWriterIfNeeded()) {
180-
this.encoded.clear();
181+
((Buffer) this.encoded).clear();
181182
result = this.sslEngine.wrap(networkBuffer, this.encoded);
182183
if (logger.isDebugEnabled()) {
183184
logger.debug("After wrap: " + resultToString(result));
@@ -186,7 +187,7 @@ private SSLEngineResult decode(ByteBuffer networkBuffer) throws IOException {
186187
this.encoded = this.allocateEncryptionBuffer(this.sslEngine.getSession().getPacketBufferSize());
187188
}
188189
else {
189-
this.encoded.flip();
190+
((Buffer) this.encoded).flip();
190191
getSSLChannelOutputStream().writeEncoded(this.encoded);
191192
}
192193
}
@@ -393,9 +394,9 @@ private void doClientSideHandshake(ByteBuffer plainText, SSLEngineResult resultA
393394
}
394395

395396
private void writeEncodedIfAny() throws IOException {
396-
TcpNioSSLConnection.this.encoded.flip();
397+
((Buffer) TcpNioSSLConnection.this.encoded).flip();
397398
writeEncoded(TcpNioSSLConnection.this.encoded);
398-
TcpNioSSLConnection.this.encoded.clear();
399+
((Buffer) TcpNioSSLConnection.this.encoded).clear();
399400
}
400401

401402
/**
@@ -430,7 +431,7 @@ else if (!isOpen()) {
430431
* Encrypts plain text data. The result may indicate handshaking is needed.
431432
*/
432433
private SSLEngineResult encode(ByteBuffer plainText) throws IOException {
433-
TcpNioSSLConnection.this.encoded.clear();
434+
((Buffer) TcpNioSSLConnection.this.encoded).clear();
434435
SSLEngineResult result =
435436
TcpNioSSLConnection.this.sslEngine.wrap(plainText, TcpNioSSLConnection.this.encoded);
436437
if (logger.isDebugEnabled()) {

spring-integration-ip/src/main/java/org/springframework/integration/ip/udp/DatagramPacketMessageMapper.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.io.UncheckedIOException;
2020
import java.io.UnsupportedEncodingException;
2121
import java.net.DatagramPacket;
22+
import java.nio.Buffer;
2223
import java.nio.ByteBuffer;
2324
import java.util.Map;
2425
import java.util.UUID;
@@ -303,7 +304,7 @@ private boolean startsWith(ByteBuffer buffer, String prefix) {
303304
}
304305
finally {
305306
//reposition the buffer
306-
buffer.position(pos);
307+
((Buffer) buffer).position(pos);
307308
}
308309
}
309310

0 commit comments

Comments
 (0)