FileDocCategorySizeDatePackage
SDPOutputStream.javaAPI DocphoneME MR2 API (J2ME)7687Wed May 02 18:00:42 BST 2007gov.nist.microedition.sip

SDPOutputStream

public class SDPOutputStream extends OutputStream
SDP output stream. This code is in the public domain.

Fields Summary
private javax.microedition.sip.SipConnection
connection
The current connection.
private final ByteArrayOutputStream
outputStream
the ByteArrayOutputStream object wrapped by this class. We cannot just inherit from the ByteArrayOutputStream class because ByteArrayOutputStream.write() does not throw an IOException. (Is it important? Yes. When the parent class does not throw an exception, the child class must not throw the exception, because the child class may be used everywhere in place of the parent class. If the child classes could throw exceptions not declared for parents, we would have undeclared exceptions thrown. And Java does not permit this. So we have to use a wrapper class.)
private boolean
isOpen
is the stream open?
Constructors Summary
public SDPOutputStream(javax.microedition.sip.SipConnection connection)
Constructs a stream for the requested connection.

param
connection the associated connection

        this.connection = connection;
        setOpen(true);
    
Methods Summary
private voidcheckOpen()
check if the stream is open

throws
IOException if the stream is closed

        if (!isOpen) {
            throw new IOException("the output stream has been closed");
        }
    
public voidclose()
Close the SDPOutputStream and send the message held by the sip connection

        if (!isOpen) {
            throw new IOException("stream already closed");
        }
        setOpen(false);
        outputStream.close();

        if (connection instanceof SipClientConnectionImpl) {
            SipClientConnectionImpl sipClientConnection =
                    (SipClientConnectionImpl)connection;
            // If the client connection is in a STREAM_OPEN state and
            // the request is an ACK
            // The connection goes into the COMPLETED state
            if (sipClientConnection.state ==
                    SipClientConnectionImpl.STREAM_OPEN) {
                if (sipClientConnection.getMethod().equals(Request.ACK)) {
                    sipClientConnection.state =
                            SipClientConnectionImpl.COMPLETED;
                    try {
                        super.close();
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                    return;
                }
            }
        }
        try {
            connection.send();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        try {
            super.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    
public voidflush()
Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

The flush method of OutputStream does nothing.

exception
IOException if the stream is closed.

        outputStream.flush();
    
protected java.io.ByteArrayOutputStreamgetByteArrayOutputStream()
Return the ByteArrayOutputStream object wrapped by this class. The problem is that ByteArrayOutputStream methods do not throw IOException, while SDPOutputStream methods should.

return
the ByteArrayOutputStream object

        return outputStream;
    
public booleanisOpen()
Return the status of the output stream (open or closed). When the stream is closed, write() throws an IOException. The open or closed status does not affect the internal ByteArrayOutputStream object.

return
true if the stream is open

        return isOpen;
    
protected voidsetOpen(boolean newOpenState)
The send() functions use this function to toggle the stream state. (send() cannot call close() because it's done vice versa: close() calls send())

param
newOpenState the new state

        isOpen = newOpenState;
    
public java.lang.StringtoString()
Convert to a string containing debugging information.

return
string containing: class name + hash value + isOpen state


                           
       
        return super.toString()+" isOpen="+isOpen;
    
public voidwrite(int b)
Writes the specified byte to the wrapped byte array output stream.

param
b the byte to be written.
exception
IOException if the stream is closed.

        checkOpen();
        outputStream.write(b);
    
public voidwrite(byte[] b)
Writes b.length bytes from the specified byte array to the wrapped output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).

param
b the data.
exception
IOException if an I/O error occurs.
see
java.io.OutputStream#write(byte[], int, int)
exception
IOException if the stream is closed.

        checkOpen();
        outputStream.write(b);
    
public voidwrite(byte[] b, int off, int len)
Writes len bytes from the specified byte array starting at offset off to the wrapped byte array output stream.

param
b the data.
param
off the start offset in the data.
param
len the number of bytes to write.
exception
IOException if the stream is closed.

        checkOpen();
        outputStream.write(b, off, len);