How to compress requests using the Spring RestTemplate. Updated GzipUtils code following editor review.
@@ -30,15 +30,11 @@ public class GzipUtils {
* @throws IOException
*/
public static byte[] compress(byte[] body) throws IOException {
- ByteArrayOutputStream output = new ByteArrayOutputStream();
- try {
- try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(output)) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(baos)) {
gzipOutputStream.write(body);
}
- } finally {
- output.close();
- }
- return output.toByteArray();
+ return baos.toByteArray();
}
/**
