Skip to content

Commit

Permalink
Some changes to IOBundle.java
Browse files Browse the repository at this point in the history
  • Loading branch information
retrodaredevil committed Sep 10, 2019
1 parent 7ffeefd commit d142dfe
Showing 1 changed file with 60 additions and 3 deletions.
63 changes: 60 additions & 3 deletions src/main/java/me/retrodaredevil/io/IOBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.InputStream;
import java.io.OutputStream;

import static java.util.Objects.requireNonNull;

/**
* Represents a {@link InputStream} and an {@link OutputStream}
* <p>
Expand All @@ -15,12 +17,67 @@ public interface IOBundle extends AutoCloseable {

/**
* This should be overridden by subclasses to close the input and output streams
*
* @throws Exception If this cannot be closed
*/
@Override
default void close() throws Exception {
getInputStream().close();
getOutputStream().close();
}

/**
* NOTE: The {@link #close()} method will do nothing on the instance returned by this method
* @param inputStream The {@link InputStream} to return in {@link #getInputStream()}
* @param outputStream The {@link OutputStream} to return in {@link #getOutputStream()}
* @return An immutable {@link IOBundle} representing {@code inputStream} and {@code outputStream}
*/
static IOBundle of(final InputStream inputStream, final OutputStream outputStream){
requireNonNull(inputStream);
requireNonNull(outputStream);
return new IOBundle() {
@Override
public InputStream getInputStream() {
return inputStream;
}

@Override
public OutputStream getOutputStream() {
return outputStream;
}
};
}

final class Defaults {
private Defaults(){ throw new UnsupportedOperationException(); }
/**
* Represents an {@link IOBundle} that returns {@link System#in} and {@link System#out}
* <p>
* NOTE: Calling {@link #close()} does nothing.
*/
public static IOBundle STANDARD_IN_OUT = new IOBundle() {
@Override
public InputStream getInputStream() {
return System.in;
}

@Override
public OutputStream getOutputStream() {
return System.out;
}
};
/**
* Represents an {@link IOBundle} that returns {@link System#in} and {@link System#err}
* <p>
* NOTE: Calling {@link #close()} does nothing.
*/
public static IOBundle STANDARD_IN_ERR = new IOBundle() {
@Override
public InputStream getInputStream() {
return System.in;
}

@Override
public OutputStream getOutputStream() {
return System.err;
}
};
}
}

0 comments on commit d142dfe

Please sign in to comment.