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
00074 #ifndef U_HIDE_DRAFT_API
00075
00086 inline void AppendU8(const char* bytes, int32_t n) {
00087 Append(bytes, n);
00088 }
00089
00090 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
00091
00102 inline void AppendU8(const char8_t* bytes, int32_t n) {
00103 Append(reinterpret_cast<const char*>(bytes), n);
00104 }
00105 #endif
00106 #endif // U_HIDE_DRAFT_API
00107
00150 virtual char* GetAppendBuffer(int32_t min_capacity,
00151 int32_t desired_capacity_hint,
00152 char* scratch, int32_t scratch_capacity,
00153 int32_t* result_capacity);
00154
00163 virtual void Flush();
00164
00165 private:
00166 ByteSink(const ByteSink &) = delete;
00167 ByteSink &operator=(const ByteSink &) = delete;
00168 };
00169
00170
00171
00172
00182 class U_COMMON_API CheckedArrayByteSink : public ByteSink {
00183 public:
00190 CheckedArrayByteSink(char* outbuf, int32_t capacity);
00195 virtual ~CheckedArrayByteSink();
00204 virtual CheckedArrayByteSink& Reset();
00211 virtual void Append(const char* bytes, int32_t n);
00226 virtual char* GetAppendBuffer(int32_t min_capacity,
00227 int32_t desired_capacity_hint,
00228 char* scratch, int32_t scratch_capacity,
00229 int32_t* result_capacity);
00235 int32_t NumberOfBytesWritten() const { return size_; }
00242 UBool Overflowed() const { return overflowed_; }
00250 int32_t NumberOfBytesAppended() const { return appended_; }
00251 private:
00252 char* outbuf_;
00253 const int32_t capacity_;
00254 int32_t size_;
00255 int32_t appended_;
00256 UBool overflowed_;
00257
00258 CheckedArrayByteSink() = delete;
00259 CheckedArrayByteSink(const CheckedArrayByteSink &) = delete;
00260 CheckedArrayByteSink &operator=(const CheckedArrayByteSink &) = delete;
00261 };
00262
00268 template<typename StringClass>
00269 class StringByteSink : public ByteSink {
00270 public:
00276 StringByteSink(StringClass* dest) : dest_(dest) { }
00284 StringByteSink(StringClass* dest, int32_t initialAppendCapacity) : dest_(dest) {
00285 if (initialAppendCapacity > 0 &&
00286 (uint32_t)initialAppendCapacity > (dest->capacity() - dest->length())) {
00287 dest->reserve(dest->length() + initialAppendCapacity);
00288 }
00289 }
00296 virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }
00297 private:
00298 StringClass* dest_;
00299
00300 StringByteSink() = delete;
00301 StringByteSink(const StringByteSink &) = delete;
00302 StringByteSink &operator=(const StringByteSink &) = delete;
00303 };
00304
00305 U_NAMESPACE_END
00306
00307 #endif
00308
00309 #endif // __BYTESTREAM_H__