From df804879ef7f32943497a9651c232e2d53f95915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Bilger?= Date: Wed, 10 Jan 2024 00:14:28 +0100 Subject: [PATCH] remove log4j-CloseShieldOutputStream usage (#779) --- .../stream/WriteableStreamArchive.java | 2 +- .../util/CloseShieldOutputStream.java | 19 ++++++++ .../planetiler/util/CountingOutputStream.java | 21 ++------- .../util/DelegatingOutputStream.java | 38 +++++++++++++++ .../util/CloseShieldOutputStreamTest.java | 38 +++++++++++++++ .../util/CountingOutputStreamTest.java | 47 +++++++++++++++++++ 6 files changed, 148 insertions(+), 17 deletions(-) create mode 100644 planetiler-core/src/main/java/com/onthegomap/planetiler/util/CloseShieldOutputStream.java create mode 100644 planetiler-core/src/main/java/com/onthegomap/planetiler/util/DelegatingOutputStream.java create mode 100644 planetiler-core/src/test/java/com/onthegomap/planetiler/util/CloseShieldOutputStreamTest.java create mode 100644 planetiler-core/src/test/java/com/onthegomap/planetiler/util/CountingOutputStreamTest.java diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/stream/WriteableStreamArchive.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/stream/WriteableStreamArchive.java index d902f3e0..31f84c1a 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/stream/WriteableStreamArchive.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/stream/WriteableStreamArchive.java @@ -3,6 +3,7 @@ package com.onthegomap.planetiler.stream; import com.onthegomap.planetiler.archive.WriteableTileArchive; import com.onthegomap.planetiler.geo.TileOrder; import com.onthegomap.planetiler.stats.Counter; +import com.onthegomap.planetiler.util.CloseShieldOutputStream; import com.onthegomap.planetiler.util.CountingOutputStream; import java.io.IOException; import java.io.OutputStream; @@ -12,7 +13,6 @@ import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.util.concurrent.atomic.AtomicInteger; -import org.apache.logging.log4j.core.util.CloseShieldOutputStream; /** * Base archive for all kinds of simple file streams. This is primarily useful when the file is a named pipe. In that diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/CloseShieldOutputStream.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/CloseShieldOutputStream.java new file mode 100644 index 00000000..101ad4b0 --- /dev/null +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/CloseShieldOutputStream.java @@ -0,0 +1,19 @@ +package com.onthegomap.planetiler.util; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * {@link OutputStream} decorator that suppresses {@link #close()}. + */ +public class CloseShieldOutputStream extends DelegatingOutputStream { + + public CloseShieldOutputStream(OutputStream wrapped) { + super(wrapped); + } + + @Override + public void close() throws IOException { + // suppress closing + } +} diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/CountingOutputStream.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/CountingOutputStream.java index efb5881d..dc263313 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/CountingOutputStream.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/CountingOutputStream.java @@ -7,41 +7,30 @@ import java.util.function.LongConsumer; /** * {@link OutputStream} decorator that notifies the callback about the written bytes. */ -public class CountingOutputStream extends OutputStream { +public class CountingOutputStream extends DelegatingOutputStream { - private final OutputStream wrapped; private final LongConsumer writtenBytesConsumer; public CountingOutputStream(OutputStream wrapped, LongConsumer writtenBytesConsumer) { - this.wrapped = wrapped; + super(wrapped); this.writtenBytesConsumer = writtenBytesConsumer; } @Override public void write(int i) throws IOException { - wrapped.write(i); + super.write(i); writtenBytesConsumer.accept(1L); } @Override public void write(byte[] b) throws IOException { - wrapped.write(b); + super.write(b); writtenBytesConsumer.accept(b.length); } @Override public void write(byte[] b, int off, int len) throws IOException { - wrapped.write(b, off, len); + super.write(b, off, len); writtenBytesConsumer.accept(len); } - - @Override - public void flush() throws IOException { - wrapped.flush(); - } - - @Override - public void close() throws IOException { - wrapped.close(); - } } diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/DelegatingOutputStream.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/DelegatingOutputStream.java new file mode 100644 index 00000000..fd11b167 --- /dev/null +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/DelegatingOutputStream.java @@ -0,0 +1,38 @@ +package com.onthegomap.planetiler.util; + +import java.io.IOException; +import java.io.OutputStream; + +abstract class DelegatingOutputStream extends OutputStream { + + private final OutputStream delegate; + + protected DelegatingOutputStream(OutputStream wrapped) { + this.delegate = wrapped; + } + + @Override + public void write(int i) throws IOException { + delegate.write(i); + } + + @Override + public void write(byte[] b) throws IOException { + delegate.write(b); + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + delegate.write(b, off, len); + } + + @Override + public void flush() throws IOException { + delegate.flush(); + } + + @Override + public void close() throws IOException { + delegate.close(); + } +} diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/util/CloseShieldOutputStreamTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/util/CloseShieldOutputStreamTest.java new file mode 100644 index 00000000..e667183d --- /dev/null +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/util/CloseShieldOutputStreamTest.java @@ -0,0 +1,38 @@ +package com.onthegomap.planetiler.util; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +import java.io.IOException; +import java.io.OutputStream; +import org.junit.jupiter.api.Test; + +class CloseShieldOutputStreamTest { + + @Test + void test() throws IOException { + final OutputStream delegate = mock(OutputStream.class); + final OutputStream os = new CloseShieldOutputStream(delegate); + + os.close(); + verifyNoMoreInteractions(delegate); + + os.write(1); + verify(delegate).write(1); + verifyNoMoreInteractions(delegate); + + os.write(new byte[]{2}); + verify(delegate).write(new byte[]{2}); + verifyNoMoreInteractions(delegate); + + os.write(new byte[]{3}, 4, 5); + verify(delegate).write(new byte[]{3}, 4, 5); + verifyNoMoreInteractions(delegate); + + os.flush(); + verify(delegate).flush(); + verifyNoMoreInteractions(delegate); + } + +} diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/util/CountingOutputStreamTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/util/CountingOutputStreamTest.java new file mode 100644 index 00000000..a06fe18e --- /dev/null +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/util/CountingOutputStreamTest.java @@ -0,0 +1,47 @@ +package com.onthegomap.planetiler.util; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +import com.onthegomap.planetiler.stats.Counter; +import java.io.IOException; +import java.io.OutputStream; +import org.junit.jupiter.api.Test; + +class CountingOutputStreamTest { + + @Test + void test() throws IOException { + + final OutputStream delegate = mock(OutputStream.class); + final var c = Counter.newSingleThreadCounter(); + final OutputStream os = new CountingOutputStream(delegate, c::incBy); + + os.close(); + verify(delegate).close(); + assertEquals(0, c.get()); + + os.write(1); + verify(delegate).write(1); + verifyNoMoreInteractions(delegate); + assertEquals(1L, c.get()); + + os.write(new byte[]{2, 3}); + verify(delegate).write(new byte[]{2, 3}); + verifyNoMoreInteractions(delegate); + assertEquals(1L + 2L, c.get()); + + os.write(new byte[]{4, 5, 6}, 7, 8); + verify(delegate).write(new byte[]{4, 5, 6}, 7, 8); + verifyNoMoreInteractions(delegate); + assertEquals(1L + 2L + 8L, c.get()); + + os.flush(); + verify(delegate).flush(); + verifyNoMoreInteractions(delegate); + assertEquals(1L + 2L + 8L, c.get()); + } + +}