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
TCPServertreat multipleDataOutputStreams as one stream. This class is a wrapper for anArrayListofDataOutputStreams. It is used by aTCPServerand passed to aTCPClientInstanceto 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.inheritDoctag 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 aServerOutputStreamwith the original output stream and no others.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(DataOutputStream dos)Adds aDataOutputStreamto the list of objects to use when writing.voidclose()Closes all of the output streams within the output stream list.voidflush()Flushes this output stream.voidremove(DataOutputStream dos)Removes aDataOutputStreamto the list of objects to use when writing.intsize()Returns the current value of thewrittencounter for each activeDataOutputStream.voidwrite(byte[] b)Writesb.lengthbytes to the output streams.voidwrite(byte[] b, int off, int len)Writeslenbytes from the specifiedbytearray starting at offsetoffto the output streams.voidwrite(int b)Writes the specifiedbyteto the output streams.voidwriteBoolean(boolean v)Writes abooleanvalue to the output streams.voidwriteByte(int v)Writes to the output streams the eight low-order bits of the argument v.voidwriteBytes(String s)Writes aStringto the output streams.voidwriteChar(int v)Writes acharvalue, which is comprised of two bytes, to the output streams.voidwriteChars(String s)Writes every character in the string s, to the output streams, in order, two bytes per character.voidwriteDouble(double v)Writes a double value, which is comprised of eight bytes, to the output streams.voidwriteFloat(float v)Writes afloatvalue, which is comprised of four bytes, to the output streams.voidwriteInt(int v)Writes anintvalue, which is comprised of four bytes, to the output streams.voidwriteLong(long v)Writes alongvalue, which is comprised of eight bytes, to the output streams.voidwriteShort(int v)Writes ashortvalue, which is comprised of two bytes, to the output streams.voidwriteUTF(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 aServerOutputStreamwith the original output stream and no others. Theoutparameter is not used by any of the methods in this class, andsuperdoesn't get called at all, so there is no repercussions to adding and them removing it. However, ifoutisnull, then it will not be added. Also note that ifoutis 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 IOExceptionFlushes this output stream. This forces any buffered output bytes to be written out to the stream. Theflushmethod does not call the flush method of its underlying output stream, unless it is part of the list of output streams.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classFilterOutputStream- Throws:
IOException- if an I/O error occurs.- See Also:
DataOutputStream.flush()
-
write
public void write(byte[] b) throws IOExceptionWritesb.lengthbytes to the output streams. Thewritemethod ofServerOutputStreamcalls itswritemethod of three arguments with the argumentsb,0, andb.length. Note that this method only calls thewritemethod of its underlying stream with the single argumentbif it is within the output stream list.- Specified by:
writein interfaceDataOutput- Overrides:
writein 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 IOExceptionWriteslenbytes from the specifiedbytearray starting at offsetoffto the output streams. This calls thewritemethod for eachDataOutputStreamwithin the output stream list. Note that this method does not call thewritemethod of the underlying output stream unless it is within the output stream list.- Specified by:
writein interfaceDataOutput- Overrides:
writein 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 IOExceptionWrites the specifiedbyteto the output streams. Thewritemethod ofServerOutputStreamdoes not call the underlying output streamswritemethod, unless it is part of the output stream list.- Specified by:
writein interfaceDataOutput- Overrides:
writein 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 IOExceptionWrites abooleanvalue 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 thereadBooleanmethod of theDataInputinterface, which will then return abooleanvalue equal to v.- Specified by:
writeBooleanin 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 IOExceptionWrites 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 thereadBytemethod of theDataInputinterface, which will then return a byte equal to (byte)v.- Specified by:
writeBytein 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 aStringto the output streams. For every character in the string s, taken in order, one byte is written to the output stream. If s isnull, aNullPointerExceptionis 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 thewriteBytemethod. The high-order eight bits of each character in the string are ignored.- Specified by:
writeBytesin 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 IOExceptionWrites acharvalue, 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 atthislink. The bytes written by this method may be read by thereadCharmethod of theDataInputinterface, which will then return acharequal to (char)v.- Specified by:
writeCharin 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, aNullPointerExceptionis thrown. Ifs.lengthis 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 thewriteBytemethod. The high-order eight bits of each character in the string are ignored.- Specified by:
writeCharsin 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 IOExceptionWrites a double value, which is comprised of eight bytes, to the output streams. It does this as if it first converts thisdoublevalue to a long in exactly the manner of thedoubleToLongBitsmethod and then writes the long value in exactly the manner of thewriteLongmethod. The bytes written by this method may be read by thereadDoublemethod of theDataInputinterface, which will then return adoubleequal to v.- Specified by:
writeDoublein 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 IOExceptionWrites afloatvalue, which is comprised of four bytes, to the output streams. It does this as if it first converts thisfloatvalue to anintin exactly the manner of thefloatToIntBitsmethod. The bytes written by this method may be read by thereadFloatmethod of theDataInputinterface, which will then return afloatequal to v.- Specified by:
writeFloatin 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 IOExceptionWrites anintvalue, 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 atthislink. The bytes written by this method may be read by thereadIntmethod of theDataInputinterface, which will then return anintequal to v.- Specified by:
writeIntin 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 IOExceptionWrites alongvalue, 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 atthislink. The bytes written by this method may be read by thereadLongmethod of theDataInputinterface, which will then return alongequal to v.- Specified by:
writeLongin 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 IOExceptionWrites ashortvalue, 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 atthislink. The bytes written by this method may be read by thereadShortmethod of theDataInputinterface, which will then return ashortequal to (short)v.- Specified by:
writeShortin 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, aNullPointerExceptionis 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. Seethisdocumentation for more details.- Specified by:
writeUTFin interfaceDataOutput- Parameters:
line- The string value to be written.- Throws:
IOException- If an I/O error occurs.
-
close
public void close() throws IOExceptionCloses 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 whichDataOutputStreams are closed and which ones are open. If at all possible, avoid calling this method.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterOutputStream- Throws:
IOException- If an I/O error occurs.
-
add
public void add(DataOutputStream dos)
Adds aDataOutputStreamto the list of objects to use when writing. This is a wrapper method forArrayListsaddmethod. However,nullcannot 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 aDataOutputStreamto the list of objects to use when writing. This is a wrapper method forArrayListsremovemethod. However, because anull DataOutputStreamcannot be added, when this method is called withnullas 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 thewrittencounter for each activeDataOutputStream. This is in effect the same as adding thewrittencounter for each activeDataOutputStream, unless the counter overflows. If this happens, then the maximum size is not added at all. Thus, if eachDataOutputStream'ssizemethod returns themaxvalue, this method will return 0.- Returns:
- The cumulative byte's written.
-
-