1 : /* vim:set expandtab ts=4 sw=4 sts=4 cin: */
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 unicode stream converter code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Christian Biesinger <cbiesinger@web.de>.
19 : * Portions created by the Initial Developer are Copyright (C) 2005
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsCOMPtr.h"
39 : #include "nsAutoPtr.h"
40 :
41 : #include "nsIServiceManager.h"
42 : #include "nsIOutputStream.h"
43 : #include "nsICharsetConverterManager.h"
44 :
45 : #include "nsConverterOutputStream.h"
46 :
47 13866 : NS_IMPL_ISUPPORTS2(nsConverterOutputStream,
48 : nsIUnicharOutputStream,
49 : nsIConverterOutputStream)
50 :
51 910 : nsConverterOutputStream::~nsConverterOutputStream()
52 : {
53 455 : Close();
54 455 : }
55 :
56 : NS_IMETHODIMP
57 633 : nsConverterOutputStream::Init(nsIOutputStream* aOutStream,
58 : const char* aCharset,
59 : PRUint32 aBufferSize /* ignored */,
60 : PRUnichar aReplacementChar)
61 : {
62 633 : NS_PRECONDITION(aOutStream, "Null output stream!");
63 :
64 633 : if (!aCharset)
65 0 : aCharset = "UTF-8";
66 :
67 : nsresult rv;
68 : nsCOMPtr<nsICharsetConverterManager> ccm =
69 1266 : do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
70 633 : if (NS_FAILED(rv)) return rv;
71 :
72 633 : rv = ccm->GetUnicodeEncoder(aCharset, getter_AddRefs(mConverter));
73 633 : if (NS_FAILED(rv))
74 0 : return rv;
75 :
76 633 : mOutStream = aOutStream;
77 :
78 : PRInt32 behaviour = aReplacementChar ? nsIUnicodeEncoder::kOnError_Replace
79 633 : : nsIUnicodeEncoder::kOnError_Signal;
80 633 : return mConverter->
81 : SetOutputErrorBehavior(behaviour,
82 : nsnull,
83 633 : aReplacementChar);
84 : }
85 :
86 : NS_IMETHODIMP
87 85513 : nsConverterOutputStream::Write(PRUint32 aCount, const PRUnichar* aChars,
88 : bool* aSuccess)
89 : {
90 85513 : if (!mOutStream) {
91 0 : NS_ASSERTION(!mConverter, "Closed streams shouldn't have converters");
92 0 : return NS_BASE_STREAM_CLOSED;
93 : }
94 85513 : NS_ASSERTION(mConverter, "Must have a converter when not closed");
95 :
96 85513 : PRInt32 inLen = aCount;
97 :
98 : PRInt32 maxLen;
99 85513 : nsresult rv = mConverter->GetMaxLength(aChars, inLen, &maxLen);
100 85513 : NS_ENSURE_SUCCESS(rv, rv);
101 :
102 171026 : nsCAutoString buf;
103 85513 : buf.SetLength(maxLen);
104 85513 : if (buf.Length() != (PRUint32) maxLen)
105 0 : return NS_ERROR_OUT_OF_MEMORY;
106 :
107 85513 : PRInt32 outLen = maxLen;
108 85513 : rv = mConverter->Convert(aChars, &inLen, buf.BeginWriting(), &outLen);
109 85513 : if (NS_FAILED(rv))
110 0 : return rv;
111 85513 : if (rv == NS_ERROR_UENC_NOMAPPING) {
112 : // Yes, NS_ERROR_UENC_NOMAPPING is a success code
113 0 : return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
114 : }
115 85513 : NS_ASSERTION((PRUint32) inLen == aCount,
116 : "Converter didn't consume all the data!");
117 :
118 : PRUint32 written;
119 85513 : rv = mOutStream->Write(buf.get(), outLen, &written);
120 85513 : *aSuccess = NS_SUCCEEDED(rv) && written == PRUint32(outLen);
121 85513 : return rv;
122 :
123 : }
124 :
125 : NS_IMETHODIMP
126 85513 : nsConverterOutputStream::WriteString(const nsAString& aString, bool* aSuccess)
127 : {
128 85513 : PRInt32 inLen = aString.Length();
129 85513 : nsAString::const_iterator i;
130 85513 : aString.BeginReading(i);
131 85513 : return Write(inLen, i.get(), aSuccess);
132 : }
133 :
134 : NS_IMETHODIMP
135 632 : nsConverterOutputStream::Flush()
136 : {
137 632 : if (!mOutStream)
138 0 : return NS_OK; // Already closed.
139 :
140 : char buf[1024];
141 632 : PRInt32 size = sizeof(buf);
142 632 : nsresult rv = mConverter->Finish(buf, &size);
143 632 : NS_ASSERTION(rv != NS_OK_UENC_MOREOUTPUT,
144 : "1024 bytes ought to be enough for everyone");
145 632 : if (NS_FAILED(rv))
146 0 : return rv;
147 : PRUint32 written;
148 632 : rv = mOutStream->Write(buf, size, &written);
149 632 : if (NS_FAILED(rv)) {
150 0 : NS_WARNING("Flush() lost data!");
151 0 : return rv;
152 : }
153 632 : if (written != PRUint32(size)) {
154 0 : NS_WARNING("Flush() lost data!");
155 0 : return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
156 : }
157 632 : return rv;
158 : }
159 :
160 : NS_IMETHODIMP
161 1045 : nsConverterOutputStream::Close()
162 : {
163 1045 : if (!mOutStream)
164 413 : return NS_OK; // Already closed.
165 :
166 632 : nsresult rv1 = Flush();
167 :
168 632 : nsresult rv2 = mOutStream->Close();
169 632 : mOutStream = nsnull;
170 632 : mConverter = nsnull;
171 632 : return NS_FAILED(rv1) ? rv1 : rv2;
172 : }
173 :
|