mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
refactor(core): gzip in-memory content through one Gzip.compress helper (#9364)
Four services gzipped a byte array held in memory with their own copy of the same lines: CloudTrail's log file, a CloudWatch Logs subscription payload, a VPC flow log file and each line of a Redshift audit log. core.common.Gzip.compress now does it once, and the four call it. Writing to memory cannot fail, so the helper rethrows the compressor's IOException unchecked rather than handing it to every caller; each caller already caught it as a runtime failure or had no way to reach it. Its buffer starts small and grows with the compressed output, as the flow log and Redshift writers' did, rather than being sized from the input. Writers that compress as they go, into a file or a stream they hand on (DynamoDB's export, Firehose's per-format encoder, the EMR and Redshift UNLOAD writers), keep their own GZIPOutputStream.
This commit is contained in:
1 parent
bb579d5fe4
commit
e15b64d07b
6 files changed
+90
-46
No files matched your search
@@ -0,0 +1,47 @@
|
||||
package io.floci.aws.core.common;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class GzipTest {
|
||||
|
||||
@Test
|
||||
void compressedContentGunzipsBackToTheSameBytes() throws IOException {
|
||||
byte[] content = "{\"Records\":[{\"eventName\":\"PutObject\"}]}\n".repeat(500).getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
assertArrayEquals(content, gunzip(Gzip.compress(content)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyContentIsStillAGzipMember() throws IOException {
|
||||
byte[] compressed = Gzip.compress(new byte[0]);
|
||||
|
||||
assertEquals((byte) 0x1f, compressed[0]);
|
||||
assertEquals((byte) 0x8b, compressed[1]);
|
||||
assertArrayEquals(new byte[0], gunzip(compressed));
|
||||
}
|
||||
|
||||
@Test
|
||||
void membersCompressedApartReadBackAsOneStreamWhenJoined() throws IOException {
|
||||
// A log file grown one member at a time, as Redshift's audit log is, reads as the lines in order.
|
||||
ByteArrayOutputStream joined = new ByteArrayOutputStream();
|
||||
joined.write(Gzip.compress("first\n".getBytes(StandardCharsets.UTF_8)));
|
||||
joined.write(Gzip.compress("second\n".getBytes(StandardCharsets.UTF_8)));
|
||||
|
||||
assertEquals("first\nsecond\n", new String(gunzip(joined.toByteArray()), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private static byte[] gunzip(byte[] compressed) throws IOException {
|
||||
try (GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(compressed))) {
|
||||
return in.readAllBytes();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user