Class NGOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.DataOutput, java.io.Flushable, java.lang.AutoCloseable

    class NGOutputStream
    extends java.io.DataOutputStream
    Wraps an OutputStream to send writes in NailGun chunks. Because multiple NGOutputStreams wrap the same OutputStream (that is, the OutputStream obtained from the Socket connection with the client), writes are synchronized on the underlying OutputStream. If this were not the case, write interleaving could completely break the NailGun protocol.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean closed  
      private java.lang.Object lock  
      private byte streamCode  
      • Fields inherited from class java.io.DataOutputStream

        written
      • Fields inherited from class java.io.FilterOutputStream

        out
    • Constructor Summary

      Constructors 
      Constructor Description
      NGOutputStream​(java.io.OutputStream out, byte streamCode)
      Creates a new NGOutputStream wrapping the specified OutputStream and using the specified Nailgun chunk code.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void flush()  
      private void throwIfClosed()
      Check if stream is closed and throw an IOException if yes.
      void write​(byte[] b)  
      void write​(byte[] b, int offset, int len)  
      void write​(int b)  
      • Methods inherited from class java.io.DataOutputStream

        size, writeBoolean, writeByte, writeBytes, writeChar, writeChars, writeDouble, writeFloat, writeInt, writeLong, writeShort, writeUTF
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • lock

        private final java.lang.Object lock
      • streamCode

        private byte streamCode
      • closed

        private boolean closed
    • Constructor Detail

      • NGOutputStream

        public NGOutputStream​(java.io.OutputStream out,
                              byte streamCode)
        Creates a new NGOutputStream wrapping the specified OutputStream and using the specified Nailgun chunk code.
        Parameters:
        out - the OutputStream to wrap
        streamCode - the NailGun chunk code associated with this stream (i.e., '1' for stdout, '2' for stderr).
    • Method Detail

      • write

        public void write​(byte[] b)
                   throws java.io.IOException
        Specified by:
        write in interface java.io.DataOutput
        Overrides:
        write in class java.io.FilterOutputStream
        Throws:
        java.io.IOException
        See Also:
        OutputStream.write(byte[])
      • write

        public void write​(int b)
                   throws java.io.IOException
        Specified by:
        write in interface java.io.DataOutput
        Overrides:
        write in class java.io.DataOutputStream
        Throws:
        java.io.IOException
        See Also:
        OutputStream.write(int)
      • write

        public void write​(byte[] b,
                          int offset,
                          int len)
                   throws java.io.IOException
        Specified by:
        write in interface java.io.DataOutput
        Overrides:
        write in class java.io.DataOutputStream
        Throws:
        java.io.IOException
        See Also:
        OutputStream.write(byte[],int,int)
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.FilterOutputStream
        Throws:
        java.io.IOException
        See Also:
        Implement an empty close function, to allow the client to close the stdout and/or stderr, without this closing the connection socket to the client.
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.DataOutputStream
        Throws:
        java.io.IOException
        See Also:
        OutputStream.flush()
      • throwIfClosed

        private void throwIfClosed()
                            throws java.io.IOException
        Check if stream is closed and throw an IOException if yes. In the case of a public operation is being performed while the stream is already closed throws an IOException.
        Throws:
        java.io.IOException