Package com.spotify.docker.client
Class DefaultLogStream
java.lang.Object
com.google.common.collect.UnmodifiableIterator<LogMessage>
com.google.common.collect.AbstractIterator<LogMessage>
com.spotify.docker.client.DefaultLogStream
- All Implemented Interfaces:
LogStream
,Closeable
,AutoCloseable
,Iterator<LogMessage>
class DefaultLogStream
extends com.google.common.collect.AbstractIterator<LogMessage>
implements LogStream
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescription(package private)
DefaultLogStream
(LogReader reader) private
DefaultLogStream
(InputStream stream) -
Method Summary
Modifier and TypeMethodDescriptionvoid
attach
(OutputStream stdout, OutputStream stderr) Attaches twoOutputStream
s to theLogStream
.void
attach
(OutputStream stdout, OutputStream stderr, boolean closeAtEof) Attaches twoOutputStream
s to theLogStream
.void
close()
Redefine to not throw checked exceptions.protected LogMessage
(package private) static DefaultLogStream
create
(InputStream stream) private static void
writeAndFlush
(ByteBuffer buffer, 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
Methods inherited from class com.google.common.collect.UnmodifiableIterator
remove
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Iterator
forEachRemaining, hasNext, next, remove
-
Field Details
-
reader
-
-
Constructor Details
-
DefaultLogStream
-
DefaultLogStream
DefaultLogStream(LogReader reader)
-
-
Method Details
-
create
-
computeNext
- Specified by:
computeNext
in classcom.google.common.collect.AbstractIterator<LogMessage>
-
close
public void close()Description copied from interface:LogStream
Redefine to not throw checked exceptions. -
readFully
-
attach
Description copied from interface:LogStream
Attaches twoOutputStream
s to theLogStream
. Closes the streams after use.- Specified by:
attach
in interfaceLogStream
- Parameters:
stdout
- OutputStream for the standard outstderr
- OutputStream for the standard err- Throws:
IOException
- if an I/O error occurs- See Also:
-
attach
Description copied from interface:LogStream
Attaches twoOutputStream
s to theLogStream
.Example usage:
dockerClient .attachContainer(containerId, AttachParameter.LOGS, AttachParameter.STDOUT, AttachParameter.STDERR, AttachParameter.STREAM) .attach(System.out, System.err);
Typically you use
PipedOutputStream
connected to aPipedInputStream
which are read by - for example - anInputStreamReader
or aScanner
. For small inputs, thePipedOutputStream
just 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:
attach
in interfaceLogStream
- Parameters:
stdout
- OutputStream for the standard outstderr
- OutputStream for the standard errcloseAtEof
- whether to close the streams when this log stream ends- Throws:
IOException
- if an I/O error occurs- See Also:
-
writeAndFlush
Write the contents of the given ByteBuffer to the OutputStream and flush the stream.- Throws:
IOException
-