Skip to content

Commit b8f482e

Browse files
committed
Restore support for Java 8 for RestClient
Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
1 parent 00dd577 commit b8f482e

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

client/rest/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ apply plugin: 'opensearch.build'
3434
apply plugin: 'opensearch.publish'
3535

3636
java {
37-
targetCompatibility = JavaVersion.VERSION_11
38-
sourceCompatibility = JavaVersion.VERSION_11
37+
targetCompatibility = JavaVersion.VERSION_1_8
38+
sourceCompatibility = JavaVersion.VERSION_1_8
3939
}
4040

4141
base {

client/rest/src/main/java/org/opensearch/client/RestClient.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,9 +1116,15 @@ public long getContentLength() {
11161116
if (chunkedEnabled.get()) {
11171117
return -1L;
11181118
} else {
1119-
long size;
1119+
long size = 0;
1120+
final byte[] buf = new byte[8192];
1121+
int nread = 0;
1122+
11201123
try (InputStream is = getContent()) {
1121-
size = is.readAllBytes().length;
1124+
// read to EOF which may read more or less than buffer size
1125+
while ((nread = is.read(buf)) > 0) {
1126+
size += nread;
1127+
}
11221128
} catch (IOException ex) {
11231129
size = -1L;
11241130
}

client/rest/src/test/java/org/opensearch/client/nio/HeapBufferedAsyncEntityConsumerTests.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,34 +35,34 @@ public void tearDown() {
3535
}
3636

3737
public void testConsumerAllocatesBufferLimit() throws IOException {
38-
consumer.consume(randomByteBufferOfLength(1000).flip());
38+
consumer.consume((ByteBuffer) randomByteBufferOfLength(1000).flip());
3939
assertThat(consumer.getBuffer().capacity(), equalTo(1000));
4040
}
4141

4242
public void testConsumerAllocatesEmptyBuffer() throws IOException {
43-
consumer.consume(ByteBuffer.allocate(0).flip());
43+
consumer.consume((ByteBuffer) ByteBuffer.allocate(0).flip());
4444
assertThat(consumer.getBuffer().capacity(), equalTo(0));
4545
}
4646

4747
public void testConsumerExpandsBufferLimits() throws IOException {
48-
consumer.consume(randomByteBufferOfLength(1000).flip());
49-
consumer.consume(randomByteBufferOfLength(2000).flip());
50-
consumer.consume(randomByteBufferOfLength(3000).flip());
48+
consumer.consume((ByteBuffer) randomByteBufferOfLength(1000).flip());
49+
consumer.consume((ByteBuffer) randomByteBufferOfLength(2000).flip());
50+
consumer.consume((ByteBuffer) randomByteBufferOfLength(3000).flip());
5151
assertThat(consumer.getBuffer().capacity(), equalTo(6000));
5252
}
5353

5454
public void testConsumerAllocatesLimit() throws IOException {
55-
consumer.consume(randomByteBufferOfLength(BUFFER_LIMIT).flip());
55+
consumer.consume((ByteBuffer) randomByteBufferOfLength(BUFFER_LIMIT).flip());
5656
assertThat(consumer.getBuffer().capacity(), equalTo(BUFFER_LIMIT));
5757
}
5858

5959
public void testConsumerFailsToAllocateOverLimit() throws IOException {
60-
assertThrows(ContentTooLongException.class, () -> consumer.consume(randomByteBufferOfLength(BUFFER_LIMIT + 1).flip()));
60+
assertThrows(ContentTooLongException.class, () -> consumer.consume((ByteBuffer) randomByteBufferOfLength(BUFFER_LIMIT + 1).flip()));
6161
}
6262

6363
public void testConsumerFailsToExpandOverLimit() throws IOException {
64-
consumer.consume(randomByteBufferOfLength(BUFFER_LIMIT).flip());
65-
assertThrows(ContentTooLongException.class, () -> consumer.consume(randomByteBufferOfLength(1).flip()));
64+
consumer.consume((ByteBuffer) randomByteBufferOfLength(BUFFER_LIMIT).flip());
65+
assertThrows(ContentTooLongException.class, () -> consumer.consume((ByteBuffer) randomByteBufferOfLength(1).flip()));
6666
}
6767

6868
private static ByteBuffer randomByteBufferOfLength(int length) {

client/test/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
apply plugin: 'opensearch.build'
3131

3232
java {
33-
targetCompatibility = JavaVersion.VERSION_11
34-
sourceCompatibility = JavaVersion.VERSION_11
33+
targetCompatibility = JavaVersion.VERSION_1_8
34+
sourceCompatibility = JavaVersion.VERSION_1_8
3535
}
3636

3737
base {

0 commit comments

Comments
 (0)