Skip to content

Commit 703d69a

Browse files
Version 1.4.1 uses ByteBuffers in a backwards compatible way. (#107)
* Version 1.4.1 uses ByteBuffers in a backwards compatible way. Java 9 introduced a change in ByteBuffers causing some methods to fail when compiled with Java 9 (or newer) but executed on Java 8 (or older) regardless of compatability levels applied. See mongodb/mongo-java-driver@21c91bd * Fix comment spacing
1 parent c5ffd63 commit 703d69a

File tree

7 files changed

+51
-9
lines changed

7 files changed

+51
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 1.4.1 -- 2019-05-10
4+
5+
### Patches
6+
* Cast ByteBuffer to Buffer prior to using some methods so that it works properly in Java 8.
7+
38
## 1.4.0 -- 2019-05-10
49

510
### Minor Changes

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ You can get the latest release from Maven:
4545
<dependency>
4646
<groupId>com.amazonaws</groupId>
4747
<artifactId>aws-encryption-sdk-java</artifactId>
48-
<version>1.4.0</version>
48+
<version>1.4.1</version>
4949
</dependency>
5050
```
5151

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.amazonaws</groupId>
66
<artifactId>aws-encryption-sdk-java</artifactId>
7-
<version>1.4.0</version>
7+
<version>1.4.1</version>
88
<packaging>jar</packaging>
99

1010
<name>aws-encryption-sdk-java</name>

src/main/java/com/amazonaws/encryptionsdk/internal/EncryptionContextSerializer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static byte[] serialize(Map<String, String> encryptionContext) {
113113
}
114114

115115
// get and return the bytes that have been serialized
116-
result.flip();
116+
Utils.flip(result);
117117
final byte[] encryptionContextBytes = new byte[result.limit()];
118118
result.get(encryptionContextBytes);
119119

@@ -173,8 +173,8 @@ public static Map<String, String> deserialize(final byte[] b) {
173173
}
174174

175175
final ByteBuffer keyBytes = encryptionContextBytes.slice();
176-
keyBytes.limit(keyLen);
177-
encryptionContextBytes.position(encryptionContextBytes.position() + keyLen);
176+
Utils.limit(keyBytes, keyLen);
177+
Utils.position(encryptionContextBytes, encryptionContextBytes.position() + keyLen);
178178

179179
final int valueLen = encryptionContextBytes.getShort();
180180
if (valueLen <= 0 || valueLen > Short.MAX_VALUE) {
@@ -184,8 +184,8 @@ public static Map<String, String> deserialize(final byte[] b) {
184184

185185
// retrieve value
186186
final ByteBuffer valueBytes = encryptionContextBytes.slice();
187-
valueBytes.limit(valueLen);
188-
encryptionContextBytes.position(encryptionContextBytes.position() + valueLen);
187+
Utils.limit(valueBytes, valueLen);
188+
Utils.position(encryptionContextBytes, encryptionContextBytes.position() + valueLen);
189189

190190
final CharBuffer keyChars = decoder.decode(keyBytes);
191191
final CharBuffer valueChars = decoder.decode(valueBytes);

src/main/java/com/amazonaws/encryptionsdk/internal/FrameEncryptionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ private byte[] getNonce() {
359359
// We technically only allocate the low 32 bits for the frame number, and the other bits are defined to be
360360
// zero. However, since MAX_FRAME_NUMBER is 2^32-1, the high-order four bytes of the long will be zero, so the
361361
// big-endian representation will also have zeros in that position.
362-
buf.position(buf.limit() - Long.BYTES);
362+
Utils.position(buf, buf.limit() - Long.BYTES);
363363
buf.putLong(frameNumber_);
364364

365365
return nonce;

src/main/java/com/amazonaws/encryptionsdk/internal/Utils.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package com.amazonaws.encryptionsdk.internal;
1515

1616
import java.io.Serializable;
17+
import java.nio.Buffer;
1718
import java.nio.ByteBuffer;
1819
import java.nio.charset.StandardCharsets;
1920
import java.security.SecureRandom;
@@ -220,4 +221,40 @@ static byte[] generateContentAad(final byte[] messageId, final String idString,
220221
static IllegalArgumentException cannotBeNegative(String field) {
221222
return new IllegalArgumentException(field + " cannot be negative");
222223
}
224+
225+
/**
226+
* Equivalent to calling {@link ByteBuffer#flip()} but in a manner which is
227+
* safe when compiled on Java 9 or newer but used on Java 8 or older.
228+
*/
229+
public static ByteBuffer flip(final ByteBuffer buff) {
230+
((Buffer) buff).flip();
231+
return buff;
232+
}
233+
234+
/**
235+
* Equivalent to calling {@link ByteBuffer#clear()} but in a manner which is
236+
* safe when compiled on Java 9 or newer but used on Java 8 or older.
237+
*/
238+
public static ByteBuffer clear(final ByteBuffer buff) {
239+
((Buffer) buff).clear();
240+
return buff;
241+
}
242+
243+
/**
244+
* Equivalent to calling {@link ByteBuffer#position(int)} but in a manner which is
245+
* safe when compiled on Java 9 or newer but used on Java 8 or older.
246+
*/
247+
public static ByteBuffer position(final ByteBuffer buff, final int newPosition) {
248+
((Buffer) buff).position(newPosition);
249+
return buff;
250+
}
251+
252+
/**
253+
* Equivalent to calling {@link ByteBuffer#limit(int)} but in a manner which is
254+
* safe when compiled on Java 9 or newer but used on Java 8 or older.
255+
*/
256+
public static ByteBuffer limit(final ByteBuffer buff, final int newLimit) {
257+
((Buffer) buff).limit(newLimit);
258+
return buff;
259+
}
223260
}

src/test/java/com/amazonaws/encryptionsdk/internal/FrameEncryptionHandlerVeryLongTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void exhaustiveIVCheck() throws Exception {
3838
long lastIndex = 1; // starting index for the test
3939
long lastTS = System.nanoTime();
4040
for (long i = lastIndex; i <= Constants.MAX_FRAME_NUMBER; i++) {
41-
expectedNonce.clear();
41+
Utils.clear(expectedNonce);
4242
expectedNonce.order(ByteOrder.BIG_ENDIAN);
4343
expectedNonce.putInt(0);
4444
expectedNonce.putLong(i);

0 commit comments

Comments
 (0)