Back to index

lightning-sunbird  0.9+nobinonly
nsConverterOutputStream.cpp
Go to the documentation of this file.
00001 /* vim:set expandtab ts=4 sw=4 sts=4 cin: */
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 unicode stream converter code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Christian Biesinger <cbiesinger@web.de>.
00019  * Portions created by the Initial Developer are Copyright (C) 2005
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsCOMPtr.h"
00039 #include "nsAutoPtr.h"
00040 
00041 #include "nsIServiceManager.h"
00042 #include "nsIOutputStream.h"
00043 #include "nsICharsetConverterManager.h"
00044 
00045 #include "nsConverterOutputStream.h"
00046 
00047 NS_IMPL_ISUPPORTS2(nsConverterOutputStream,
00048                    nsIUnicharOutputStream,
00049                    nsIConverterOutputStream)
00050 
00051 nsConverterOutputStream::~nsConverterOutputStream()
00052 {
00053     Close();
00054 }
00055 
00056 NS_IMETHODIMP
00057 nsConverterOutputStream::Init(nsIOutputStream* aOutStream,
00058                               const char*      aCharset,
00059                               PRUint32         aBufferSize /* ignored */,
00060                               PRUnichar        aReplacementChar)
00061 {
00062     NS_PRECONDITION(aOutStream, "Null output stream!");
00063 
00064     if (!aCharset)
00065         aCharset = "UTF-8";
00066 
00067     nsresult rv;
00068     nsCOMPtr<nsICharsetConverterManager> ccm =
00069         do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
00070     if (NS_FAILED(rv)) return rv;
00071 
00072     rv = ccm->GetUnicodeEncoder(aCharset, getter_AddRefs(mConverter));
00073     if (NS_FAILED(rv))
00074         return rv;
00075 
00076     mOutStream = aOutStream;
00077 
00078     PRInt32 behaviour = aReplacementChar ? nsIUnicodeEncoder::kOnError_Replace
00079                                          : nsIUnicodeEncoder::kOnError_Signal;
00080     return mConverter->
00081         SetOutputErrorBehavior(behaviour,
00082                                nsnull,
00083                                aReplacementChar);
00084 }
00085 
00086 NS_IMETHODIMP
00087 nsConverterOutputStream::Write(PRUint32 aCount, const PRUnichar* aChars,
00088                                PRBool* aSuccess)
00089 {
00090     if (!mOutStream) {
00091         NS_ASSERTION(!mConverter, "Closed streams shouldn't have converters");
00092         return NS_BASE_STREAM_CLOSED;
00093     }
00094     NS_ASSERTION(mConverter, "Must have a converter when not closed");
00095 
00096     PRInt32 inLen = aCount;
00097 
00098     PRInt32 maxLen;
00099     nsresult rv = mConverter->GetMaxLength(aChars, inLen, &maxLen);
00100     NS_ENSURE_SUCCESS(rv, rv);
00101 
00102     nsCAutoString buf;
00103     buf.SetLength(maxLen);
00104     if (buf.Length() != maxLen)
00105         return NS_ERROR_OUT_OF_MEMORY;
00106 
00107     PRInt32 outLen = maxLen;
00108     rv = mConverter->Convert(aChars, &inLen, buf.BeginWriting(), &outLen);
00109     if (NS_FAILED(rv))
00110         return rv;
00111     if (rv == NS_ERROR_UENC_NOMAPPING) {
00112         // Yes, NS_ERROR_UENC_NOMAPPING is a success code
00113         return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
00114     }
00115     NS_ASSERTION(inLen == aCount,
00116                  "Converter didn't consume all the data!");
00117 
00118     PRUint32 written;
00119     rv = mOutStream->Write(buf.get(), outLen, &written);
00120     *aSuccess = NS_SUCCEEDED(rv) && written == PRUint32(outLen);
00121     return rv;
00122 
00123 }
00124 
00125 NS_IMETHODIMP
00126 nsConverterOutputStream::WriteString(const nsAString& aString, PRBool* aSuccess)
00127 {
00128     PRInt32 inLen = aString.Length();
00129     nsAString::const_iterator i;
00130     aString.BeginReading(i);
00131     return Write(inLen, i.get(), aSuccess);
00132 }
00133 
00134 NS_IMETHODIMP
00135 nsConverterOutputStream::Flush()
00136 {
00137     if (!mOutStream)
00138         return NS_OK; // Already closed.
00139 
00140     char buf[1024];
00141     PRInt32 size = sizeof(buf);
00142     nsresult rv = mConverter->Finish(buf, &size);
00143     NS_ASSERTION(rv != NS_OK_UENC_MOREOUTPUT,
00144                  "1024 bytes ought to be enough for everyone");
00145     if (NS_FAILED(rv))
00146         return rv;
00147     PRUint32 written;
00148     rv = mOutStream->Write(buf, size, &written);
00149     if (NS_FAILED(rv)) {
00150         NS_WARNING("Flush() lost data!");
00151         return rv;
00152     }
00153     if (written != PRUint32(size)) {
00154         NS_WARNING("Flush() lost data!");
00155         return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
00156     }
00157     return rv;
00158 }
00159 
00160 NS_IMETHODIMP
00161 nsConverterOutputStream::Close()
00162 {
00163     if (!mOutStream)
00164         return NS_OK; // Already closed.
00165 
00166     nsresult rv1 = Flush();
00167 
00168     nsresult rv2 = mOutStream->Close();
00169     mOutStream = nsnull;
00170     mConverter = nsnull;
00171     return NS_FAILED(rv1) ? rv1 : rv2;
00172 }
00173