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"
41 #include "unicode/uobject.h"
42 #include "unicode/std_string.h"
44 #if U_SHOW_CPLUSPLUS_API
48 * A ByteSink can be filled with bytes.
51 class U_COMMON_API ByteSink
: public UMemory
{
54 * Default constructor.
65 * Append "bytes[0,n-1]" to this.
66 * @param bytes the pointer to the bytes
67 * @param n the number of bytes; must be non-negative
70 virtual void Append(const char* bytes
, int32_t n
) = 0;
73 * Returns a writable buffer for appending and writes the buffer's capacity to
74 * *result_capacity. Guarantees *result_capacity>=min_capacity.
75 * May return a pointer to the caller-owned scratch buffer which must have
76 * scratch_capacity>=min_capacity.
77 * The returned buffer is only valid until the next operation
80 * After writing at most *result_capacity bytes, call Append() with the
81 * pointer returned from this function and the number of bytes written.
82 * Many Append() implementations will avoid copying bytes if this function
83 * returned an internal buffer.
85 * Partial usage example:
87 * char* buffer = sink->GetAppendBuffer(..., &capacity);
88 * ... Write n bytes into buffer, with n <= capacity.
89 * sink->Append(buffer, n);
90 * In many implementations, that call to Append will avoid copying bytes.
92 * If the ByteSink allocates or reallocates an internal buffer, it should use
93 * the desired_capacity_hint if appropriate.
94 * If a caller cannot provide a reasonable guess at the desired capacity,
95 * it should pass desired_capacity_hint=0.
97 * If a non-scratch buffer is returned, the caller may only pass
98 * a prefix to it to Append().
99 * That is, it is not correct to pass an interior pointer to Append().
101 * The default implementation always returns the scratch buffer.
103 * @param min_capacity required minimum capacity of the returned buffer;
104 * must be non-negative
105 * @param desired_capacity_hint desired capacity of the returned buffer;
106 * must be non-negative
107 * @param scratch default caller-owned buffer
108 * @param scratch_capacity capacity of the scratch buffer
109 * @param result_capacity pointer to an integer which will be set to the
110 * capacity of the returned buffer
111 * @return a buffer with *result_capacity>=min_capacity
114 virtual char* GetAppendBuffer(int32_t min_capacity
,
115 int32_t desired_capacity_hint
,
116 char* scratch
, int32_t scratch_capacity
,
117 int32_t* result_capacity
);
120 * Flush internal buffers.
121 * Some byte sinks use internal buffers or provide buffering
122 * and require calling Flush() at the end of the stream.
123 * The ByteSink should be ready for further Append() calls after Flush().
124 * The default implementation of Flush() does nothing.
127 virtual void Flush();
130 ByteSink(const ByteSink
&) = delete;
131 ByteSink
&operator=(const ByteSink
&) = delete;
134 // -------------------------------------------------------------
135 // Some standard implementations
138 * Implementation of ByteSink that writes to a flat byte array,
139 * with bounds-checking:
140 * This sink will not write more than capacity bytes to outbuf.
141 * If more than capacity bytes are Append()ed, then excess bytes are ignored,
142 * and Overflowed() will return true.
143 * Overflow does not cause a runtime error.
146 class U_COMMON_API CheckedArrayByteSink
: public ByteSink
{
149 * Constructs a ByteSink that will write to outbuf[0..capacity-1].
150 * @param outbuf buffer to write to
151 * @param capacity size of the buffer
154 CheckedArrayByteSink(char* outbuf
, int32_t capacity
);
159 virtual ~CheckedArrayByteSink();
161 * Returns the sink to its original state, without modifying the buffer.
162 * Useful for reusing both the buffer and the sink for multiple streams.
163 * Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0
164 * and Overflowed()=FALSE.
168 virtual CheckedArrayByteSink
& Reset();
170 * Append "bytes[0,n-1]" to this.
171 * @param bytes the pointer to the bytes
172 * @param n the number of bytes; must be non-negative
175 virtual void Append(const char* bytes
, int32_t n
);
177 * Returns a writable buffer for appending and writes the buffer's capacity to
178 * *result_capacity. For details see the base class documentation.
179 * @param min_capacity required minimum capacity of the returned buffer;
180 * must be non-negative
181 * @param desired_capacity_hint desired capacity of the returned buffer;
182 * must be non-negative
183 * @param scratch default caller-owned buffer
184 * @param scratch_capacity capacity of the scratch buffer
185 * @param result_capacity pointer to an integer which will be set to the
186 * capacity of the returned buffer
187 * @return a buffer with *result_capacity>=min_capacity
190 virtual char* GetAppendBuffer(int32_t min_capacity
,
191 int32_t desired_capacity_hint
,
192 char* scratch
, int32_t scratch_capacity
,
193 int32_t* result_capacity
);
195 * Returns the number of bytes actually written to the sink.
196 * @return number of bytes written to the buffer
199 int32_t NumberOfBytesWritten() const { return size_
; }
201 * Returns true if any bytes were discarded, i.e., if there was an
202 * attempt to write more than 'capacity' bytes.
203 * @return TRUE if more than 'capacity' bytes were Append()ed
206 UBool
Overflowed() const { return overflowed_
; }
208 * Returns the number of bytes appended to the sink.
209 * If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten()
210 * else they return the same number.
211 * @return number of bytes written to the buffer
214 int32_t NumberOfBytesAppended() const { return appended_
; }
217 const int32_t capacity_
;
222 CheckedArrayByteSink() = delete;
223 CheckedArrayByteSink(const CheckedArrayByteSink
&) = delete;
224 CheckedArrayByteSink
&operator=(const CheckedArrayByteSink
&) = delete;
228 * Implementation of ByteSink that writes to a "string".
229 * The StringClass is usually instantiated with a std::string.
232 template<typename StringClass
>
233 class StringByteSink
: public ByteSink
{
236 * Constructs a ByteSink that will append bytes to the dest string.
237 * @param dest pointer to string object to append to
240 StringByteSink(StringClass
* dest
) : dest_(dest
) { }
242 * Constructs a ByteSink that reserves append capacity and will append bytes to the dest string.
244 * @param dest pointer to string object to append to
245 * @param initialAppendCapacity capacity beyond dest->length() to be reserve()d
248 StringByteSink(StringClass
* dest
, int32_t initialAppendCapacity
) : dest_(dest
) {
249 if (initialAppendCapacity
> 0 &&
250 (uint32_t)initialAppendCapacity
> (dest
->capacity() - dest
->length())) {
251 dest
->reserve(dest
->length() + initialAppendCapacity
);
255 * Append "bytes[0,n-1]" to this.
256 * @param data the pointer to the bytes
257 * @param n the number of bytes; must be non-negative
260 virtual void Append(const char* data
, int32_t n
) { dest_
->append(data
, n
); }
264 StringByteSink() = delete;
265 StringByteSink(const StringByteSink
&) = delete;
266 StringByteSink
&operator=(const StringByteSink
&) = delete;
270 #endif // U_SHOW_CPLUSPLUS_API
272 #endif // __BYTESTREAM_H__