1 // Copyright (C) 2009-2010, International Business Machines 2 // Corporation and others. All Rights Reserved. 3 // 4 // Copyright 2007 Google Inc. All Rights Reserved. 5 // Author: sanjay (at) google.com (Sanjay Ghemawat) 6 // 7 // Abstract interface that consumes a sequence of bytes (ByteSink). 8 // 9 // Used so that we can write a single piece of code that can operate 10 // on a variety of output string types. 11 // 12 // Various implementations of this interface are provided: 13 // ByteSink: 14 // CheckedArrayByteSink Write to a flat array, with bounds checking 15 // StringByteSink Write to an STL string 16 17 // This code is a contribution of Google code, and the style used here is 18 // a compromise between the original Google code and the ICU coding guidelines. 19 // For example, data types are ICU-ified (size_t,int->int32_t), 20 // and API comments doxygen-ified, but function names and behavior are 21 // as in the original, if possible. 22 // Assertion-style error handling, not available in ICU, was changed to 23 // parameter "pinning" similar to UnicodeString. 24 // 25 // In addition, this is only a partial port of the original Google code, 26 // limited to what was needed so far. The (nearly) complete original code 27 // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib 28 // (see ICU ticket 6765, r25517). 29 30 #ifndef __BYTESTREAM_H__ 31 #define __BYTESTREAM_H__ 32 33 /** 34 * \file 35 * \brief C++ API: Interface for writing bytes, and implementation classes. 36 */ 37 38 #include "unicode/utypes.h" 39 #include "unicode/uobject.h" 40 #include "unicode/std_string.h" 41 42 U_NAMESPACE_BEGIN 43 44 /** 45 * A ByteSink can be filled with bytes. 46 * @stable ICU 4.2 47 */ 48 class U_COMMON_API ByteSink : public UMemory { 49 public: 50 /** 51 * Default constructor. 52 * @stable ICU 4.2 53 */ 54 ByteSink() { } 55 /** 56 * Virtual destructor. 57 * @stable ICU 4.2 58 */ 59 virtual ~ByteSink() { } 60 61 /** 62 * Append "bytes[0,n-1]" to this. 63 * @param bytes the pointer to the bytes 64 * @param n the number of bytes; must be non-negative 65 * @stable ICU 4.2 66 */ 67 virtual void Append(const char* bytes, int32_t n) = 0; 68 69 /** 70 * Returns a writable buffer for appending and writes the buffer's capacity to 71 * *result_capacity. Guarantees *result_capacity>=min_capacity. 72 * May return a pointer to the caller-owned scratch buffer which must have 73 * scratch_capacity>=min_capacity. 74 * The returned buffer is only valid until the next operation 75 * on this ByteSink. 76 * 77 * After writing at most *result_capacity bytes, call Append() with the 78 * pointer returned from this function and the number of bytes written. 79 * Many Append() implementations will avoid copying bytes if this function 80 * returned an internal buffer. 81 * 82 * Partial usage example: 83 * int32_t capacity; 84 * char* buffer = sink->GetAppendBuffer(..., &capacity); 85 * ... Write n bytes into buffer, with n <= capacity. 86 * sink->Append(buffer, n); 87 * In many implementations, that call to Append will avoid copying bytes. 88 * 89 * If the ByteSink allocates or reallocates an internal buffer, it should use 90 * the desired_capacity_hint if appropriate. 91 * If a caller cannot provide a reasonable guess at the desired capacity, 92 * it should pass desired_capacity_hint=0. 93 * 94 * If a non-scratch buffer is returned, the caller may only pass 95 * a prefix to it to Append(). 96 * That is, it is not correct to pass an interior pointer to Append(). 97 * 98 * The default implementation always returns the scratch buffer. 99 * 100 * @param min_capacity required minimum capacity of the returned buffer; 101 * must be non-negative 102 * @param desired_capacity_hint desired capacity of the returned buffer; 103 * must be non-negative 104 * @param scratch default caller-owned buffer 105 * @param scratch_capacity capacity of the scratch buffer 106 * @param result_capacity pointer to an integer which will be set to the 107 * capacity of the returned buffer 108 * @return a buffer with *result_capacity>=min_capacity 109 * @stable ICU 4.2 110 */ 111 virtual char* GetAppendBuffer(int32_t min_capacity, 112 int32_t desired_capacity_hint, 113 char* scratch, int32_t scratch_capacity, 114 int32_t* result_capacity); 115 116 /** 117 * Flush internal buffers. 118 * Some byte sinks use internal buffers or provide buffering 119 * and require calling Flush() at the end of the stream. 120 * The default implementation of Flush() does nothing. 121 * @stable ICU 4.2 122 */ 123 virtual void Flush(); 124 125 private: 126 ByteSink(const ByteSink &); // copy constructor not implemented 127 ByteSink &operator=(const ByteSink &); // assignment operator not implemented 128 }; 129 130 // ------------------------------------------------------------- 131 // Some standard implementations 132 133 /** 134 * Implementation of ByteSink that writes to a flat byte array, 135 * with bounds-checking: 136 * This sink will not write more than capacity bytes to outbuf. 137 * If more than capacity bytes are Append()ed, then excess bytes are ignored, 138 * and Overflowed() will return true. 139 * Overflow does not cause a runtime error. 140 * @stable ICU 4.2 141 */ 142 class U_COMMON_API CheckedArrayByteSink : public ByteSink { 143 public: 144 /** 145 * Constructs a ByteSink that will write to outbuf[0..capacity-1]. 146 * @param outbuf buffer to write to 147 * @param capacity size of the buffer 148 * @stable ICU 4.2 149 */ 150 CheckedArrayByteSink(char* outbuf, int32_t capacity); 151 /** 152 * Append "bytes[0,n-1]" to this. 153 * @param bytes the pointer to the bytes 154 * @param n the number of bytes; must be non-negative 155 * @stable ICU 4.2 156 */ 157 virtual void Append(const char* bytes, int32_t n); 158 /** 159 * Returns a writable buffer for appending and writes the buffer's capacity to 160 * *result_capacity. For details see the base class documentation. 161 * @param min_capacity required minimum capacity of the returned buffer; 162 * must be non-negative 163 * @param desired_capacity_hint desired capacity of the returned buffer; 164 * must be non-negative 165 * @param scratch default caller-owned buffer 166 * @param scratch_capacity capacity of the scratch buffer 167 * @param result_capacity pointer to an integer which will be set to the 168 * capacity of the returned buffer 169 * @return a buffer with *result_capacity>=min_capacity 170 * @stable ICU 4.2 171 */ 172 virtual char* GetAppendBuffer(int32_t min_capacity, 173 int32_t desired_capacity_hint, 174 char* scratch, int32_t scratch_capacity, 175 int32_t* result_capacity); 176 /** 177 * Returns the number of bytes actually written to the sink. 178 * @return number of bytes written to the buffer 179 * @stable ICU 4.2 180 */ 181 int32_t NumberOfBytesWritten() const { return size_; } 182 /** 183 * Returns true if any bytes were discarded, i.e., if there was an 184 * attempt to write more than 'capacity' bytes. 185 * @return TRUE if more than 'capacity' bytes were Append()ed 186 * @stable ICU 4.2 187 */ 188 UBool Overflowed() const { return overflowed_; } 189 private: 190 char* outbuf_; 191 const int32_t capacity_; 192 int32_t size_; 193 bool overflowed_; 194 CheckedArrayByteSink(); ///< default constructor not implemented 195 CheckedArrayByteSink(const CheckedArrayByteSink &); ///< copy constructor not implemented 196 CheckedArrayByteSink &operator=(const CheckedArrayByteSink &); ///< assignment operator not implemented 197 }; 198 199 #if U_HAVE_STD_STRING 200 201 /** 202 * Implementation of ByteSink that writes to a "string". 203 * The StringClass is usually instantiated with a std::string. 204 * @stable ICU 4.2 205 */ 206 template<typename StringClass> 207 class StringByteSink : public ByteSink { 208 public: 209 /** 210 * Constructs a ByteSink that will append bytes to the dest string. 211 * @param dest pointer to string object to append to 212 * @stable ICU 4.2 213 */ 214 StringByteSink(StringClass* dest) : dest_(dest) { } 215 /** 216 * Append "bytes[0,n-1]" to this. 217 * @param data the pointer to the bytes 218 * @param n the number of bytes; must be non-negative 219 * @stable ICU 4.2 220 */ 221 virtual void Append(const char* data, int32_t n) { dest_->append(data, n); } 222 private: 223 StringClass* dest_; 224 StringByteSink(); ///< default constructor not implemented 225 StringByteSink(const StringByteSink &); ///< copy constructor not implemented 226 StringByteSink &operator=(const StringByteSink &); ///< assignment operator not implemented 227 }; 228 229 #endif 230 231 U_NAMESPACE_END 232 233 #endif // __BYTESTREAM_H__ 234