Class 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
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private LogReader reader  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void attach​(java.io.OutputStream stdout, java.io.OutputStream stderr)
      Attaches two OutputStreams to the LogStream.
      void attach​(java.io.OutputStream stdout, java.io.OutputStream stderr, boolean closeAtEof)
      Attaches two OutputStreams to the LogStream.
      void close()
      Redefine to not throw checked exceptions.
      protected LogMessage computeNext()  
      (package private) static DefaultLogStream create​(java.io.InputStream stream)  
      java.lang.String readFully()  
      private static void writeAndFlush​(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
      • 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
    • Constructor Detail

      • DefaultLogStream

        private DefaultLogStream​(java.io.InputStream stream)
      • DefaultLogStream

        DefaultLogStream​(LogReader reader)
    • Method Detail

      • computeNext

        protected LogMessage computeNext()
        Specified by:
        computeNext in class com.google.common.collect.AbstractIterator<LogMessage>
      • close

        public void close()
        Description copied from interface: LogStream
        Redefine to not throw checked exceptions.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in interface LogStream
      • readFully

        public java.lang.String readFully()
        Specified by:
        readFully in interface LogStream
      • attach

        public void attach​(java.io.OutputStream stdout,
                           java.io.OutputStream stderr)
                    throws java.io.IOException
        Description copied from interface: LogStream
        Attaches two OutputStreams to the LogStream. Closes the streams after use.
        Specified by:
        attach in interface LogStream
        Parameters:
        stdout - OutputStream for the standard out
        stderr - 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.IOException
        Description copied from interface: LogStream
        Attaches two OutputStreams to the LogStream.

        Example usage:

         
         dockerClient
             .attachContainer(containerId,
                 AttachParameter.LOGS, AttachParameter.STDOUT,
                 AttachParameter.STDERR, AttachParameter.STREAM)
             .attach(System.out, System.err);
         
         

        Typically you use PipedOutputStream connected to a PipedInputStream which are read by - for example - an InputStreamReader or a Scanner. For small inputs, the PipedOutputStream just writes to the buffer of the PipedInputStream, 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&lt;Void&gt;() {
             &#064;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 interface LogStream
        Parameters:
        stdout - OutputStream for the standard out
        stderr - OutputStream for the standard err
        closeAtEof - 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.IOException
        Write the contents of the given ByteBuffer to the OutputStream and flush the stream.
        Throws:
        java.io.IOException