xref: /aosp_15_r20/external/icu/icu4c/source/common/unicode/bytestream.h (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others.
2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html
3*0e209d39SAndroid Build Coastguard Worker // Copyright (C) 2009-2012, International Business Machines
4*0e209d39SAndroid Build Coastguard Worker // Corporation and others. All Rights Reserved.
5*0e209d39SAndroid Build Coastguard Worker //
6*0e209d39SAndroid Build Coastguard Worker // Copyright 2007 Google Inc. All Rights Reserved.
7*0e209d39SAndroid Build Coastguard Worker // Author: [email protected] (Sanjay Ghemawat)
8*0e209d39SAndroid Build Coastguard Worker //
9*0e209d39SAndroid Build Coastguard Worker // Abstract interface that consumes a sequence of bytes (ByteSink).
10*0e209d39SAndroid Build Coastguard Worker //
11*0e209d39SAndroid Build Coastguard Worker // Used so that we can write a single piece of code that can operate
12*0e209d39SAndroid Build Coastguard Worker // on a variety of output string types.
13*0e209d39SAndroid Build Coastguard Worker //
14*0e209d39SAndroid Build Coastguard Worker // Various implementations of this interface are provided:
15*0e209d39SAndroid Build Coastguard Worker //   ByteSink:
16*0e209d39SAndroid Build Coastguard Worker //      CheckedArrayByteSink    Write to a flat array, with bounds checking
17*0e209d39SAndroid Build Coastguard Worker //      StringByteSink          Write to an STL string
18*0e209d39SAndroid Build Coastguard Worker 
19*0e209d39SAndroid Build Coastguard Worker // This code is a contribution of Google code, and the style used here is
20*0e209d39SAndroid Build Coastguard Worker // a compromise between the original Google code and the ICU coding guidelines.
21*0e209d39SAndroid Build Coastguard Worker // For example, data types are ICU-ified (size_t,int->int32_t),
22*0e209d39SAndroid Build Coastguard Worker // and API comments doxygen-ified, but function names and behavior are
23*0e209d39SAndroid Build Coastguard Worker // as in the original, if possible.
24*0e209d39SAndroid Build Coastguard Worker // Assertion-style error handling, not available in ICU, was changed to
25*0e209d39SAndroid Build Coastguard Worker // parameter "pinning" similar to UnicodeString.
26*0e209d39SAndroid Build Coastguard Worker //
27*0e209d39SAndroid Build Coastguard Worker // In addition, this is only a partial port of the original Google code,
28*0e209d39SAndroid Build Coastguard Worker // limited to what was needed so far. The (nearly) complete original code
29*0e209d39SAndroid Build Coastguard Worker // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
30*0e209d39SAndroid Build Coastguard Worker // (see ICU ticket 6765, r25517).
31*0e209d39SAndroid Build Coastguard Worker 
32*0e209d39SAndroid Build Coastguard Worker #ifndef __BYTESTREAM_H__
33*0e209d39SAndroid Build Coastguard Worker #define __BYTESTREAM_H__
34*0e209d39SAndroid Build Coastguard Worker 
35*0e209d39SAndroid Build Coastguard Worker /**
36*0e209d39SAndroid Build Coastguard Worker  * \file
37*0e209d39SAndroid Build Coastguard Worker  * \brief C++ API: Interface for writing bytes, and implementation classes.
38*0e209d39SAndroid Build Coastguard Worker  */
39*0e209d39SAndroid Build Coastguard Worker 
40*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h"
41*0e209d39SAndroid Build Coastguard Worker 
42*0e209d39SAndroid Build Coastguard Worker #if U_SHOW_CPLUSPLUS_API
43*0e209d39SAndroid Build Coastguard Worker 
44*0e209d39SAndroid Build Coastguard Worker #include "unicode/uobject.h"
45*0e209d39SAndroid Build Coastguard Worker #include "unicode/std_string.h"
46*0e209d39SAndroid Build Coastguard Worker 
47*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN
48*0e209d39SAndroid Build Coastguard Worker 
49*0e209d39SAndroid Build Coastguard Worker /**
50*0e209d39SAndroid Build Coastguard Worker  * A ByteSink can be filled with bytes.
51*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 4.2
52*0e209d39SAndroid Build Coastguard Worker  */
53*0e209d39SAndroid Build Coastguard Worker class U_COMMON_API ByteSink : public UMemory {
54*0e209d39SAndroid Build Coastguard Worker public:
55*0e209d39SAndroid Build Coastguard Worker   /**
56*0e209d39SAndroid Build Coastguard Worker    * Default constructor.
57*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 4.2
58*0e209d39SAndroid Build Coastguard Worker    */
ByteSink()59*0e209d39SAndroid Build Coastguard Worker   ByteSink() { }
60*0e209d39SAndroid Build Coastguard Worker   /**
61*0e209d39SAndroid Build Coastguard Worker    * Virtual destructor.
62*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 4.2
63*0e209d39SAndroid Build Coastguard Worker    */
64*0e209d39SAndroid Build Coastguard Worker   virtual ~ByteSink();
65*0e209d39SAndroid Build Coastguard Worker 
66*0e209d39SAndroid Build Coastguard Worker   /**
67*0e209d39SAndroid Build Coastguard Worker    * Append "bytes[0,n-1]" to this.
68*0e209d39SAndroid Build Coastguard Worker    * @param bytes the pointer to the bytes
69*0e209d39SAndroid Build Coastguard Worker    * @param n the number of bytes; must be non-negative
70*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 4.2
71*0e209d39SAndroid Build Coastguard Worker    */
72*0e209d39SAndroid Build Coastguard Worker   virtual void Append(const char* bytes, int32_t n) = 0;
73*0e209d39SAndroid Build Coastguard Worker 
74*0e209d39SAndroid Build Coastguard Worker   /**
75*0e209d39SAndroid Build Coastguard Worker    * Appends n bytes to this. Same as Append().
76*0e209d39SAndroid Build Coastguard Worker    * Call AppendU8() with u8"string literals" which are const char * in C++11
77*0e209d39SAndroid Build Coastguard Worker    * but const char8_t * in C++20.
78*0e209d39SAndroid Build Coastguard Worker    * If the compiler does support char8_t as a distinct type,
79*0e209d39SAndroid Build Coastguard Worker    * then an AppendU8() overload for that is defined and will be chosen.
80*0e209d39SAndroid Build Coastguard Worker    *
81*0e209d39SAndroid Build Coastguard Worker    * @param bytes the pointer to the bytes
82*0e209d39SAndroid Build Coastguard Worker    * @param n the number of bytes; must be non-negative
83*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 67
84*0e209d39SAndroid Build Coastguard Worker    */
AppendU8(const char * bytes,int32_t n)85*0e209d39SAndroid Build Coastguard Worker   inline void AppendU8(const char* bytes, int32_t n) {
86*0e209d39SAndroid Build Coastguard Worker     Append(bytes, n);
87*0e209d39SAndroid Build Coastguard Worker   }
88*0e209d39SAndroid Build Coastguard Worker 
89*0e209d39SAndroid Build Coastguard Worker #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
90*0e209d39SAndroid Build Coastguard Worker   /**
91*0e209d39SAndroid Build Coastguard Worker    * Appends n bytes to this. Same as Append() but for a const char8_t * pointer.
92*0e209d39SAndroid Build Coastguard Worker    * Call AppendU8() with u8"string literals" which are const char * in C++11
93*0e209d39SAndroid Build Coastguard Worker    * but const char8_t * in C++20.
94*0e209d39SAndroid Build Coastguard Worker    * If the compiler does support char8_t as a distinct type,
95*0e209d39SAndroid Build Coastguard Worker    * then this AppendU8() overload for that is defined and will be chosen.
96*0e209d39SAndroid Build Coastguard Worker    *
97*0e209d39SAndroid Build Coastguard Worker    * @param bytes the pointer to the bytes
98*0e209d39SAndroid Build Coastguard Worker    * @param n the number of bytes; must be non-negative
99*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 67
100*0e209d39SAndroid Build Coastguard Worker    */
AppendU8(const char8_t * bytes,int32_t n)101*0e209d39SAndroid Build Coastguard Worker   inline void AppendU8(const char8_t* bytes, int32_t n) {
102*0e209d39SAndroid Build Coastguard Worker     Append(reinterpret_cast<const char*>(bytes), n);
103*0e209d39SAndroid Build Coastguard Worker   }
104*0e209d39SAndroid Build Coastguard Worker #endif
105*0e209d39SAndroid Build Coastguard Worker 
106*0e209d39SAndroid Build Coastguard Worker   /**
107*0e209d39SAndroid Build Coastguard Worker    * Returns a writable buffer for appending and writes the buffer's capacity to
108*0e209d39SAndroid Build Coastguard Worker    * *result_capacity. Guarantees *result_capacity>=min_capacity.
109*0e209d39SAndroid Build Coastguard Worker    * May return a pointer to the caller-owned scratch buffer which must have
110*0e209d39SAndroid Build Coastguard Worker    * scratch_capacity>=min_capacity.
111*0e209d39SAndroid Build Coastguard Worker    * The returned buffer is only valid until the next operation
112*0e209d39SAndroid Build Coastguard Worker    * on this ByteSink.
113*0e209d39SAndroid Build Coastguard Worker    *
114*0e209d39SAndroid Build Coastguard Worker    * After writing at most *result_capacity bytes, call Append() with the
115*0e209d39SAndroid Build Coastguard Worker    * pointer returned from this function and the number of bytes written.
116*0e209d39SAndroid Build Coastguard Worker    * Many Append() implementations will avoid copying bytes if this function
117*0e209d39SAndroid Build Coastguard Worker    * returned an internal buffer.
118*0e209d39SAndroid Build Coastguard Worker    *
119*0e209d39SAndroid Build Coastguard Worker    * Partial usage example:
120*0e209d39SAndroid Build Coastguard Worker    *  int32_t capacity;
121*0e209d39SAndroid Build Coastguard Worker    *  char* buffer = sink->GetAppendBuffer(..., &capacity);
122*0e209d39SAndroid Build Coastguard Worker    *  ... Write n bytes into buffer, with n <= capacity.
123*0e209d39SAndroid Build Coastguard Worker    *  sink->Append(buffer, n);
124*0e209d39SAndroid Build Coastguard Worker    * In many implementations, that call to Append will avoid copying bytes.
125*0e209d39SAndroid Build Coastguard Worker    *
126*0e209d39SAndroid Build Coastguard Worker    * If the ByteSink allocates or reallocates an internal buffer, it should use
127*0e209d39SAndroid Build Coastguard Worker    * the desired_capacity_hint if appropriate.
128*0e209d39SAndroid Build Coastguard Worker    * If a caller cannot provide a reasonable guess at the desired capacity,
129*0e209d39SAndroid Build Coastguard Worker    * it should pass desired_capacity_hint=0.
130*0e209d39SAndroid Build Coastguard Worker    *
131*0e209d39SAndroid Build Coastguard Worker    * If a non-scratch buffer is returned, the caller may only pass
132*0e209d39SAndroid Build Coastguard Worker    * a prefix to it to Append().
133*0e209d39SAndroid Build Coastguard Worker    * That is, it is not correct to pass an interior pointer to Append().
134*0e209d39SAndroid Build Coastguard Worker    *
135*0e209d39SAndroid Build Coastguard Worker    * The default implementation always returns the scratch buffer.
136*0e209d39SAndroid Build Coastguard Worker    *
137*0e209d39SAndroid Build Coastguard Worker    * @param min_capacity required minimum capacity of the returned buffer;
138*0e209d39SAndroid Build Coastguard Worker    *                     must be non-negative
139*0e209d39SAndroid Build Coastguard Worker    * @param desired_capacity_hint desired capacity of the returned buffer;
140*0e209d39SAndroid Build Coastguard Worker    *                              must be non-negative
141*0e209d39SAndroid Build Coastguard Worker    * @param scratch default caller-owned buffer
142*0e209d39SAndroid Build Coastguard Worker    * @param scratch_capacity capacity of the scratch buffer
143*0e209d39SAndroid Build Coastguard Worker    * @param result_capacity pointer to an integer which will be set to the
144*0e209d39SAndroid Build Coastguard Worker    *                        capacity of the returned buffer
145*0e209d39SAndroid Build Coastguard Worker    * @return a buffer with *result_capacity>=min_capacity
146*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 4.2
147*0e209d39SAndroid Build Coastguard Worker    */
148*0e209d39SAndroid Build Coastguard Worker   virtual char* GetAppendBuffer(int32_t min_capacity,
149*0e209d39SAndroid Build Coastguard Worker                                 int32_t desired_capacity_hint,
150*0e209d39SAndroid Build Coastguard Worker                                 char* scratch, int32_t scratch_capacity,
151*0e209d39SAndroid Build Coastguard Worker                                 int32_t* result_capacity);
152*0e209d39SAndroid Build Coastguard Worker 
153*0e209d39SAndroid Build Coastguard Worker   /**
154*0e209d39SAndroid Build Coastguard Worker    * Flush internal buffers.
155*0e209d39SAndroid Build Coastguard Worker    * Some byte sinks use internal buffers or provide buffering
156*0e209d39SAndroid Build Coastguard Worker    * and require calling Flush() at the end of the stream.
157*0e209d39SAndroid Build Coastguard Worker    * The ByteSink should be ready for further Append() calls after Flush().
158*0e209d39SAndroid Build Coastguard Worker    * The default implementation of Flush() does nothing.
159*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 4.2
160*0e209d39SAndroid Build Coastguard Worker    */
161*0e209d39SAndroid Build Coastguard Worker   virtual void Flush();
162*0e209d39SAndroid Build Coastguard Worker 
163*0e209d39SAndroid Build Coastguard Worker private:
164*0e209d39SAndroid Build Coastguard Worker   ByteSink(const ByteSink &) = delete;
165*0e209d39SAndroid Build Coastguard Worker   ByteSink &operator=(const ByteSink &) = delete;
166*0e209d39SAndroid Build Coastguard Worker };
167*0e209d39SAndroid Build Coastguard Worker 
168*0e209d39SAndroid Build Coastguard Worker // -------------------------------------------------------------
169*0e209d39SAndroid Build Coastguard Worker // Some standard implementations
170*0e209d39SAndroid Build Coastguard Worker 
171*0e209d39SAndroid Build Coastguard Worker /**
172*0e209d39SAndroid Build Coastguard Worker  * Implementation of ByteSink that writes to a flat byte array,
173*0e209d39SAndroid Build Coastguard Worker  * with bounds-checking:
174*0e209d39SAndroid Build Coastguard Worker  * This sink will not write more than capacity bytes to outbuf.
175*0e209d39SAndroid Build Coastguard Worker  * If more than capacity bytes are Append()ed, then excess bytes are ignored,
176*0e209d39SAndroid Build Coastguard Worker  * and Overflowed() will return true.
177*0e209d39SAndroid Build Coastguard Worker  * Overflow does not cause a runtime error.
178*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 4.2
179*0e209d39SAndroid Build Coastguard Worker  */
180*0e209d39SAndroid Build Coastguard Worker class U_COMMON_API CheckedArrayByteSink : public ByteSink {
181*0e209d39SAndroid Build Coastguard Worker public:
182*0e209d39SAndroid Build Coastguard Worker   /**
183*0e209d39SAndroid Build Coastguard Worker    * Constructs a ByteSink that will write to outbuf[0..capacity-1].
184*0e209d39SAndroid Build Coastguard Worker    * @param outbuf buffer to write to
185*0e209d39SAndroid Build Coastguard Worker    * @param capacity size of the buffer
186*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 4.2
187*0e209d39SAndroid Build Coastguard Worker    */
188*0e209d39SAndroid Build Coastguard Worker   CheckedArrayByteSink(char* outbuf, int32_t capacity);
189*0e209d39SAndroid Build Coastguard Worker   /**
190*0e209d39SAndroid Build Coastguard Worker    * Destructor.
191*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 4.2
192*0e209d39SAndroid Build Coastguard Worker    */
193*0e209d39SAndroid Build Coastguard Worker   virtual ~CheckedArrayByteSink();
194*0e209d39SAndroid Build Coastguard Worker   /**
195*0e209d39SAndroid Build Coastguard Worker    * Returns the sink to its original state, without modifying the buffer.
196*0e209d39SAndroid Build Coastguard Worker    * Useful for reusing both the buffer and the sink for multiple streams.
197*0e209d39SAndroid Build Coastguard Worker    * Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0
198*0e209d39SAndroid Build Coastguard Worker    * and Overflowed()=false.
199*0e209d39SAndroid Build Coastguard Worker    * @return *this
200*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 4.6
201*0e209d39SAndroid Build Coastguard Worker    */
202*0e209d39SAndroid Build Coastguard Worker   virtual CheckedArrayByteSink& Reset();
203*0e209d39SAndroid Build Coastguard Worker   /**
204*0e209d39SAndroid Build Coastguard Worker    * Append "bytes[0,n-1]" to this.
205*0e209d39SAndroid Build Coastguard Worker    * @param bytes the pointer to the bytes
206*0e209d39SAndroid Build Coastguard Worker    * @param n the number of bytes; must be non-negative
207*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 4.2
208*0e209d39SAndroid Build Coastguard Worker    */
209*0e209d39SAndroid Build Coastguard Worker   virtual void Append(const char* bytes, int32_t n) override;
210*0e209d39SAndroid Build Coastguard Worker   /**
211*0e209d39SAndroid Build Coastguard Worker    * Returns a writable buffer for appending and writes the buffer's capacity to
212*0e209d39SAndroid Build Coastguard Worker    * *result_capacity. For details see the base class documentation.
213*0e209d39SAndroid Build Coastguard Worker    * @param min_capacity required minimum capacity of the returned buffer;
214*0e209d39SAndroid Build Coastguard Worker    *                     must be non-negative
215*0e209d39SAndroid Build Coastguard Worker    * @param desired_capacity_hint desired capacity of the returned buffer;
216*0e209d39SAndroid Build Coastguard Worker    *                              must be non-negative
217*0e209d39SAndroid Build Coastguard Worker    * @param scratch default caller-owned buffer
218*0e209d39SAndroid Build Coastguard Worker    * @param scratch_capacity capacity of the scratch buffer
219*0e209d39SAndroid Build Coastguard Worker    * @param result_capacity pointer to an integer which will be set to the
220*0e209d39SAndroid Build Coastguard Worker    *                        capacity of the returned buffer
221*0e209d39SAndroid Build Coastguard Worker    * @return a buffer with *result_capacity>=min_capacity
222*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 4.2
223*0e209d39SAndroid Build Coastguard Worker    */
224*0e209d39SAndroid Build Coastguard Worker   virtual char* GetAppendBuffer(int32_t min_capacity,
225*0e209d39SAndroid Build Coastguard Worker                                 int32_t desired_capacity_hint,
226*0e209d39SAndroid Build Coastguard Worker                                 char* scratch, int32_t scratch_capacity,
227*0e209d39SAndroid Build Coastguard Worker                                 int32_t* result_capacity) override;
228*0e209d39SAndroid Build Coastguard Worker   /**
229*0e209d39SAndroid Build Coastguard Worker    * Returns the number of bytes actually written to the sink.
230*0e209d39SAndroid Build Coastguard Worker    * @return number of bytes written to the buffer
231*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 4.2
232*0e209d39SAndroid Build Coastguard Worker    */
NumberOfBytesWritten()233*0e209d39SAndroid Build Coastguard Worker   int32_t NumberOfBytesWritten() const { return size_; }
234*0e209d39SAndroid Build Coastguard Worker   /**
235*0e209d39SAndroid Build Coastguard Worker    * Returns true if any bytes were discarded, i.e., if there was an
236*0e209d39SAndroid Build Coastguard Worker    * attempt to write more than 'capacity' bytes.
237*0e209d39SAndroid Build Coastguard Worker    * @return true if more than 'capacity' bytes were Append()ed
238*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 4.2
239*0e209d39SAndroid Build Coastguard Worker    */
Overflowed()240*0e209d39SAndroid Build Coastguard Worker   UBool Overflowed() const { return overflowed_; }
241*0e209d39SAndroid Build Coastguard Worker   /**
242*0e209d39SAndroid Build Coastguard Worker    * Returns the number of bytes appended to the sink.
243*0e209d39SAndroid Build Coastguard Worker    * If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten()
244*0e209d39SAndroid Build Coastguard Worker    * else they return the same number.
245*0e209d39SAndroid Build Coastguard Worker    * @return number of bytes written to the buffer
246*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 4.6
247*0e209d39SAndroid Build Coastguard Worker    */
NumberOfBytesAppended()248*0e209d39SAndroid Build Coastguard Worker   int32_t NumberOfBytesAppended() const { return appended_; }
249*0e209d39SAndroid Build Coastguard Worker private:
250*0e209d39SAndroid Build Coastguard Worker   char* outbuf_;
251*0e209d39SAndroid Build Coastguard Worker   const int32_t capacity_;
252*0e209d39SAndroid Build Coastguard Worker   int32_t size_;
253*0e209d39SAndroid Build Coastguard Worker   int32_t appended_;
254*0e209d39SAndroid Build Coastguard Worker   UBool overflowed_;
255*0e209d39SAndroid Build Coastguard Worker 
256*0e209d39SAndroid Build Coastguard Worker   CheckedArrayByteSink() = delete;
257*0e209d39SAndroid Build Coastguard Worker   CheckedArrayByteSink(const CheckedArrayByteSink &) = delete;
258*0e209d39SAndroid Build Coastguard Worker   CheckedArrayByteSink &operator=(const CheckedArrayByteSink &) = delete;
259*0e209d39SAndroid Build Coastguard Worker };
260*0e209d39SAndroid Build Coastguard Worker 
261*0e209d39SAndroid Build Coastguard Worker /**
262*0e209d39SAndroid Build Coastguard Worker  * Implementation of ByteSink that writes to a "string".
263*0e209d39SAndroid Build Coastguard Worker  * The StringClass is usually instantiated with a std::string.
264*0e209d39SAndroid Build Coastguard Worker  * @stable ICU 4.2
265*0e209d39SAndroid Build Coastguard Worker  */
266*0e209d39SAndroid Build Coastguard Worker template<typename StringClass>
267*0e209d39SAndroid Build Coastguard Worker class StringByteSink : public ByteSink {
268*0e209d39SAndroid Build Coastguard Worker  public:
269*0e209d39SAndroid Build Coastguard Worker   /**
270*0e209d39SAndroid Build Coastguard Worker    * Constructs a ByteSink that will append bytes to the dest string.
271*0e209d39SAndroid Build Coastguard Worker    * @param dest pointer to string object to append to
272*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 4.2
273*0e209d39SAndroid Build Coastguard Worker    */
StringByteSink(StringClass * dest)274*0e209d39SAndroid Build Coastguard Worker   StringByteSink(StringClass* dest) : dest_(dest) { }
275*0e209d39SAndroid Build Coastguard Worker   /**
276*0e209d39SAndroid Build Coastguard Worker    * Constructs a ByteSink that reserves append capacity and will append bytes to the dest string.
277*0e209d39SAndroid Build Coastguard Worker    *
278*0e209d39SAndroid Build Coastguard Worker    * @param dest pointer to string object to append to
279*0e209d39SAndroid Build Coastguard Worker    * @param initialAppendCapacity capacity beyond dest->length() to be reserve()d
280*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 60
281*0e209d39SAndroid Build Coastguard Worker    */
StringByteSink(StringClass * dest,int32_t initialAppendCapacity)282*0e209d39SAndroid Build Coastguard Worker   StringByteSink(StringClass* dest, int32_t initialAppendCapacity) : dest_(dest) {
283*0e209d39SAndroid Build Coastguard Worker     if (initialAppendCapacity > 0 &&
284*0e209d39SAndroid Build Coastguard Worker         (uint32_t)initialAppendCapacity > (dest->capacity() - dest->length())) {
285*0e209d39SAndroid Build Coastguard Worker       dest->reserve(dest->length() + initialAppendCapacity);
286*0e209d39SAndroid Build Coastguard Worker     }
287*0e209d39SAndroid Build Coastguard Worker   }
288*0e209d39SAndroid Build Coastguard Worker   /**
289*0e209d39SAndroid Build Coastguard Worker    * Append "bytes[0,n-1]" to this.
290*0e209d39SAndroid Build Coastguard Worker    * @param data the pointer to the bytes
291*0e209d39SAndroid Build Coastguard Worker    * @param n the number of bytes; must be non-negative
292*0e209d39SAndroid Build Coastguard Worker    * @stable ICU 4.2
293*0e209d39SAndroid Build Coastguard Worker    */
Append(const char * data,int32_t n)294*0e209d39SAndroid Build Coastguard Worker   virtual void Append(const char* data, int32_t n) override { dest_->append(data, n); }
295*0e209d39SAndroid Build Coastguard Worker  private:
296*0e209d39SAndroid Build Coastguard Worker   StringClass* dest_;
297*0e209d39SAndroid Build Coastguard Worker 
298*0e209d39SAndroid Build Coastguard Worker   StringByteSink() = delete;
299*0e209d39SAndroid Build Coastguard Worker   StringByteSink(const StringByteSink &) = delete;
300*0e209d39SAndroid Build Coastguard Worker   StringByteSink &operator=(const StringByteSink &) = delete;
301*0e209d39SAndroid Build Coastguard Worker };
302*0e209d39SAndroid Build Coastguard Worker 
303*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END
304*0e209d39SAndroid Build Coastguard Worker 
305*0e209d39SAndroid Build Coastguard Worker #endif /* U_SHOW_CPLUSPLUS_API */
306*0e209d39SAndroid Build Coastguard Worker 
307*0e209d39SAndroid Build Coastguard Worker #endif  // __BYTESTREAM_H__
308