Package com.martiansoftware.nailgun
Class NGOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- java.io.DataOutputStream
-
- com.martiansoftware.nailgun.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
-
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)
-
-
-
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 wrapstreamCode
- 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 interfacejava.io.DataOutput
- Overrides:
write
in classjava.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 interfacejava.io.DataOutput
- Overrides:
write
in classjava.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 interfacejava.io.DataOutput
- Overrides:
write
in classjava.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 interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.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 interfacejava.io.Flushable
- Overrides:
flush
in classjava.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
-
-