Class 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 multiple DataOutputStreams as one stream. This class is a wrapper for an ArrayList of DataOutputStreams. It is used by a TCPServer and passed to a TCPClientInstance 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 from FilerOutputStream. Thus, any documentation should actually come from DataOutputStream. If there are any misconstrued documentation, please refer to DataOutputStream. I 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 the 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
    • Constructor Summary

      Constructors 
      Constructor Description
      ServerOutputStream​(OutputStream out)
      Creates a ServerOutputStream 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 a DataOutputStream 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 a DataOutputStream to the list of objects to use when writing.
      int size()
      Returns the current value of the written counter for each active DataOutputStream.
      void write​(byte[] b)
      Writes b.length bytes to the output streams.
      void write​(byte[] b, int off, int len)
      Writes len bytes from the specified byte array starting at offset off to the output streams.
      void write​(int b)
      Writes the specified byte to the output streams.
      void writeBoolean​(boolean v)
      Writes a boolean 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 a String to the output streams.
      void writeChar​(int v)
      Writes a char 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 a float value, which is comprised of four bytes, to the output streams.
      void writeInt​(int v)
      Writes an int value, which is comprised of four bytes, to the output streams.
      void writeLong​(long v)
      Writes a long value, which is comprised of eight bytes, to the output streams.
      void writeShort​(int v)
      Writes a short 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.
    • Constructor Detail

      • ServerOutputStream

        public ServerOutputStream​(OutputStream out)
        Creates a ServerOutputStream with the original output stream and no others. The out parameter is not used by any of the methods in this class, and super doesn't get called at all, so there is no repercussions to adding and them removing it. However, if out is null, then it will not be added. Also note that if out is not a DataOutputStream, 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. The flush 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 interface Flushable
        Overrides:
        flush in class FilterOutputStream
        Throws:
        IOException - if an I/O error occurs.
        See Also:
        DataOutputStream.flush()
      • write

        public void write​(byte[] b)
                   throws IOException
        Writes b.length bytes to the output streams. The write method of ServerOutputStream calls its write method of three arguments with the arguments b, 0, and b.length. Note that this method only calls the write method of its underlying stream with the single argument b if it is within the output stream list.
        Specified by:
        write in interface DataOutput
        Overrides:
        write in class FilterOutputStream
        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
        Writes len bytes from the specified byte array starting at offset off to the output streams. This calls the write method for each DataOutputStream within the output stream list. Note that this method does not call the write method of the underlying output stream unless it is within the output stream list.
        Specified by:
        write in interface DataOutput
        Overrides:
        write in class FilterOutputStream
        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 specified byte to the output streams. The write method of ServerOutputStream does not call the underlying output streams write method, unless it is part of the output stream list.
        Specified by:
        write in interface DataOutput
        Overrides:
        write in class FilterOutputStream
        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 a boolean value to the output streams. If the argument v is true, the value (byte) 1 is written; if v is false, the value (byte) 0 is written. The byte written by this method may be read by the readBoolean method of the DataInput interface, which will then return a boolean value equal to v.
        Specified by:
        writeBoolean in interface DataOutput
        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 the readByte method of the DataInput interface, which will then return a byte equal to (byte)v.
        Specified by:
        writeByte in interface DataOutput
        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 a String to the output streams. For every character in the string s, taken in order, one byte is written to the output stream. If s is null, a NullPointerException 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 the writeByte method. The high-order eight bits of each character in the string are ignored.
        Specified by:
        writeBytes in interface DataOutput
        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 a char 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 at this link. The bytes written by this method may be read by the readChar method of the DataInput interface, which will then return a char equal to (char)v.
        Specified by:
        writeChar in interface DataOutput
        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 i null, a NullPointerException 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 the writeByte method. The high-order eight bits of each character in the string are ignored.
        Specified by:
        writeChars in interface DataOutput
        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 this double value to a long in exactly the manner of the doubleToLongBits method and then writes the long value in exactly the manner of the writeLong method. The bytes written by this method may be read by the readDouble method of the DataInput interface, which will then return a double equal to v.
        Specified by:
        writeDouble in interface DataOutput
        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 a float value, which is comprised of four bytes, to the output streams. It does this as if it first converts this float value to an int in exactly the manner of the floatToIntBits method. The bytes written by this method may be read by the readFloat method of the DataInput interface, which will then return a float equal to v.
        Specified by:
        writeFloat in interface DataOutput
        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 an int 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 at this link. The bytes written by this method may be read by the readInt method of the DataInput interface, which will then return an int equal to v.
        Specified by:
        writeInt in interface DataOutput
        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 a long 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 at this link. The bytes written by this method may be read by the readLong method of the DataInput interface, which will then return a long equal to v.
        Specified by:
        writeLong in interface DataOutput
        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 a short 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 at this link. The bytes written by this method may be read by the readShort method of the DataInput interface, which will then return a short equal to (short)v.
        Specified by:
        writeShort in interface DataOutput
        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 is null, a NullPointerException 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. See this documentation for more details.
        Specified by:
        writeUTF in interface DataOutput
        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 another DataOutputStream, there is no code that blocks us from allowing us to do this, and no way of knowing which DataOutputStreams are closed and which ones are open. If at all possible, avoid calling this method.
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
        Overrides:
        close in class FilterOutputStream
        Throws:
        IOException - If an I/O error occurs.
      • add

        public void add​(DataOutputStream dos)
        Adds a DataOutputStream to the list of objects to use when writing. This is a wrapper method for ArrayLists add method. However, null cannot be added to the list, so if this method is called with null, 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 a DataOutputStream to the list of objects to use when writing. This is a wrapper method for ArrayLists remove method. However, because a null DataOutputStream cannot be added, when this method is called with null 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 the written counter for each active DataOutputStream. This is in effect the same as adding the written counter for each active DataOutputStream, unless the counter overflows. If this happens, then the maximum size is not added at all. Thus, if each DataOutputStream's size method returns the max value, this method will return 0.
        Returns:
        The cumulative byte's written.