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 #include "unicode/uobject.h"
00042 #include "unicode/std_string.h"
00043
00044 U_NAMESPACE_BEGIN
00045
00050 class U_COMMON_API ByteSink : public UMemory {
00051 public:
00056 ByteSink() { }
00061 virtual ~ByteSink();
00062
00069 virtual void Append(const char* bytes, int32_t n) = 0;
00070
00113 virtual char* GetAppendBuffer(int32_t min_capacity,
00114 int32_t desired_capacity_hint,
00115 char* scratch, int32_t scratch_capacity,
00116 int32_t* result_capacity);
00117
00126 virtual void Flush();
00127
00128 private:
00129 ByteSink(const ByteSink &) = delete;
00130 ByteSink &operator=(const ByteSink &) = delete;
00131 };
00132
00133
00134
00135
00145 class U_COMMON_API CheckedArrayByteSink : public ByteSink {
00146 public:
00153 CheckedArrayByteSink(char* outbuf, int32_t capacity);
00158 virtual ~CheckedArrayByteSink();
00167 virtual CheckedArrayByteSink& Reset();
00174 virtual void Append(const char* bytes, int32_t n);
00189 virtual char* GetAppendBuffer(int32_t min_capacity,
00190 int32_t desired_capacity_hint,
00191 char* scratch, int32_t scratch_capacity,
00192 int32_t* result_capacity);
00198 int32_t NumberOfBytesWritten() const { return size_; }
00205 UBool Overflowed() const { return overflowed_; }
00213 int32_t NumberOfBytesAppended() const { return appended_; }
00214 private:
00215 char* outbuf_;
00216 const int32_t capacity_;
00217 int32_t size_;
00218 int32_t appended_;
00219 UBool overflowed_;
00220
00221 CheckedArrayByteSink() = delete;
00222 CheckedArrayByteSink(const CheckedArrayByteSink &) = delete;
00223 CheckedArrayByteSink &operator=(const CheckedArrayByteSink &) = delete;
00224 };
00225
00231 template<typename StringClass>
00232 class StringByteSink : public ByteSink {
00233 public:
00239 StringByteSink(StringClass* dest) : dest_(dest) { }
00240 #ifndef U_HIDE_DRAFT_API
00241
00248 StringByteSink(StringClass* dest, int32_t initialAppendCapacity) : dest_(dest) {
00249 if (initialAppendCapacity > 0 &&
00250 (uint32_t)initialAppendCapacity > (dest->capacity() - dest->length())) {
00251 dest->reserve(dest->length() + initialAppendCapacity);
00252 }
00253 }
00254 #endif // U_HIDE_DRAFT_API
00255
00261 virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }
00262 private:
00263 StringClass* dest_;
00264
00265 StringByteSink() = delete;
00266 StringByteSink(const StringByteSink &) = delete;
00267 StringByteSink &operator=(const StringByteSink &) = delete;
00268 };
00269
00270 U_NAMESPACE_END
00271
00272 #endif // __BYTESTREAM_H__