00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef __BYTESTREAM_H__
00033 #define __BYTESTREAM_H__
00034
00040 #include "unicode/utypes.h"
00041
00042 #if U_SHOW_CPLUSPLUS_API
00043
00044 #include "unicode/uobject.h"
00045 #include "unicode/std_string.h"
00046
00047 U_NAMESPACE_BEGIN
00048
00053 class U_COMMON_API ByteSink : public UMemory {
00054 public:
00059 ByteSink() { }
00064 virtual ~ByteSink();
00065
00072 virtual void Append(const char* bytes, int32_t n) = 0;
00073
00085 inline void AppendU8(const char* bytes, int32_t n) {
00086 Append(bytes, n);
00087 }
00088
00089 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
00090
00101 inline void AppendU8(const char8_t* bytes, int32_t n) {
00102 Append(reinterpret_cast<const char*>(bytes), n);
00103 }
00104 #endif
00105
00148 virtual char* GetAppendBuffer(int32_t min_capacity,
00149 int32_t desired_capacity_hint,
00150 char* scratch, int32_t scratch_capacity,
00151 int32_t* result_capacity);
00152
00161 virtual void Flush();
00162
00163 private:
00164 ByteSink(const ByteSink &) = delete;
00165 ByteSink &operator=(const ByteSink &) = delete;
00166 };
00167
00168
00169
00170
00180 class U_COMMON_API CheckedArrayByteSink : public ByteSink {
00181 public:
00188 CheckedArrayByteSink(char* outbuf, int32_t capacity);
00193 virtual ~CheckedArrayByteSink();
00202 virtual CheckedArrayByteSink& Reset();
00209 virtual void Append(const char* bytes, int32_t n) override;
00224 virtual char* GetAppendBuffer(int32_t min_capacity,
00225 int32_t desired_capacity_hint,
00226 char* scratch, int32_t scratch_capacity,
00227 int32_t* result_capacity) override;
00233 int32_t NumberOfBytesWritten() const { return size_; }
00240 UBool Overflowed() const { return overflowed_; }
00248 int32_t NumberOfBytesAppended() const { return appended_; }
00249 private:
00250 char* outbuf_;
00251 const int32_t capacity_;
00252 int32_t size_;
00253 int32_t appended_;
00254 UBool overflowed_;
00255
00256 CheckedArrayByteSink() = delete;
00257 CheckedArrayByteSink(const CheckedArrayByteSink &) = delete;
00258 CheckedArrayByteSink &operator=(const CheckedArrayByteSink &) = delete;
00259 };
00260
00266 template<typename StringClass>
00267 class StringByteSink : public ByteSink {
00268 public:
00274 StringByteSink(StringClass* dest) : dest_(dest) { }
00282 StringByteSink(StringClass* dest, int32_t initialAppendCapacity) : dest_(dest) {
00283 if (initialAppendCapacity > 0 &&
00284 (uint32_t)initialAppendCapacity > (dest->capacity() - dest->length())) {
00285 dest->reserve(dest->length() + initialAppendCapacity);
00286 }
00287 }
00294 virtual void Append(const char* data, int32_t n) override { dest_->append(data, n); }
00295 private:
00296 StringClass* dest_;
00297
00298 StringByteSink() = delete;
00299 StringByteSink(const StringByteSink &) = delete;
00300 StringByteSink &operator=(const StringByteSink &) = delete;
00301 };
00302
00303 U_NAMESPACE_END
00304
00305 #endif
00306
00307 #endif // __BYTESTREAM_H__