Skip to content

Commit

Permalink
remove log4j-CloseShieldOutputStream usage
Browse files Browse the repository at this point in the history
  • Loading branch information
bbilger committed Jan 9, 2024
1 parent d2b267a commit a9950de
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand All @@ -12,7 +13,6 @@
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,30 @@
/**
* {@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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit a9950de

Please sign in to comment.