From 83cd970ce425fab862b25924e88bed07beb3cad8 Mon Sep 17 00:00:00 2001 From: Steve Kradel Date: Tue, 9 Jul 2024 11:16:19 -0400 Subject: [PATCH] Use Guava ByteStreams.copy instead of checking InputStream.available --- .../common/clients/service/ReportingService.java | 16 ++++------------ .../direct/ingestion/DataIngesterService.java | 6 ++---- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/wavefront/sdk/common/clients/service/ReportingService.java b/src/main/java/com/wavefront/sdk/common/clients/service/ReportingService.java index 0eb3868c..c8950e3c 100644 --- a/src/main/java/com/wavefront/sdk/common/clients/service/ReportingService.java +++ b/src/main/java/com/wavefront/sdk/common/clients/service/ReportingService.java @@ -2,6 +2,7 @@ import com.google.common.annotations.VisibleForTesting; +import com.google.common.io.ByteStreams; import com.wavefront.sdk.common.Constants; import com.wavefront.sdk.common.Utils; import com.wavefront.sdk.common.clients.service.token.TokenService; @@ -82,10 +83,7 @@ public int send(String format, InputStream stream) { urlConn.setReadTimeout(READ_TIMEOUT_MILLIS); try (GZIPOutputStream gzipOS = new GZIPOutputStream(urlConn.getOutputStream())) { - byte[] buffer = new byte[BUFFER_SIZE]; - while (stream.available() > 0) { - gzipOS.write(buffer, 0, stream.read(buffer)); - } + ByteStreams.copy(stream, gzipOS); gzipOS.flush(); } statusCode = urlConn.getResponseCode(); @@ -125,20 +123,14 @@ public int sendEvent(InputStream stream) { urlConn.addRequestProperty("Content-Type", "application/octet-stream"); urlConn.addRequestProperty("Content-Encoding", "gzip"); try (GZIPOutputStream gzipOS = new GZIPOutputStream(urlConn.getOutputStream())) { - byte[] buffer = new byte[BUFFER_SIZE]; - while (stream.available() > 0) { - gzipOS.write(buffer, 0, stream.read(buffer)); - } + ByteStreams.copy(stream, gzipOS); gzipOS.flush(); } } else { // Event is in uncompressed JSON format for direct ingestion. urlConn.addRequestProperty("Content-Type", "application/json"); try (OutputStream urlOS = urlConn.getOutputStream()) { - byte[] buffer = new byte[BUFFER_SIZE]; - while (stream.available() > 0) { - urlOS.write(buffer, 0, stream.read(buffer)); - } + ByteStreams.copy(stream, urlOS); urlOS.flush(); } } diff --git a/src/main/java/com/wavefront/sdk/direct/ingestion/DataIngesterService.java b/src/main/java/com/wavefront/sdk/direct/ingestion/DataIngesterService.java index a9f1588f..c02da324 100644 --- a/src/main/java/com/wavefront/sdk/direct/ingestion/DataIngesterService.java +++ b/src/main/java/com/wavefront/sdk/direct/ingestion/DataIngesterService.java @@ -1,5 +1,6 @@ package com.wavefront.sdk.direct.ingestion; +import com.google.common.io.ByteStreams; import com.wavefront.sdk.common.clients.WavefrontClientFactory; import com.wavefront.sdk.common.clients.service.ReportingService; @@ -60,10 +61,7 @@ public int report(String format, InputStream stream) { urlConn.setReadTimeout(READ_TIMEOUT_MILLIS); try (GZIPOutputStream gzipOS = new GZIPOutputStream(urlConn.getOutputStream())) { - byte[] buffer = new byte[4096]; - while (stream.available() > 0) { - gzipOS.write(buffer, 0, stream.read(buffer)); - } + ByteStreams.copy(stream, gzipOS); gzipOS.flush(); } statusCode = urlConn.getResponseCode();