Class ServerOutputStream
- java.lang.Object
-
- java.io.OutputStream
-
- java.io.FilterOutputStream
-
- com.coswald.jtalker.net.ServerOutputStream
-
- All Implemented Interfaces:
Closeable
,DataOutput
,Flushable
,AutoCloseable
public final class ServerOutputStream extends FilterOutputStream implements DataOutput
A data output stream that lets a
TCPServer
treat multipleDataOutputStream
s as one stream. This class is a wrapper for anArrayList
ofDataOutputStream
s. It is used by aTCPServer
and passed to aTCPClientInstance
to make sure the server can talk to multiple clients at the same time.Note that no method in this class is declared as
synchronized
. This is due to the fact that there should be no reason for it to be so. Even though we are having multi-threaded access on this object, only output is handled, and there is no way of circling back input. That being said, if in the future that is intended functionality, you may consider having an object lock on the methods that you wish to use. We did not want to sacrifice performance when we didn't need to.In this class you will see a lot of "inherited" documentation.
This is because I did not want to rewrite the wheel when documenting; however, this inherited documentation comes fromI now put the "inherited" in air quotes, due to the fact that I ended up having to re-invent the wheel on my journey to getting the Javadoc 11 tool to work for me. I could get theFilerOutputStream
. Thus, any documentation should actually come fromDataOutputStream
. If there are any misconstrued documentation, please refer toDataOutputStream
.inheritDoc
tag to work, but now all documentation is explicit instead of lazy. Enjoy the copy and paste.- Since:
- JTalker 0.0.1
- Version:
- 0.0.1
- Author:
- C. William Oswald
- See Also:
DataOutputStream
-
-
Field Summary
-
Fields inherited from class java.io.FilterOutputStream
out
-
-
Constructor Summary
Constructors Constructor Description ServerOutputStream(OutputStream out)
Creates aServerOutputStream
with the original output stream and no others.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(DataOutputStream dos)
Adds aDataOutputStream
to the list of objects to use when writing.void
close()
Closes all of the output streams within the output stream list.void
flush()
Flushes this output stream.void
remove(DataOutputStream dos)
Removes aDataOutputStream
to the list of objects to use when writing.int
size()
Returns the current value of thewritten
counter for each activeDataOutputStream
.void
write(byte[] b)
Writesb.length
bytes to the output streams.void
write(byte[] b, int off, int len)
Writeslen
bytes from the specifiedbyte
array starting at offsetoff
to the output streams.void
write(int b)
Writes the specifiedbyte
to the output streams.void
writeBoolean(boolean v)
Writes aboolean
value to the output streams.void
writeByte(int v)
Writes to the output streams the eight low-order bits of the argument v.void
writeBytes(String s)
Writes aString
to the output streams.void
writeChar(int v)
Writes achar
value, which is comprised of two bytes, to the output streams.void
writeChars(String s)
Writes every character in the string s, to the output streams, in order, two bytes per character.void
writeDouble(double v)
Writes a double value, which is comprised of eight bytes, to the output streams.void
writeFloat(float v)
Writes afloat
value, which is comprised of four bytes, to the output streams.void
writeInt(int v)
Writes anint
value, which is comprised of four bytes, to the output streams.void
writeLong(long v)
Writes along
value, which is comprised of eight bytes, to the output streams.void
writeShort(int v)
Writes ashort
value, which is comprised of two bytes, to the output streams.void
writeUTF(String line)
Writes two bytes of length information to the output streams, followed by the modified UTF-8 representation of every character in the string s.-
Methods inherited from class java.io.OutputStream
nullOutputStream
-
-
-
-
Constructor Detail
-
ServerOutputStream
public ServerOutputStream(OutputStream out)
Creates aServerOutputStream
with the original output stream and no others. Theout
parameter is not used by any of the methods in this class, andsuper
doesn't get called at all, so there is no repercussions to adding and them removing it. However, ifout
isnull
, then it will not be added. Also note that ifout
is not aDataOutputStream
, it will be wrapped as one (and therefore can't be removed).- Parameters:
out
- The original output stream.
-
-
Method Detail
-
flush
public void flush() throws IOException
Flushes this output stream. This forces any buffered output bytes to be written out to the stream. Theflush
method does not call the flush method of its underlying output stream, unless it is part of the list of output streams.- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classFilterOutputStream
- Throws:
IOException
- if an I/O error occurs.- See Also:
DataOutputStream.flush()
-
write
public void write(byte[] b) throws IOException
Writesb.length
bytes to the output streams. Thewrite
method ofServerOutputStream
calls itswrite
method of three arguments with the argumentsb
,0
, andb.length
. Note that this method only calls thewrite
method of its underlying stream with the single argumentb
if it is within the output stream list.- Specified by:
write
in interfaceDataOutput
- Overrides:
write
in classFilterOutputStream
- Parameters:
b
- The data to be written.- Throws:
IOException
- If an I/O error occurs.- See Also:
FilterOutputStream.write(byte[])
-
write
public void write(byte[] b, int off, int len) throws IOException
Writeslen
bytes from the specifiedbyte
array starting at offsetoff
to the output streams. This calls thewrite
method for eachDataOutputStream
within the output stream list. Note that this method does not call thewrite
method of the underlying output stream unless it is within the output stream list.- Specified by:
write
in interfaceDataOutput
- Overrides:
write
in classFilterOutputStream
- Parameters:
b
- The data.off
- The start offset in the data.len
- The number of bytes to write.- Throws:
IOException
- If an I/O error occurs.- See Also:
DataOutputStream.write(byte[], int, int)
-
write
public void write(int b) throws IOException
Writes the specifiedbyte
to the output streams. Thewrite
method ofServerOutputStream
does not call the underlying output streamswrite
method, unless it is part of the output stream list.- Specified by:
write
in interfaceDataOutput
- Overrides:
write
in classFilterOutputStream
- Parameters:
b
- The byte to write.- Throws:
IOException
- If an I/O error occurs.- See Also:
DataOutputStream.write(int)
-
writeBoolean
public void writeBoolean(boolean v) throws IOException
Writes aboolean
value to the output streams. If the argument v istrue
, the value (byte
) 1 is written; if v isfalse
, the value (byte
) 0 is written. The byte written by this method may be read by thereadBoolean
method of theDataInput
interface, which will then return aboolean
value equal to v.- Specified by:
writeBoolean
in interfaceDataOutput
- Parameters:
v
- The boolean to be written.- Throws:
IOException
- If an I/O error occurs.- See Also:
DataOutputStream.writeBoolean(boolean)
-
writeByte
public void writeByte(int v) throws IOException
Writes to the output streams the eight low-order bits of the argument v. The 24 high-order bits of v are ignored. The byte written by this method may be read by thereadByte
method of theDataInput
interface, which will then return a byte equal to (byte
)v.- Specified by:
writeByte
in interfaceDataOutput
- Parameters:
v
- The byte value to be written- Throws:
IOException
- If an I/O error occurs.- See Also:
DataOutputStream.writeByte(int)
-
writeBytes
public void writeBytes(String s) throws IOException
Writes aString
to the output streams. For every character in the string s, taken in order, one byte is written to the output stream. If s isnull
, aNullPointerException
is thrown. If s.length is zero, then no bytes are written. Otherwise, the character s[0] is written first, then s[1], and so on; the last character written is s[s.length - 1]. For each character, one byte is written, the low-order byte, in exactly the manner of thewriteByte
method. The high-order eight bits of each character in the string are ignored.- Specified by:
writeBytes
in interfaceDataOutput
- Parameters:
s
- The String of bytes to be written.- Throws:
IOException
- If an I/O error occurs.- See Also:
DataOutputStream.writeBytes(String)
-
writeChar
public void writeChar(int v) throws IOException
Writes achar
value, which is comprised of two bytes, to the output streams. The byte values to be written are shown in the order they are written in atthis
link. The bytes written by this method may be read by thereadChar
method of theDataInput
interface, which will then return achar
equal to (char
)v.- Specified by:
writeChar
in interfaceDataOutput
- Parameters:
v
- The char value to be written.- Throws:
IOException
- If an I/O error occurs.- See Also:
DataOutputStream.writeChar(int)
-
writeChars
public void writeChars(String s) throws IOException
Writes every character in the string s, to the output streams, in order, two bytes per character. If s inull
, aNullPointerException
is thrown. Ifs.length
is zero, then no bytes are written. Otherwise, the character s[0] is written first, then s[1], and so on; the last character written is s[s.length - 1]. For each character, one byte is written, the low-order byte, in exactly the manner of thewriteByte
method. The high-order eight bits of each character in the string are ignored.- Specified by:
writeChars
in interfaceDataOutput
- Parameters:
s
- The string of bytes to be written- Throws:
IOException
- If an I/O error occurs.- See Also:
DataOutputStream.writeChars(String)
-
writeDouble
public void writeDouble(double v) throws IOException
Writes a double value, which is comprised of eight bytes, to the output streams. It does this as if it first converts thisdouble
value to a long in exactly the manner of thedoubleToLongBits
method and then writes the long value in exactly the manner of thewriteLong
method. The bytes written by this method may be read by thereadDouble
method of theDataInput
interface, which will then return adouble
equal to v.- Specified by:
writeDouble
in interfaceDataOutput
- Parameters:
v
- The double value to be written- Throws:
IOException
- If an I/O error occurs.- See Also:
DataOutputStream.writeDouble(double)
-
writeFloat
public void writeFloat(float v) throws IOException
Writes afloat
value, which is comprised of four bytes, to the output streams. It does this as if it first converts thisfloat
value to anint
in exactly the manner of thefloatToIntBits
method. The bytes written by this method may be read by thereadFloat
method of theDataInput
interface, which will then return afloat
equal to v.- Specified by:
writeFloat
in interfaceDataOutput
- Parameters:
v
- The float value to be written.- Throws:
IOException
- If an I/O error occurs.- See Also:
DataOutputStream.writeFloat(float)
-
writeInt
public void writeInt(int v) throws IOException
Writes anint
value, which is comprised of four bytes, to the output streams. The byte values to be written are shown in the order they are written in atthis
link. The bytes written by this method may be read by thereadInt
method of theDataInput
interface, which will then return anint
equal to v.- Specified by:
writeInt
in interfaceDataOutput
- Parameters:
v
- The int value to be written.- Throws:
IOException
- If an I/O error occurs.- See Also:
DataOutputStream.writeInt(int)
-
writeLong
public void writeLong(long v) throws IOException
Writes along
value, which is comprised of eight bytes, to the output streams. The byte values to be written are shown in the order they are written in atthis
link. The bytes written by this method may be read by thereadLong
method of theDataInput
interface, which will then return along
equal to v.- Specified by:
writeLong
in interfaceDataOutput
- Parameters:
v
- The long value to be written.- Throws:
IOException
- If an I/O error occurs.- See Also:
DataOutputStream.writeLong(long)
-
writeShort
public void writeShort(int v) throws IOException
Writes ashort
value, which is comprised of two bytes, to the output streams. The byte values to be written are shown in the order they are written in atthis
link. The bytes written by this method may be read by thereadShort
method of theDataInput
interface, which will then return ashort
equal to (short
)v.- Specified by:
writeShort
in interfaceDataOutput
- Parameters:
v
- The short value to be written.- Throws:
IOException
- If an I/O error occurs.- See Also:
DataOutputStream.writeShort(int)
-
writeUTF
public void writeUTF(String line) throws IOException
Writes two bytes of length information to the output streams, followed by the modified UTF-8 representation of every character in the string s. If s isnull
, aNullPointerException
is thrown. Each character in the string s is converted to a group of one, two, or three bytes, depending on the value of the character. Seethis
documentation for more details.- Specified by:
writeUTF
in interfaceDataOutput
- Parameters:
line
- The string value to be written.- Throws:
IOException
- If an I/O error occurs.
-
close
public void close() throws IOException
Closes all of the output streams within the output stream list. It is preferred to not call this method unless the server is shutting down, as this output stream should always be available to be running. If this method is called, and we add anotherDataOutputStream
, there is no code that blocks us from allowing us to do this, and no way of knowing whichDataOutputStream
s are closed and which ones are open. If at all possible, avoid calling this method.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classFilterOutputStream
- Throws:
IOException
- If an I/O error occurs.
-
add
public void add(DataOutputStream dos)
Adds aDataOutputStream
to the list of objects to use when writing. This is a wrapper method forArrayList
sadd
method. However,null
cannot be added to the list, so if this method is called withnull
, nothing will happen.- Parameters:
dos
- The output stream to add to the list of outputs.- See Also:
ArrayList.add(Object)
,remove(DataOutputStream)
-
remove
public void remove(DataOutputStream dos)
Removes aDataOutputStream
to the list of objects to use when writing. This is a wrapper method forArrayList
sremove
method. However, because anull DataOutputStream
cannot be added, when this method is called withnull
as a parameter, nothing happens.- Parameters:
dos
- The output stream to remove from the list of outputs.- See Also:
ArrayList.remove(Object)
,add(DataOutputStream)
-
size
public int size()
Returns the current value of thewritten
counter for each activeDataOutputStream
. This is in effect the same as adding thewritten
counter for each activeDataOutputStream
, unless the counter overflows. If this happens, then the maximum size is not added at all. Thus, if eachDataOutputStream
'ssize
method returns themax
value, this method will return 0.- Returns:
- The cumulative byte's written.
-
-