001    package com.skype.ipc;
002    
003    import java.io.BufferedOutputStream;
004    import java.io.FileOutputStream;
005    import java.io.IOException;
006    import java.io.OutputStream;
007    
008    public class LoggedBufferedOutputStream extends BufferedOutputStream implements OutputTransporting {
009    
010        public LoggedBufferedOutputStream(OutputStream out, ClientConfiguration cfg) {
011            super(out);
012            if (cfg.generateTransportLog()) {
013                try {
014                    mTransportLog = new FileOutputStream(cfg.getOutputTransportLogName());
015                } catch (IOException e) {
016                    mTransportLog = null;
017                }
018            }
019        }
020    
021        public void writeBytes(byte [] src) throws IOException {
022            super.write(src, 0, src.length);
023            if (mTransportLog != null) {
024                try {
025                    mTransportLog.write(src, 0, src.length);
026                } catch (IOException e) {
027                    mTransportLog = null;
028                }
029            }
030        }
031    
032        public void writeBytesAndFlush(byte [] src) throws IOException {
033            super.write(src, 0, src.length);
034            super.flush();
035            if (mTransportLog != null) {
036                try {
037                    mTransportLog.write(src, 0, src.length);
038                } catch (IOException e) {
039                    mTransportLog = null;
040                }
041            }
042        }
043    
044        public void writeByte(int value) throws IOException {
045            super.write(value);
046            if (mTransportLog != null) {
047                try {
048                    mTransportLog.write(value);
049                } catch (IOException e) {
050                    mTransportLog = null;
051                }
052            }
053        }
054    
055        public void writeByteAndFlush(int value) throws IOException {
056            super.write(value);
057            super.flush();
058            if (mTransportLog != null) {
059                try {
060                    mTransportLog.write(value);
061                } catch (IOException e) {
062                    mTransportLog = null;
063                }
064            }
065        }
066    
067        public void close() throws IOException {
068            try {
069                if (mTransportLog != null) 
070                     mTransportLog.close();
071            } catch (IOException e) {
072            }
073            super.close();
074        }
075    
076        private FileOutputStream mTransportLog;
077    }
078