Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* vim:set expandtab ts=4 sw=4 sts=4 cin: */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsCOMPtr.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsIOutputStream.h" |
michael@0 | 9 | #include "nsICharsetConverterManager.h" |
michael@0 | 10 | |
michael@0 | 11 | #include "nsConverterOutputStream.h" |
michael@0 | 12 | #include "nsServiceManagerUtils.h" |
michael@0 | 13 | |
michael@0 | 14 | NS_IMPL_ISUPPORTS(nsConverterOutputStream, |
michael@0 | 15 | nsIUnicharOutputStream, |
michael@0 | 16 | nsIConverterOutputStream) |
michael@0 | 17 | |
michael@0 | 18 | nsConverterOutputStream::~nsConverterOutputStream() |
michael@0 | 19 | { |
michael@0 | 20 | Close(); |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | NS_IMETHODIMP |
michael@0 | 24 | nsConverterOutputStream::Init(nsIOutputStream* aOutStream, |
michael@0 | 25 | const char* aCharset, |
michael@0 | 26 | uint32_t aBufferSize /* ignored */, |
michael@0 | 27 | char16_t aReplacementChar) |
michael@0 | 28 | { |
michael@0 | 29 | NS_PRECONDITION(aOutStream, "Null output stream!"); |
michael@0 | 30 | |
michael@0 | 31 | if (!aCharset) |
michael@0 | 32 | aCharset = "UTF-8"; |
michael@0 | 33 | |
michael@0 | 34 | nsresult rv; |
michael@0 | 35 | nsCOMPtr<nsICharsetConverterManager> ccm = |
michael@0 | 36 | do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv); |
michael@0 | 37 | if (NS_FAILED(rv)) return rv; |
michael@0 | 38 | |
michael@0 | 39 | rv = ccm->GetUnicodeEncoder(aCharset, getter_AddRefs(mConverter)); |
michael@0 | 40 | if (NS_FAILED(rv)) |
michael@0 | 41 | return rv; |
michael@0 | 42 | |
michael@0 | 43 | mOutStream = aOutStream; |
michael@0 | 44 | |
michael@0 | 45 | int32_t behaviour = aReplacementChar ? nsIUnicodeEncoder::kOnError_Replace |
michael@0 | 46 | : nsIUnicodeEncoder::kOnError_Signal; |
michael@0 | 47 | return mConverter-> |
michael@0 | 48 | SetOutputErrorBehavior(behaviour, |
michael@0 | 49 | nullptr, |
michael@0 | 50 | aReplacementChar); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | NS_IMETHODIMP |
michael@0 | 54 | nsConverterOutputStream::Write(uint32_t aCount, const char16_t* aChars, |
michael@0 | 55 | bool* aSuccess) |
michael@0 | 56 | { |
michael@0 | 57 | if (!mOutStream) { |
michael@0 | 58 | NS_ASSERTION(!mConverter, "Closed streams shouldn't have converters"); |
michael@0 | 59 | return NS_BASE_STREAM_CLOSED; |
michael@0 | 60 | } |
michael@0 | 61 | NS_ASSERTION(mConverter, "Must have a converter when not closed"); |
michael@0 | 62 | |
michael@0 | 63 | int32_t inLen = aCount; |
michael@0 | 64 | |
michael@0 | 65 | int32_t maxLen; |
michael@0 | 66 | nsresult rv = mConverter->GetMaxLength(aChars, inLen, &maxLen); |
michael@0 | 67 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 68 | |
michael@0 | 69 | nsAutoCString buf; |
michael@0 | 70 | buf.SetLength(maxLen); |
michael@0 | 71 | if (buf.Length() != (uint32_t) maxLen) |
michael@0 | 72 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 73 | |
michael@0 | 74 | int32_t outLen = maxLen; |
michael@0 | 75 | rv = mConverter->Convert(aChars, &inLen, buf.BeginWriting(), &outLen); |
michael@0 | 76 | if (NS_FAILED(rv)) |
michael@0 | 77 | return rv; |
michael@0 | 78 | if (rv == NS_ERROR_UENC_NOMAPPING) { |
michael@0 | 79 | // Yes, NS_ERROR_UENC_NOMAPPING is a success code |
michael@0 | 80 | return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA; |
michael@0 | 81 | } |
michael@0 | 82 | NS_ASSERTION((uint32_t) inLen == aCount, |
michael@0 | 83 | "Converter didn't consume all the data!"); |
michael@0 | 84 | |
michael@0 | 85 | uint32_t written; |
michael@0 | 86 | rv = mOutStream->Write(buf.get(), outLen, &written); |
michael@0 | 87 | *aSuccess = NS_SUCCEEDED(rv) && written == uint32_t(outLen); |
michael@0 | 88 | return rv; |
michael@0 | 89 | |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | NS_IMETHODIMP |
michael@0 | 93 | nsConverterOutputStream::WriteString(const nsAString& aString, bool* aSuccess) |
michael@0 | 94 | { |
michael@0 | 95 | int32_t inLen = aString.Length(); |
michael@0 | 96 | nsAString::const_iterator i; |
michael@0 | 97 | aString.BeginReading(i); |
michael@0 | 98 | return Write(inLen, i.get(), aSuccess); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | NS_IMETHODIMP |
michael@0 | 102 | nsConverterOutputStream::Flush() |
michael@0 | 103 | { |
michael@0 | 104 | if (!mOutStream) |
michael@0 | 105 | return NS_OK; // Already closed. |
michael@0 | 106 | |
michael@0 | 107 | char buf[1024]; |
michael@0 | 108 | int32_t size = sizeof(buf); |
michael@0 | 109 | nsresult rv = mConverter->Finish(buf, &size); |
michael@0 | 110 | NS_ASSERTION(rv != NS_OK_UENC_MOREOUTPUT, |
michael@0 | 111 | "1024 bytes ought to be enough for everyone"); |
michael@0 | 112 | if (NS_FAILED(rv)) |
michael@0 | 113 | return rv; |
michael@0 | 114 | if (size == 0) |
michael@0 | 115 | return NS_OK; |
michael@0 | 116 | |
michael@0 | 117 | uint32_t written; |
michael@0 | 118 | rv = mOutStream->Write(buf, size, &written); |
michael@0 | 119 | if (NS_FAILED(rv)) { |
michael@0 | 120 | NS_WARNING("Flush() lost data!"); |
michael@0 | 121 | return rv; |
michael@0 | 122 | } |
michael@0 | 123 | if (written != uint32_t(size)) { |
michael@0 | 124 | NS_WARNING("Flush() lost data!"); |
michael@0 | 125 | return NS_ERROR_LOSS_OF_SIGNIFICANT_DATA; |
michael@0 | 126 | } |
michael@0 | 127 | return rv; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | NS_IMETHODIMP |
michael@0 | 131 | nsConverterOutputStream::Close() |
michael@0 | 132 | { |
michael@0 | 133 | if (!mOutStream) |
michael@0 | 134 | return NS_OK; // Already closed. |
michael@0 | 135 | |
michael@0 | 136 | nsresult rv1 = Flush(); |
michael@0 | 137 | |
michael@0 | 138 | nsresult rv2 = mOutStream->Close(); |
michael@0 | 139 | mOutStream = nullptr; |
michael@0 | 140 | mConverter = nullptr; |
michael@0 | 141 | return NS_FAILED(rv1) ? rv1 : rv2; |
michael@0 | 142 | } |
michael@0 | 143 |