Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove log4j-CloseShieldOutputStream usage #779

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}

}
Loading