1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsUnicharInputStream.h"
8 #include "nsIInputStream.h"
12 #include "nsStreamUtils.h"
13 #include "nsConverterInputStream.h"
14 #include "mozilla/Attributes.h"
22 #define STRING_BUFFER_SIZE 8192
24 class StringUnicharInputStream final
: public nsIUnicharInputStream
{
26 explicit StringUnicharInputStream(const nsAString
& aString
)
27 : mString(aString
), mPos(0), mLen(aString
.Length()) {}
30 NS_DECL_NSIUNICHARINPUTSTREAM
37 ~StringUnicharInputStream() = default;
41 StringUnicharInputStream::Read(char16_t
* aBuf
, uint32_t aCount
,
42 uint32_t* aReadCount
) {
47 nsAString::const_iterator iter
;
48 mString
.BeginReading(iter
);
49 const char16_t
* us
= iter
.get();
50 uint32_t amount
= mLen
- mPos
;
51 if (amount
> aCount
) {
54 memcpy(aBuf
, us
+ mPos
, sizeof(char16_t
) * amount
);
61 StringUnicharInputStream::ReadSegments(nsWriteUnicharSegmentFun aWriter
,
62 void* aClosure
, uint32_t aCount
,
63 uint32_t* aReadCount
) {
64 uint32_t bytesWritten
;
65 uint32_t totalBytesWritten
= 0;
68 aCount
= XPCOM_MIN
<uint32_t>(mString
.Length() - mPos
, aCount
);
70 nsAString::const_iterator iter
;
71 mString
.BeginReading(iter
);
74 rv
= aWriter(this, aClosure
, iter
.get() + mPos
, totalBytesWritten
, aCount
,
78 // don't propagate errors to the caller
82 aCount
-= bytesWritten
;
83 totalBytesWritten
+= bytesWritten
;
87 *aReadCount
= totalBytesWritten
;
93 StringUnicharInputStream::ReadString(uint32_t aCount
, nsAString
& aString
,
94 uint32_t* aReadCount
) {
99 uint32_t amount
= mLen
- mPos
;
100 if (amount
> aCount
) {
103 aString
= Substring(mString
, mPos
, amount
);
105 *aReadCount
= amount
;
109 nsresult
StringUnicharInputStream::Close() {
114 NS_IMPL_ISUPPORTS(StringUnicharInputStream
, nsIUnicharInputStream
)
116 //----------------------------------------------------------------------
118 nsresult
NS_NewUnicharInputStream(nsIInputStream
* aStreamToWrap
,
119 nsIUnicharInputStream
** aResult
) {
122 // Create converter input stream
123 RefPtr
<nsConverterInputStream
> it
= new nsConverterInputStream();
124 nsresult rv
= it
->Init(aStreamToWrap
, "UTF-8", STRING_BUFFER_SIZE
,
125 nsIConverterInputStream::ERRORS_ARE_FATAL
);