Package com.spotify.docker.client
Class DefaultLogStream
- java.lang.Object
-
- com.google.common.collect.UnmodifiableIterator<T>
-
- com.google.common.collect.AbstractIterator<LogMessage>
-
- com.spotify.docker.client.DefaultLogStream
-
- All Implemented Interfaces:
LogStream,java.io.Closeable,java.lang.AutoCloseable,java.util.Iterator<LogMessage>
class DefaultLogStream extends com.google.common.collect.AbstractIterator<LogMessage> implements LogStream
-
-
Constructor Summary
Constructors Modifier Constructor Description (package private)DefaultLogStream(LogReader reader)privateDefaultLogStream(java.io.InputStream stream)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidattach(java.io.OutputStream stdout, java.io.OutputStream stderr)Attaches twoOutputStreams to theLogStream.voidattach(java.io.OutputStream stdout, java.io.OutputStream stderr, boolean closeAtEof)Attaches twoOutputStreams to theLogStream.voidclose()Redefine to not throw checked exceptions.protected LogMessagecomputeNext()(package private) static DefaultLogStreamcreate(java.io.InputStream stream)java.lang.StringreadFully()private static voidwriteAndFlush(java.nio.ByteBuffer buffer, java.io.OutputStream outputStream)Write the contents of the given ByteBuffer to the OutputStream and flush the stream.-
Methods inherited from class com.google.common.collect.AbstractIterator
endOfData, hasNext, next, peek
-
-
-
-
Field Detail
-
reader
private final LogReader reader
-
-
Constructor Detail
-
DefaultLogStream
private DefaultLogStream(java.io.InputStream stream)
-
DefaultLogStream
DefaultLogStream(LogReader reader)
-
-
Method Detail
-
create
static DefaultLogStream create(java.io.InputStream stream)
-
computeNext
protected LogMessage computeNext()
- Specified by:
computeNextin classcom.google.common.collect.AbstractIterator<LogMessage>
-
close
public void close()
Description copied from interface:LogStreamRedefine to not throw checked exceptions.
-
attach
public void attach(java.io.OutputStream stdout, java.io.OutputStream stderr) throws java.io.IOExceptionDescription copied from interface:LogStreamAttaches twoOutputStreams to theLogStream. Closes the streams after use.- Specified by:
attachin interfaceLogStream- Parameters:
stdout- OutputStream for the standard outstderr- OutputStream for the standard err- Throws:
java.io.IOException- if an I/O error occurs- See Also:
for control over stream lifecycles
-
attach
public void attach(java.io.OutputStream stdout, java.io.OutputStream stderr, boolean closeAtEof) throws java.io.IOExceptionDescription copied from interface:LogStreamAttaches twoOutputStreams to theLogStream.Example usage:
dockerClient .attachContainer(containerId, AttachParameter.LOGS, AttachParameter.STDOUT, AttachParameter.STDERR, AttachParameter.STREAM) .attach(System.out, System.err);Typically you use
PipedOutputStreamconnected to aPipedInputStreamwhich are read by - for example - anInputStreamReaderor aScanner. For small inputs, thePipedOutputStreamjust writes to the buffer of thePipedInputStream, but you actually want to read and write from separate threads, as it may deadlock the thread.final PipedInputStream stdout = new PipedInputStream(); final PipedInputStream stderr = new PipedInputStream(); final PipedOutputStream stdout_pipe = new PipedOutputStream(stdout); final PipedOutputStream stderr_pipe = new PipedOutputStream(stderr); executor.submit(new Callable<Void>() { @Override public Void call() throws Exception { dockerClient.attachContainer(containerId, AttachParameter.LOGS, AttachParameter.STDOUT, AttachParameter.STDERR, AttachParameter.STREAM .attach(stdout_pipe, stderr_pipe); return null; } }); try (Scanner sc_stdout = new Scanner(stdout); Scanner sc_stderr = new Scanner(stderr)) { // ... read here }- Specified by:
attachin interfaceLogStream- Parameters:
stdout- OutputStream for the standard outstderr- OutputStream for the standard errcloseAtEof- whether to close the streams when this log stream ends- Throws:
java.io.IOException- if an I/O error occurs- See Also:
PipedInputStream,PipedOutputStream
-
writeAndFlush
private static void writeAndFlush(java.nio.ByteBuffer buffer, java.io.OutputStream outputStream) throws java.io.IOExceptionWrite the contents of the given ByteBuffer to the OutputStream and flush the stream.- Throws:
java.io.IOException
-
-