1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is mozilla.org code.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 2001
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | * Darin Fisher <[email protected]> (original author)
|
---|
24 | *
|
---|
25 | * Alternatively, the contents of this file may be used under the terms of
|
---|
26 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
27 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
28 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
29 | * of those above. If you wish to allow use of your version of this file only
|
---|
30 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
31 | * use your version of this file under the terms of the MPL, indicate your
|
---|
32 | * decision by deleting the provisions above and replace them with the notice
|
---|
33 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
34 | * the provisions above, a recipient may use your version of this file under
|
---|
35 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
36 | *
|
---|
37 | * ***** END LICENSE BLOCK ***** */
|
---|
38 |
|
---|
39 | #include "nsIInputStreamTee.h"
|
---|
40 | #include "nsIInputStream.h"
|
---|
41 | #include "nsIOutputStream.h"
|
---|
42 | #include "nsCOMPtr.h"
|
---|
43 |
|
---|
44 | class nsInputStreamTee : public nsIInputStreamTee
|
---|
45 | {
|
---|
46 | public:
|
---|
47 | NS_DECL_ISUPPORTS
|
---|
48 | NS_DECL_NSIINPUTSTREAM
|
---|
49 | NS_DECL_NSIINPUTSTREAMTEE
|
---|
50 |
|
---|
51 | nsInputStreamTee();
|
---|
52 |
|
---|
53 | private:
|
---|
54 | ~nsInputStreamTee() {}
|
---|
55 |
|
---|
56 | nsresult TeeSegment(const char *buf, PRUint32 count);
|
---|
57 |
|
---|
58 | static NS_METHOD WriteSegmentFun(nsIInputStream *, void *, const char *,
|
---|
59 | PRUint32, PRUint32, PRUint32 *);
|
---|
60 |
|
---|
61 | private:
|
---|
62 | nsCOMPtr<nsIInputStream> mSource;
|
---|
63 | nsCOMPtr<nsIOutputStream> mSink;
|
---|
64 | nsWriteSegmentFun mWriter; // for implementing ReadSegments
|
---|
65 | void *mClosure; // for implementing ReadSegments
|
---|
66 | };
|
---|
67 |
|
---|
68 | nsInputStreamTee::nsInputStreamTee()
|
---|
69 | {
|
---|
70 | }
|
---|
71 |
|
---|
72 | nsresult
|
---|
73 | nsInputStreamTee::TeeSegment(const char *buf, PRUint32 count)
|
---|
74 | {
|
---|
75 | if (!mSink)
|
---|
76 | return NS_OK; // nothing to do
|
---|
77 | nsresult rv;
|
---|
78 | PRUint32 bytesWritten = 0;
|
---|
79 | while (count) {
|
---|
80 | rv = mSink->Write(buf + bytesWritten, count, &bytesWritten);
|
---|
81 | if (NS_FAILED(rv)) {
|
---|
82 | // ok, this is not a fatal error... just drop our reference to mSink
|
---|
83 | // and continue on as if nothing happened.
|
---|
84 | NS_WARNING("Write failed (non-fatal)");
|
---|
85 | // catch possible misuse of the input stream tee
|
---|
86 | NS_ASSERTION(rv != NS_BASE_STREAM_WOULD_BLOCK, "sink must be a blocking stream");
|
---|
87 | mSink = 0;
|
---|
88 | break;
|
---|
89 | }
|
---|
90 | NS_ASSERTION(bytesWritten <= count, "wrote too much");
|
---|
91 | count -= bytesWritten;
|
---|
92 | }
|
---|
93 | return NS_OK;
|
---|
94 | }
|
---|
95 |
|
---|
96 | NS_METHOD
|
---|
97 | nsInputStreamTee::WriteSegmentFun(nsIInputStream *in, void *closure, const char *fromSegment,
|
---|
98 | PRUint32 offset, PRUint32 count, PRUint32 *writeCount)
|
---|
99 | {
|
---|
100 | nsInputStreamTee *tee = NS_REINTERPRET_CAST(nsInputStreamTee *, closure);
|
---|
101 |
|
---|
102 | nsresult rv = tee->mWriter(in, tee->mClosure, fromSegment, offset, count, writeCount);
|
---|
103 | if (NS_FAILED(rv) || (*writeCount == 0)) {
|
---|
104 | NS_ASSERTION((NS_FAILED(rv) ? (*writeCount == 0) : PR_TRUE),
|
---|
105 | "writer returned an error with non-zero writeCount");
|
---|
106 | return rv;
|
---|
107 | }
|
---|
108 |
|
---|
109 | return tee->TeeSegment(fromSegment, *writeCount);
|
---|
110 | }
|
---|
111 |
|
---|
112 | NS_IMPL_ISUPPORTS2(nsInputStreamTee,
|
---|
113 | nsIInputStreamTee,
|
---|
114 | nsIInputStream)
|
---|
115 |
|
---|
116 | NS_IMETHODIMP
|
---|
117 | nsInputStreamTee::Close()
|
---|
118 | {
|
---|
119 | NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED);
|
---|
120 | nsresult rv = mSource->Close();
|
---|
121 | mSource = 0;
|
---|
122 | mSink = 0;
|
---|
123 | return rv;
|
---|
124 | }
|
---|
125 |
|
---|
126 | NS_IMETHODIMP
|
---|
127 | nsInputStreamTee::Available(PRUint32 *avail)
|
---|
128 | {
|
---|
129 | NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED);
|
---|
130 | return mSource->Available(avail);
|
---|
131 | }
|
---|
132 |
|
---|
133 | NS_IMETHODIMP
|
---|
134 | nsInputStreamTee::Read(char *buf, PRUint32 count, PRUint32 *bytesRead)
|
---|
135 | {
|
---|
136 | NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED);
|
---|
137 |
|
---|
138 | nsresult rv = mSource->Read(buf, count, bytesRead);
|
---|
139 | if (NS_FAILED(rv) || (*bytesRead == 0))
|
---|
140 | return rv;
|
---|
141 |
|
---|
142 | return TeeSegment(buf, *bytesRead);
|
---|
143 | }
|
---|
144 |
|
---|
145 | NS_IMETHODIMP
|
---|
146 | nsInputStreamTee::ReadSegments(nsWriteSegmentFun writer,
|
---|
147 | void *closure,
|
---|
148 | PRUint32 count,
|
---|
149 | PRUint32 *bytesRead)
|
---|
150 | {
|
---|
151 | NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED);
|
---|
152 |
|
---|
153 | mWriter = writer;
|
---|
154 | mClosure = closure;
|
---|
155 |
|
---|
156 | return mSource->ReadSegments(WriteSegmentFun, this, count, bytesRead);
|
---|
157 | }
|
---|
158 |
|
---|
159 | NS_IMETHODIMP
|
---|
160 | nsInputStreamTee::IsNonBlocking(PRBool *result)
|
---|
161 | {
|
---|
162 | NS_ENSURE_TRUE(mSource, NS_ERROR_NOT_INITIALIZED);
|
---|
163 | return mSource->IsNonBlocking(result);
|
---|
164 | }
|
---|
165 |
|
---|
166 | NS_IMETHODIMP
|
---|
167 | nsInputStreamTee::SetSource(nsIInputStream *source)
|
---|
168 | {
|
---|
169 | mSource = source;
|
---|
170 | return NS_OK;
|
---|
171 | }
|
---|
172 |
|
---|
173 | NS_IMETHODIMP
|
---|
174 | nsInputStreamTee::GetSource(nsIInputStream **source)
|
---|
175 | {
|
---|
176 | NS_IF_ADDREF(*source = mSource);
|
---|
177 | return NS_OK;
|
---|
178 | }
|
---|
179 |
|
---|
180 | NS_IMETHODIMP
|
---|
181 | nsInputStreamTee::SetSink(nsIOutputStream *sink)
|
---|
182 | {
|
---|
183 | #ifdef DEBUG
|
---|
184 | if (sink) {
|
---|
185 | PRBool nonBlocking;
|
---|
186 | nsresult rv = sink->IsNonBlocking(&nonBlocking);
|
---|
187 | if (NS_FAILED(rv) || nonBlocking)
|
---|
188 | NS_ERROR("sink should be a blocking stream");
|
---|
189 | }
|
---|
190 | #endif
|
---|
191 | mSink = sink;
|
---|
192 | return NS_OK;
|
---|
193 | }
|
---|
194 |
|
---|
195 | NS_IMETHODIMP
|
---|
196 | nsInputStreamTee::GetSink(nsIOutputStream **sink)
|
---|
197 | {
|
---|
198 | NS_IF_ADDREF(*sink = mSink);
|
---|
199 | return NS_OK;
|
---|
200 | }
|
---|
201 |
|
---|
202 | // factory method
|
---|
203 |
|
---|
204 | NS_COM nsresult
|
---|
205 | NS_NewInputStreamTee(nsIInputStream **result,
|
---|
206 | nsIInputStream *source,
|
---|
207 | nsIOutputStream *sink)
|
---|
208 | {
|
---|
209 | nsresult rv;
|
---|
210 |
|
---|
211 | nsCOMPtr<nsIInputStreamTee> tee;
|
---|
212 | NS_NEWXPCOM(tee, nsInputStreamTee);
|
---|
213 | if (!tee)
|
---|
214 | return NS_ERROR_OUT_OF_MEMORY;
|
---|
215 |
|
---|
216 | rv = tee->SetSource(source);
|
---|
217 | if (NS_FAILED(rv)) return rv;
|
---|
218 |
|
---|
219 | rv = tee->SetSink(sink);
|
---|
220 | if (NS_FAILED(rv)) return rv;
|
---|
221 |
|
---|
222 | NS_ADDREF(*result = tee);
|
---|
223 | return rv;
|
---|
224 | }
|
---|