Skip to content

Commit

Permalink
Use an inner stream for close shield streams yegor256#1449
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier B. OURA committed Mar 13, 2021
1 parent 81a405f commit df0f657
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
24 changes: 13 additions & 11 deletions src/main/java/org/cactoos/io/SafeInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicReference;

/**
* Decorator of {@link InputStream} to prevent it
Expand All @@ -37,63 +38,64 @@
public final class SafeInputStream extends InputStream {

/**
* Origin stream.
* Inner {@link InputStream}.
*/
private final InputStream origin;
private final AtomicReference<InputStream> inner;

/**
* Ctor.
* @param origin Origin
*/
public SafeInputStream(final InputStream origin) {
super();
this.origin = origin;
this.inner = new AtomicReference<>(origin);
}

@Override
public int read() throws IOException {
return this.origin.read();
return this.inner.get().read();
}

@Override
public int read(final byte[] buffer) throws IOException {
return this.origin.read(buffer);
return this.inner.get().read(buffer);
}

@Override
public int read(final byte[] buffer, final int offset,
final int length) throws IOException {
return this.origin.read(buffer, offset, length);
return this.inner.get().read(buffer, offset, length);
}

@Override
@SuppressWarnings("PMD.UncommentedEmptyMethodBody")
public void close() throws IOException {
this.inner.set(new DeadInputStream());
}

@Override
public long skip(final long num) throws IOException {
return this.origin.skip(num);
return this.inner.get().skip(num);
}

@Override
public int available() throws IOException {
return this.origin.available();
return this.inner.get().available();
}

@Override
public void mark(final int limit) {
this.origin.mark(limit);
this.inner.get().mark(limit);
}

@Override
public void reset() throws IOException {
this.origin.reset();
this.inner.get().reset();
}

@Override
public boolean markSupported() {
return this.origin.markSupported();
return this.inner.get().markSupported();
}

}
17 changes: 9 additions & 8 deletions src/main/java/org/cactoos/io/SafeOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.atomic.AtomicReference;

/**
* Decorator of {@link OutputStream} to prevent it
Expand All @@ -37,43 +38,43 @@
public final class SafeOutputStream extends OutputStream {

/**
* Origin.
* Inner {@link OutputStream}.
*/
private final OutputStream origin;
private final AtomicReference<OutputStream> inner;

/**
* Ctor.
* @param origin Origin
*/
public SafeOutputStream(final OutputStream origin) {
super();
this.origin = origin;
this.inner = new AtomicReference<>(origin);
}

@Override
public void write(final int data) throws IOException {
this.origin.write(data);
this.inner.get().write(data);
}

@Override
public void write(final byte[] buf) throws IOException {
this.origin.write(buf);
this.inner.get().write(buf);
}

@Override
public void write(final byte[] buf, final int off, final int len)
throws IOException {
this.origin.write(buf, off, len);
this.inner.get().write(buf, off, len);
}

@Override
public void flush() throws IOException {
this.origin.flush();
this.inner.get().flush();
}

@Override
@SuppressWarnings("PMD.UncommentedEmptyMethodBody")
public void close() throws IOException {
this.inner.set(new DeadOutputStream());
}

}

0 comments on commit df0f657

Please sign in to comment.