2005-06-24 23:44:50 +04:00
|
|
|
/* vim:set expandtab ts=4 sw=4 sts=4 cin: */
|
2012-05-21 15:12:37 +04: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/. */
|
2005-06-24 23:44:50 +04:00
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
|
|
|
|
#include "nsIOutputStream.h"
|
2014-05-08 13:32:00 +04:00
|
|
|
#include "nsString.h"
|
2005-06-24 23:44:50 +04:00
|
|
|
|
|
|
|
#include "nsConverterOutputStream.h"
|
2017-04-27 13:27:03 +03:00
|
|
|
#include "mozilla/Encoding.h"
|
|
|
|
#include "mozilla/Unused.h"
|
2014-05-08 13:32:00 +04:00
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
using namespace mozilla;
|
2005-06-24 23:44:50 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsConverterOutputStream,
|
|
|
|
nsIUnicharOutputStream,
|
|
|
|
nsIConverterOutputStream)
|
2005-06-24 23:44:50 +04:00
|
|
|
|
|
|
|
nsConverterOutputStream::~nsConverterOutputStream()
|
|
|
|
{
|
2006-03-11 19:05:26 +03:00
|
|
|
Close();
|
2005-06-24 23:44:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsConverterOutputStream::Init(nsIOutputStream* aOutStream,
|
2017-06-17 13:30:09 +03:00
|
|
|
const char* aCharset)
|
2005-06-24 23:44:50 +04:00
|
|
|
{
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aOutStream, "Null output stream!");
|
2005-06-24 23:44:50 +04:00
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
const Encoding* encoding;
|
2014-05-08 13:32:00 +04:00
|
|
|
if (!aCharset) {
|
2017-04-27 13:27:03 +03:00
|
|
|
encoding = UTF_8_ENCODING;
|
2014-05-08 13:32:00 +04:00
|
|
|
} else {
|
2017-04-27 13:27:03 +03:00
|
|
|
encoding = Encoding::ForLabelNoReplacement(MakeStringSpan(aCharset));
|
|
|
|
if (!encoding || encoding == UTF_16LE_ENCODING ||
|
|
|
|
encoding == UTF_16BE_ENCODING) {
|
|
|
|
return NS_ERROR_UCONV_NOCONV;
|
|
|
|
}
|
2014-05-08 13:32:00 +04:00
|
|
|
}
|
2005-06-24 23:44:50 +04:00
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
mConverter = encoding->NewEncoder();
|
2005-06-24 23:44:50 +04:00
|
|
|
|
2006-03-11 19:05:26 +03:00
|
|
|
mOutStream = aOutStream;
|
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
return NS_OK;
|
2005-06-24 23:44:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
nsConverterOutputStream::Write(uint32_t aCount, const char16_t* aChars,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool* aSuccess)
|
2005-06-24 23:44:50 +04:00
|
|
|
{
|
2006-01-05 16:27:50 +03:00
|
|
|
if (!mOutStream) {
|
|
|
|
NS_ASSERTION(!mConverter, "Closed streams shouldn't have converters");
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
}
|
2017-04-27 13:27:03 +03:00
|
|
|
MOZ_ASSERT(mConverter, "Must have a converter when not closed");
|
|
|
|
uint8_t buffer[4096];
|
|
|
|
auto dst = MakeSpan(buffer);
|
|
|
|
auto src = MakeSpan(aChars, aCount);
|
|
|
|
for (;;) {
|
|
|
|
uint32_t result;
|
|
|
|
size_t read;
|
|
|
|
size_t written;
|
|
|
|
bool hadErrors;
|
|
|
|
Tie(result, read, written, hadErrors) =
|
|
|
|
mConverter->EncodeFromUTF16(src, dst, false);
|
|
|
|
Unused << hadErrors;
|
|
|
|
src = src.From(read);
|
|
|
|
uint32_t streamWritten;
|
|
|
|
nsresult rv = mOutStream->Write(
|
|
|
|
reinterpret_cast<char*>(dst.Elements()), written, &streamWritten);
|
|
|
|
*aSuccess = NS_SUCCEEDED(rv) && written == streamWritten;
|
|
|
|
if (!(*aSuccess)) {
|
2005-06-24 23:44:50 +04:00
|
|
|
return rv;
|
2017-04-27 13:27:03 +03:00
|
|
|
}
|
|
|
|
if (result == kInputEmpty) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2005-06-24 23:44:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsConverterOutputStream::WriteString(const nsAString& aString, bool* aSuccess)
|
2005-06-24 23:44:50 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t inLen = aString.Length();
|
2005-06-24 23:44:50 +04: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.
|
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
// If we are encoding to ISO-2022-JP, potentially
|
|
|
|
// transition back to the ASCII state. The buffer
|
|
|
|
// needs to be large enough for an additional NCR,
|
|
|
|
// though.
|
|
|
|
uint8_t buffer[12];
|
|
|
|
auto dst = MakeSpan(buffer);
|
|
|
|
Span<char16_t> src(nullptr);
|
|
|
|
uint32_t result;
|
|
|
|
size_t read;
|
|
|
|
size_t written;
|
|
|
|
bool hadErrors;
|
|
|
|
Tie(result, read, written, hadErrors) =
|
|
|
|
mConverter->EncodeFromUTF16(src, dst, true);
|
|
|
|
Unused << hadErrors;
|
|
|
|
MOZ_ASSERT(result == kInputEmpty);
|
|
|
|
uint32_t streamWritten;
|
|
|
|
if (!written) {
|
|
|
|
return NS_OK;
|
2005-06-24 23:44:50 +04:00
|
|
|
}
|
2017-04-27 13:27:03 +03:00
|
|
|
return mOutStream->Write(
|
|
|
|
reinterpret_cast<char*>(dst.Elements()), written, &streamWritten);
|
2005-06-24 23:44:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsConverterOutputStream::Close()
|
|
|
|
{
|
2006-01-05 16:27:50 +03:00
|
|
|
if (!mOutStream)
|
|
|
|
return NS_OK; // Already closed.
|
|
|
|
|
2005-06-24 23:44:50 +04:00
|
|
|
nsresult rv1 = Flush();
|
|
|
|
|
|
|
|
nsresult rv2 = mOutStream->Close();
|
2012-07-30 18:20:58 +04:00
|
|
|
mOutStream = nullptr;
|
|
|
|
mConverter = nullptr;
|
2005-06-24 23:44:50 +04:00
|
|
|
return NS_FAILED(rv1) ? rv1 : rv2;
|
|
|
|
}
|