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
00116 virtual char* GetAppendBuffer(int32_t min_capacity,
00117 int32_t desired_capacity_hint,
00118 char* scratch, int32_t scratch_capacity,
00119 int32_t* result_capacity);
00120
00129 virtual void Flush();
00130
00131 private:
00132 ByteSink(const ByteSink &) = delete;
00133 ByteSink &operator=(const ByteSink &) = delete;
00134 };
00135
00136
00137
00138
00148 class U_COMMON_API CheckedArrayByteSink : public ByteSink {
00149 public:
00156 CheckedArrayByteSink(char* outbuf, int32_t capacity);
00161 virtual ~CheckedArrayByteSink();
00170 virtual CheckedArrayByteSink& Reset();
00177 virtual void Append(const char* bytes, int32_t n);
00192 virtual char* GetAppendBuffer(int32_t min_capacity,
00193 int32_t desired_capacity_hint,
00194 char* scratch, int32_t scratch_capacity,
00195 int32_t* result_capacity);
00201 int32_t NumberOfBytesWritten() const { return size_; }
00208 UBool Overflowed() const { return overflowed_; }
00216 int32_t NumberOfBytesAppended() const { return appended_; }
00217 private:
00218 char* outbuf_;
00219 const int32_t capacity_;
00220 int32_t size_;
00221 int32_t appended_;
00222 UBool overflowed_;
00223
00224 CheckedArrayByteSink() = delete;
00225 CheckedArrayByteSink(const CheckedArrayByteSink &) = delete;
00226 CheckedArrayByteSink &operator=(const CheckedArrayByteSink &) = delete;
00227 };
00228
00234 template<typename StringClass>
00235 class StringByteSink : public ByteSink {
00236 public:
00242 StringByteSink(StringClass* dest) : dest_(dest) { }
00250 StringByteSink(StringClass* dest, int32_t initialAppendCapacity) : dest_(dest) {
00251 if (initialAppendCapacity > 0 &&
00252 (uint32_t)initialAppendCapacity > (dest->capacity() - dest->length())) {
00253 dest->reserve(dest->length() + initialAppendCapacity);
00254 }
00255 }
00262 virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }
00263 private:
00264 StringClass* dest_;
00265
00266 StringByteSink() = delete;
00267 StringByteSink(const StringByteSink &) = delete;
00268 StringByteSink &operator=(const StringByteSink &) = delete;
00269 };
00270
00271 U_NAMESPACE_END
00272
00273 #endif
00274
00275 #endif // __BYTESTREAM_H__