'Called when a stream is finished. It moves the stream from
openStreams to closedStreams, prints output, and dumps the file'

def __closeStream(self, key, message=''):

    theStream = self.openStreams[key]
    if (not theStream['filename']):
        message = 'INCOMPLETE    --    missing    filename'
    rebuiltFile = ''
    for i in sorted(theStream['filedata'].keys()):
        rebuiltFile += theStream['filedata'][i]
    if (theStream['readwrite'] == 'read'):
        ipsNports = (key[2], key[3], key[0], key[1])
    else:
        ipsNports = key
    msg = ('%s    %s    (%s    bytes)    %s' % (theStream['readwrite'], theStream['filename'], len(rebuiltFile), message))
    self.alert(msg, ts=theStream['timestamp'], sip=ipsNports[0], sport=ipsNports[1], dip=ipsNports[2], dport=ipsNports[3])
    if (self.rip and (len(rebuiltFile) > 0)):
        outpath = self.__localfilename(self.outdir, theStream['filename'])
        outfile = open(outpath, 'wb')
        outfile.write(rebuiltFile)
        outfile.close()
    self.closedStreams.append((key, self.openStreams[key]['closed_connection']))
    del self.openStreams[key]
