1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 // Copyright (C) 2009-2012, International Business Machines
4 // Corporation and others. All Rights Reserved.
6 // Copyright 2007 Google Inc. All Rights Reserved.
7 // Author: sanjay@google.com (Sanjay Ghemawat)
9 // Abstract interface that consumes a sequence of bytes (ByteSink).
11 // Used so that we can write a single piece of code that can operate
12 // on a variety of output string types.
14 // Various implementations of this interface are provided:
16 // CheckedArrayByteSink Write to a flat array, with bounds checking
17 // StringByteSink Write to an STL string
19 // This code is a contribution of Google code, and the style used here is
20 // a compromise between the original Google code and the ICU coding guidelines.
21 // For example, data types are ICU-ified (size_t,int->int32_t),
22 // and API comments doxygen-ified, but function names and behavior are
23 // as in the original, if possible.
24 // Assertion-style error handling, not available in ICU, was changed to
25 // parameter "pinning" similar to UnicodeString.
27 // In addition, this is only a partial port of the original Google code,
28 // limited to what was needed so far. The (nearly) complete original code
29 // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
30 // (see ICU ticket 6765, r25517).
32 #ifndef __BYTESTREAM_H__
33 #define __BYTESTREAM_H__
37 * \brief C++ API: Interface for writing bytes, and implementation classes.
40 #include "unicode/utypes.h"
42 #if U_SHOW_CPLUSPLUS_API
44 #include "unicode/uobject.h"
45 #include "unicode/std_string.h"
50 * A ByteSink can be filled with bytes.
53 class U_COMMON_API ByteSink
: public UMemory
{
56 * Default constructor.
67 * Append "bytes[0,n-1]" to this.
68 * @param bytes the pointer to the bytes
69 * @param n the number of bytes; must be non-negative
72 virtual void Append(const char* bytes
, int32_t n
) = 0;
75 * Returns a writable buffer for appending and writes the buffer's capacity to
76 * *result_capacity. Guarantees *result_capacity>=min_capacity.
77 * May return a pointer to the caller-owned scratch buffer which must have
78 * scratch_capacity>=min_capacity.
79 * The returned buffer is only valid until the next operation
82 * After writing at most *result_capacity bytes, call Append() with the
83 * pointer returned from this function and the number of bytes written.
84 * Many Append() implementations will avoid copying bytes if this function
85 * returned an internal buffer.
87 * Partial usage example:
89 * char* buffer = sink->GetAppendBuffer(..., &capacity);
90 * ... Write n bytes into buffer, with n <= capacity.
91 * sink->Append(buffer, n);
92 * In many implementations, that call to Append will avoid copying bytes.
94 * If the ByteSink allocates or reallocates an internal buffer, it should use
95 * the desired_capacity_hint if appropriate.
96 * If a caller cannot provide a reasonable guess at the desired capacity,
97 * it should pass desired_capacity_hint=0.
99 * If a non-scratch buffer is returned, the caller may only pass
100 * a prefix to it to Append().
101 * That is, it is not correct to pass an interior pointer to Append().
103 * The default implementation always returns the scratch buffer.
105 * @param min_capacity required minimum capacity of the returned buffer;
106 * must be non-negative
107 * @param desired_capacity_hint desired capacity of the returned buffer;
108 * must be non-negative
109 * @param scratch default caller-owned buffer
110 * @param scratch_capacity capacity of the scratch buffer
111 * @param result_capacity pointer to an integer which will be set to the
112 * capacity of the returned buffer
113 * @return a buffer with *result_capacity>=min_capacity
116 virtual char* GetAppendBuffer(int32_t min_capacity
,
117 int32_t desired_capacity_hint
,
118 char* scratch
, int32_t scratch_capacity
,
119 int32_t* result_capacity
);
122 * Flush internal buffers.
123 * Some byte sinks use internal buffers or provide buffering
124 * and require calling Flush() at the end of the stream.
125 * The ByteSink should be ready for further Append() calls after Flush().
126 * The default implementation of Flush() does nothing.
129 virtual void Flush();
132 ByteSink(const ByteSink
&) = delete;
133 ByteSink
&operator=(const ByteSink
&) = delete;
136 // -------------------------------------------------------------
137 // Some standard implementations
140 * Implementation of ByteSink that writes to a flat byte array,
141 * with bounds-checking:
142 * This sink will not write more than capacity bytes to outbuf.
143 * If more than capacity bytes are Append()ed, then excess bytes are ignored,
144 * and Overflowed() will return true.
145 * Overflow does not cause a runtime error.
148 class U_COMMON_API CheckedArrayByteSink
: public ByteSink
{
151 * Constructs a ByteSink that will write to outbuf[0..capacity-1].
152 * @param outbuf buffer to write to
153 * @param capacity size of the buffer
156 CheckedArrayByteSink(char* outbuf
, int32_t capacity
);
161 virtual ~CheckedArrayByteSink();
163 * Returns the sink to its original state, without modifying the buffer.
164 * Useful for reusing both the buffer and the sink for multiple streams.
165 * Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0
166 * and Overflowed()=FALSE.
170 virtual CheckedArrayByteSink
& Reset();
172 * Append "bytes[0,n-1]" to this.
173 * @param bytes the pointer to the bytes
174 * @param n the number of bytes; must be non-negative
177 virtual void Append(const char* bytes
, int32_t n
);
179 * Returns a writable buffer for appending and writes the buffer's capacity to
180 * *result_capacity. For details see the base class documentation.
181 * @param min_capacity required minimum capacity of the returned buffer;
182 * must be non-negative
183 * @param desired_capacity_hint desired capacity of the returned buffer;
184 * must be non-negative
185 * @param scratch default caller-owned buffer
186 * @param scratch_capacity capacity of the scratch buffer
187 * @param result_capacity pointer to an integer which will be set to the
188 * capacity of the returned buffer
189 * @return a buffer with *result_capacity>=min_capacity
192 virtual char* GetAppendBuffer(int32_t min_capacity
,
193 int32_t desired_capacity_hint
,
194 char* scratch
, int32_t scratch_capacity
,
195 int32_t* result_capacity
);
197 * Returns the number of bytes actually written to the sink.
198 * @return number of bytes written to the buffer
201 int32_t NumberOfBytesWritten() const { return size_
; }
203 * Returns true if any bytes were discarded, i.e., if there was an
204 * attempt to write more than 'capacity' bytes.
205 * @return TRUE if more than 'capacity' bytes were Append()ed
208 UBool
Overflowed() const { return overflowed_
; }
210 * Returns the number of bytes appended to the sink.
211 * If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten()
212 * else they return the same number.
213 * @return number of bytes written to the buffer
216 int32_t NumberOfBytesAppended() const { return appended_
; }
219 const int32_t capacity_
;
224 CheckedArrayByteSink() = delete;
225 CheckedArrayByteSink(const CheckedArrayByteSink
&) = delete;
226 CheckedArrayByteSink
&operator=(const CheckedArrayByteSink
&) = delete;
230 * Implementation of ByteSink that writes to a "string".
231 * The StringClass is usually instantiated with a std::string.
234 template<typename StringClass
>
235 class StringByteSink
: public ByteSink
{
238 * Constructs a ByteSink that will append bytes to the dest string.
239 * @param dest pointer to string object to append to
242 StringByteSink(StringClass
* dest
) : dest_(dest
) { }
244 * Constructs a ByteSink that reserves append capacity and will append bytes to the dest string.
246 * @param dest pointer to string object to append to
247 * @param initialAppendCapacity capacity beyond dest->length() to be reserve()d
250 StringByteSink(StringClass
* dest
, int32_t initialAppendCapacity
) : dest_(dest
) {
251 if (initialAppendCapacity
> 0 &&
252 (uint32_t)initialAppendCapacity
> (dest
->capacity() - dest
->length())) {
253 dest
->reserve(dest
->length() + initialAppendCapacity
);
257 * Append "bytes[0,n-1]" to this.
258 * @param data the pointer to the bytes
259 * @param n the number of bytes; must be non-negative
262 virtual void Append(const char* data
, int32_t n
) { dest_
->append(data
, n
); }
266 StringByteSink() = delete;
267 StringByteSink(const StringByteSink
&) = delete;
268 StringByteSink
&operator=(const StringByteSink
&) = delete;
273 #endif /* U_SHOW_CPLUSPLUS_API */
275 #endif // __BYTESTREAM_H__