diff --git a/icu4c/source/common/bytestream.cpp b/icu4c/source/common/bytestream.cpp index ce7d1cbd43..d9cc94851a 100644 --- a/icu4c/source/common/bytestream.cpp +++ b/icu4c/source/common/bytestream.cpp @@ -26,7 +26,12 @@ void ByteSink::Flush() {} CheckedArrayByteSink::CheckedArrayByteSink(char* outbuf, int32_t capacity) : outbuf_(outbuf), capacity_(capacity < 0 ? 0 : capacity), - size_(0), appended_(0), overflowed_(false) { + size_(0), appended_(0), overflowed_(FALSE) { +} + +CheckedArrayByteSink& CheckedArrayByteSink::Reset() { + size_ = appended_ = 0; + overflowed_ = FALSE; } void CheckedArrayByteSink::Append(const char* bytes, int32_t n) { @@ -37,7 +42,7 @@ void CheckedArrayByteSink::Append(const char* bytes, int32_t n) { int32_t available = capacity_ - size_; if (n > available) { n = available; - overflowed_ = true; + overflowed_ = TRUE; } if (n > 0 && bytes != (outbuf_ + size_)) { uprv_memcpy(outbuf_ + size_, bytes, n); diff --git a/icu4c/source/common/unicode/bytestream.h b/icu4c/source/common/unicode/bytestream.h index 097d51489d..bbf90aa62a 100644 --- a/icu4c/source/common/unicode/bytestream.h +++ b/icu4c/source/common/unicode/bytestream.h @@ -148,6 +148,14 @@ public: * @stable ICU 4.2 */ CheckedArrayByteSink(char* outbuf, int32_t capacity); + /** + * Returns the sink to its original state, without modifying the buffer. + * Useful for reusing both the buffer and the sink for multiple streams. + * Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0 + * and Overflowed()=FALSE. + * @draft ICU 4.6 + */ + CheckedArrayByteSink& Reset(); /** * Append "bytes[0,n-1]" to this. * @param bytes the pointer to the bytes @@ -199,7 +207,7 @@ private: const int32_t capacity_; int32_t size_; int32_t appended_; - bool overflowed_; + UBool overflowed_; CheckedArrayByteSink(); ///< default constructor not implemented CheckedArrayByteSink(const CheckedArrayByteSink &); ///< copy constructor not implemented CheckedArrayByteSink &operator=(const CheckedArrayByteSink &); ///< assignment operator not implemented diff --git a/icu4c/source/test/intltest/strtest.cpp b/icu4c/source/test/intltest/strtest.cpp index 228482bb49..d4a53c9e83 100644 --- a/icu4c/source/test/intltest/strtest.cpp +++ b/icu4c/source/test/intltest/strtest.cpp @@ -443,7 +443,8 @@ StringTest::TestCheckedArrayByteSink() { CheckedArrayByteSink sink(buffer, (int32_t)sizeof(buffer)); sink.Append("abc", 3); if(!(sink.NumberOfBytesAppended() == 3 && sink.NumberOfBytesWritten() == 3 && - 0 == memcmp("abc", buffer, 3) && buffer[3] == '!') + 0 == memcmp("abc", buffer, 3) && buffer[3] == '!') && + !sink.Overflowed() ) { errln("CheckedArrayByteSink did not Append() as expected"); return; @@ -488,6 +489,14 @@ StringTest::TestCheckedArrayByteSink() { errln("CheckedArrayByteSink did not Append(scratch buffer) as expected"); return; } + sink.Reset().Append("123", 3); + if(!(sink.NumberOfBytesAppended() == 3 && sink.NumberOfBytesWritten() == 3 && + 0 == memcmp("123defghijklmnopqrstuvwxyz", buffer, (int32_t)sizeof(buffer)) && + !sink.Overflowed()) + ) { + errln("CheckedArrayByteSink did not Reset().Append() as expected"); + return; + } } void