Skip to content

Commit

Permalink
refactor compressor code
Browse files Browse the repository at this point in the history
  • Loading branch information
osiegmar committed Mar 28, 2024
1 parent a41d586 commit d2c4912
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
19 changes: 17 additions & 2 deletions src/main/java/de/siegmar/logbackgelf/compressor/Compressor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,25 @@

package de.siegmar.logbackgelf.compressor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;

public interface Compressor {

default byte[] compress(byte[] binMessage) {
return binMessage;
default byte[] compress(final byte[] binMessage) {
final var bos = new ByteArrayOutputStream(binMessage.length);
try (var wrappedOut = wrap(bos)) {
wrappedOut.write(binMessage);
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
return bos.toByteArray();
}

default OutputStream wrap(final OutputStream out) throws IOException {
return out;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,15 @@

package de.siegmar.logbackgelf.compressor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;

public class GZIPCompressor implements Compressor {

@Override
public byte[] compress(final byte[] binMessage) {
final var bos = new ByteArrayOutputStream(binMessage.length);
try (var gzipOut = new GZIPOutputStream(bos)) {
gzipOut.write(binMessage);
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
return bos.toByteArray();
public OutputStream wrap(final OutputStream out) throws IOException {
return new GZIPOutputStream(out);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,14 @@

package de.siegmar.logbackgelf.compressor;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.OutputStream;
import java.util.zip.DeflaterOutputStream;

public class ZLIBCompressor implements Compressor {

@Override
public byte[] compress(final byte[] binMessage) {
final var bos = new ByteArrayOutputStream(binMessage.length);
try (var deflaterOut = new DeflaterOutputStream(bos)) {
deflaterOut.write(binMessage);
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
return bos.toByteArray();
public OutputStream wrap(final OutputStream out) {
return new DeflaterOutputStream(out);
}

}

0 comments on commit d2c4912

Please sign in to comment.