2007-03-22 10:30:00 -07:00
|
|
|
/* vim:set expandtab ts=4 sw=4 sts=4 cin: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsICharsetConverterManager.h"
|
|
|
|
|
|
|
|
#include "nsConverterOutputStream.h"
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS2(nsConverterOutputStream,
|
|
|
|
nsIUnicharOutputStream,
|
|
|
|
nsIConverterOutputStream)
|
|
|
|
|
|
|
|
nsConverterOutputStream::~nsConverterOutputStream()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsConverterOutputStream::Init(nsIOutputStream* aOutStream,
|
|
|
|
const char* aCharset,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aBufferSize /* ignored */,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUnichar aReplacementChar)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aOutStream, "Null output stream!");
|
|
|
|
|
|
|
|
if (!aCharset)
|
|
|
|
aCharset = "UTF-8";
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsICharsetConverterManager> ccm =
|
|
|
|
do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = ccm->GetUnicodeEncoder(aCharset, getter_AddRefs(mConverter));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
mOutStream = aOutStream;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t behaviour = aReplacementChar ? nsIUnicodeEncoder::kOnError_Replace
|
2007-03-22 10:30:00 -07:00
|
|
|
: nsIUnicodeEncoder::kOnError_Signal;
|
|
|
|
return mConverter->
|
|
|
|
SetOutputErrorBehavior(behaviour,
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr,
|
2007-03-22 10:30:00 -07:00
|
|
|
aReplacementChar);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsConverterOutputStream::Write(uint32_t aCount, const PRUnichar* aChars,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* aSuccess)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!mOutStream) {
|
|
|
|
NS_ASSERTION(!mConverter, "Closed streams shouldn't have converters");
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
|
|
|
NS_ASSERTION(mConverter, "Must have a converter when not closed");
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t inLen = aCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t maxLen;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = mConverter->GetMaxLength(aChars, inLen, &maxLen);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString buf;
|
2007-03-22 10:30:00 -07:00
|
|
|
buf.SetLength(maxLen);
|
2012-08-22 08:56:38 -07:00
|
|
|
if (buf.Length() != (uint32_t) maxLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t outLen = maxLen;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = mConverter->Convert(aChars, &inLen, buf.BeginWriting(), &outLen);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (rv == NS_ERROR_UENC_NOMAPPING) {
|
|
|
|
// Yes, NS_ERROR_UENC_NOMAPPING is a success code
|
|
|
|
return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
NS_ASSERTION((uint32_t) inLen == aCount,
|
2007-03-22 10:30:00 -07:00
|
|
|
"Converter didn't consume all the data!");
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t written;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = mOutStream->Write(buf.get(), outLen, &written);
|
2012-08-22 08:56:38 -07:00
|
|
|
*aSuccess = NS_SUCCEEDED(rv) && written == uint32_t(outLen);
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsConverterOutputStream::WriteString(const nsAString& aString, bool* aSuccess)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t inLen = aString.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAString::const_iterator i;
|
|
|
|
aString.BeginReading(i);
|
|
|
|
return Write(inLen, i.get(), aSuccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsConverterOutputStream::Flush()
|
|
|
|
{
|
|
|
|
if (!mOutStream)
|
|
|
|
return NS_OK; // Already closed.
|
|
|
|
|
|
|
|
char buf[1024];
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t size = sizeof(buf);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = mConverter->Finish(buf, &size);
|
|
|
|
NS_ASSERTION(rv != NS_OK_UENC_MOREOUTPUT,
|
|
|
|
"1024 bytes ought to be enough for everyone");
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2012-05-24 10:46:48 -07:00
|
|
|
if (size == 0)
|
|
|
|
return NS_OK;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t written;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = mOutStream->Write(buf, size, &written);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Flush() lost data!");
|
|
|
|
return rv;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
if (written != uint32_t(size)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_WARNING("Flush() lost data!");
|
|
|
|
return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsConverterOutputStream::Close()
|
|
|
|
{
|
|
|
|
if (!mOutStream)
|
|
|
|
return NS_OK; // Already closed.
|
|
|
|
|
|
|
|
nsresult rv1 = Flush();
|
|
|
|
|
|
|
|
nsresult rv2 = mOutStream->Close();
|
2012-07-30 07:20:58 -07:00
|
|
|
mOutStream = nullptr;
|
|
|
|
mConverter = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_FAILED(rv1) ? rv1 : rv2;
|
|
|
|
}
|
|
|
|
|