Back to index

lightning-sunbird  0.9+nobinonly
nsInputStreamTee.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Darin Fisher <darin@netscape.com> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsIInputStreamTee.h"
00040 #include "nsIInputStream.h"
00041 #include "nsIOutputStream.h"
00042 #include "nsCOMPtr.h"
00043 
00044 class nsInputStreamTee : public nsIInputStreamTee
00045 {
00046 public:
00047     NS_DECL_ISUPPORTS
00048     NS_DECL_NSIINPUTSTREAM
00049     NS_DECL_NSIINPUTSTREAMTEE
00050 
00051     nsInputStreamTee();
00052 
00053 private:
00054     ~nsInputStreamTee() {}
00055 
00056     nsresult TeeSegment(const char *buf, PRUint32 count);
00057 
00058     static NS_METHOD WriteSegmentFun(nsIInputStream *, void *, const char *,
00059                                      PRUint32, PRUint32, PRUint32 *);
00060 
00061 private:
00062     nsCOMPtr<nsIInputStream>  mSource;
00063     nsCOMPtr<nsIOutputStream> mSink;
00064     nsWriteSegmentFun         mWriter;  // for implementing ReadSegments
00065     void                     *mClosure; // for implementing ReadSegments
00066 };
00067 
00068 nsInputStreamTee::nsInputStreamTee()
00069 {
00070 }
00071 
00072 nsresult
00073 nsInputStreamTee::TeeSegment(const char *buf, PRUint32 count)
00074 {
00075     if (!mSink)
00076         return NS_OK; // nothing to do
00077     nsresult rv;
00078     PRUint32 bytesWritten = 0;
00079     while (count) {
00080         rv = mSink->Write(buf + bytesWritten, count, &bytesWritten);
00081         if (NS_FAILED(rv)) {
00082             // ok, this is not a fatal error... just drop our reference to mSink
00083             // and continue on as if nothing happened.
00084             NS_WARNING("Write failed (non-fatal)");
00085             // catch possible misuse of the input stream tee
00086             NS_ASSERTION(rv != NS_BASE_STREAM_WOULD_BLOCK, "sink must be a blocking stream");
00087             mSink = 0;
00088             break;
00089         }
00090         NS_ASSERTION(bytesWritten <= count, "wrote too much");
00091         count -= bytesWritten;
00092     }
00093     return NS_OK;
00094 }
00095 
00096 NS_METHOD
00097 nsInputStreamTee::WriteSegmentFun(nsIInputStream *in, void *closure, const char *fromSegment,
00098                                   PRUint32 offset, PRUint32 count, PRUint32 *writeCount)
00099 {
00100     nsInputStreamTee *tee = NS_REINTERPRET_CAST(nsInputStreamTee *, closure);
00101 
00102     nsresult rv = tee->mWriter(in, tee->mClosure, fromSegment, offset, count, writeCount);
00103     if (NS_FAILED(rv) || (*writeCount == 0)) {
00104         NS_ASSERTION((NS_FAILED(rv) ? (*writeCount == 0) : PR_TRUE),
00105                 "writer returned an error with non-zero writeCount");
00106         return rv;
00107     }
00108 
00109     return tee->TeeSegment(fromSegment, *writeCount);
00110 }
00111 
00112 NS_IMPL_ISUPPORTS2(nsInputStreamTee,
00113                    nsIInputStreamTee,
00114                    nsIInputStream)
00115 
00116 NS_IMETHODIMP
00117 nsInputStreamTee::Close()
00118 {
00119     NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED);
00120     nsresult rv = mSource->Close();
00121     mSource = 0;
00122     mSink = 0;
00123     return rv;
00124 }
00125 
00126 NS_IMETHODIMP
00127 nsInputStreamTee::Available(PRUint32 *avail)
00128 {
00129     NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED);
00130     return mSource->Available(avail);
00131 }
00132 
00133 NS_IMETHODIMP
00134 nsInputStreamTee::Read(char *buf, PRUint32 count, PRUint32 *bytesRead)
00135 {
00136     NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED);
00137 
00138     nsresult rv = mSource->Read(buf, count, bytesRead);
00139     if (NS_FAILED(rv) || (*bytesRead == 0))
00140         return rv;
00141 
00142     return TeeSegment(buf, *bytesRead);
00143 }
00144 
00145 NS_IMETHODIMP
00146 nsInputStreamTee::ReadSegments(nsWriteSegmentFun writer, 
00147                                void *closure,
00148                                PRUint32 count,
00149                                PRUint32 *bytesRead)
00150 {
00151     NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED);
00152 
00153     mWriter = writer;
00154     mClosure = closure;
00155 
00156     return mSource->ReadSegments(WriteSegmentFun, this, count, bytesRead);
00157 }
00158 
00159 NS_IMETHODIMP
00160 nsInputStreamTee::IsNonBlocking(PRBool *result)
00161 {
00162     NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED);
00163     return mSource->IsNonBlocking(result);
00164 }
00165 
00166 NS_IMETHODIMP
00167 nsInputStreamTee::SetSource(nsIInputStream *source)
00168 {
00169     mSource = source;
00170     return NS_OK;
00171 }
00172 
00173 NS_IMETHODIMP
00174 nsInputStreamTee::GetSource(nsIInputStream **source)
00175 {
00176     NS_IF_ADDREF(*source = mSource);
00177     return NS_OK;
00178 }
00179 
00180 NS_IMETHODIMP
00181 nsInputStreamTee::SetSink(nsIOutputStream *sink)
00182 {
00183 #ifdef DEBUG
00184     if (sink) {
00185         PRBool nonBlocking;
00186         nsresult rv = sink->IsNonBlocking(&nonBlocking);
00187         if (NS_FAILED(rv) || nonBlocking)
00188             NS_ERROR("sink should be a blocking stream");
00189     }
00190 #endif
00191     mSink = sink;
00192     return NS_OK;
00193 }
00194 
00195 NS_IMETHODIMP
00196 nsInputStreamTee::GetSink(nsIOutputStream **sink)
00197 {
00198     NS_IF_ADDREF(*sink = mSink);
00199     return NS_OK;
00200 }
00201 
00202 // factory method
00203 
00204 NS_COM nsresult 
00205 NS_NewInputStreamTee(nsIInputStream **result,
00206                      nsIInputStream *source,
00207                      nsIOutputStream *sink)
00208 {
00209     nsresult rv;
00210     
00211     nsCOMPtr<nsIInputStreamTee> tee;
00212     NS_NEWXPCOM(tee, nsInputStreamTee);
00213     if (!tee)
00214         return NS_ERROR_OUT_OF_MEMORY;
00215 
00216     rv = tee->SetSource(source);
00217     if (NS_FAILED(rv)) return rv;
00218 
00219     rv = tee->SetSink(sink);
00220     if (NS_FAILED(rv)) return rv;
00221 
00222     NS_ADDREF(*result = tee);
00223     return rv;
00224 }