Skip to content

Instantly share code, notes, and snippets.

@spieden
Last active August 18, 2016 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spieden/db215de328468f35ff7f577b4a19453a to your computer and use it in GitHub Desktop.
Save spieden/db215de328468f35ff7f577b4a19453a to your computer and use it in GitHub Desktop.
import clojure.lang.Delay;
import java.io.IOException;
import java.io.InputStream;
/**
* A lazily initialized InputStream using a Clojure delay.
*
* Construct with a delay that returns an InputStream. This delay will not be derefed until the
* DelayedInputStream is first accessed.
*/
public class DelayedInputStream extends InputStream {
private Delay delay;
private InputStream inputStream;
public DelayedInputStream() {
super();
}
public DelayedInputStream(Delay delay) {
super();
this.delay = delay;
this.inputStream = null;
}
private InputStream getInputStream() {
if (inputStream == null) {
this.inputStream = (InputStream) this.delay.deref();
}
return this.inputStream;
}
@Override
public int read() throws IOException {
return getInputStream().read();
}
@Override
public int read(byte[] b) throws IOException {
return getInputStream().read(b);
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
return getInputStream().read(b, off, len);
}
@Override
public long skip(long n) throws IOException {
return getInputStream().skip(n);
}
@Override
public int available() throws IOException {
return getInputStream().available();
}
@Override
public void close() throws IOException {
getInputStream().close();
}
@Override
public synchronized void mark(int readlimit) {
getInputStream().mark(readlimit);
}
@Override
public synchronized void reset() throws IOException {
getInputStream().reset();
}
@Override
public boolean markSupported() {
return getInputStream().markSupported();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment