1 // Copyright (C) 2009-2012, International Business Machines
2 // Corporation and others. All Rights Reserved.
4 // Copyright 2007 Google Inc. All Rights Reserved.
5 // Author: sanjay@google.com (Sanjay Ghemawat)
7 // Abstract interface that consumes a sequence of bytes (ByteSink).
9 // Used so that we can write a single piece of code that can operate
10 // on a variety of output string types.
12 // Various implementations of this interface are provided:
14 // CheckedArrayByteSink Write to a flat array, with bounds checking
15 // StringByteSink Write to an STL string
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.
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).
30 #ifndef __BYTESTREAM_H__
31 #define __BYTESTREAM_H__
35 * \brief C++ API: Interface for writing bytes, and implementation classes.
38 #include "unicode/utypes.h"
39 #include "unicode/uobject.h"
40 #include "unicode/std_string.h"
45 * A ByteSink can be filled with bytes.
48 class U_COMMON_API ByteSink
: public UMemory
{
51 * Default constructor.
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
67 virtual void Append(const char* bytes
, int32_t n
) = 0;
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
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.
82 * Partial usage example:
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.
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.
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().
98 * The default implementation always returns the scratch buffer.
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
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
);
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 ByteSink should be ready for further Append() calls after Flush().
121 * The default implementation of Flush() does nothing.
124 virtual void Flush();
127 ByteSink(const ByteSink
&); // copy constructor not implemented
128 ByteSink
&operator=(const ByteSink
&); // assignment operator not implemented
131 // -------------------------------------------------------------
132 // Some standard implementations
135 * Implementation of ByteSink that writes to a flat byte array,
136 * with bounds-checking:
137 * This sink will not write more than capacity bytes to outbuf.
138 * If more than capacity bytes are Append()ed, then excess bytes are ignored,
139 * and Overflowed() will return true.
140 * Overflow does not cause a runtime error.
143 class U_COMMON_API CheckedArrayByteSink
: public ByteSink
{
146 * Constructs a ByteSink that will write to outbuf[0..capacity-1].
147 * @param outbuf buffer to write to
148 * @param capacity size of the buffer
151 CheckedArrayByteSink(char* outbuf
, int32_t capacity
);
156 virtual ~CheckedArrayByteSink();
158 * Returns the sink to its original state, without modifying the buffer.
159 * Useful for reusing both the buffer and the sink for multiple streams.
160 * Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0
161 * and Overflowed()=FALSE.
165 virtual CheckedArrayByteSink
& Reset();
167 * Append "bytes[0,n-1]" to this.
168 * @param bytes the pointer to the bytes
169 * @param n the number of bytes; must be non-negative
172 virtual void Append(const char* bytes
, int32_t n
);
174 * Returns a writable buffer for appending and writes the buffer's capacity to
175 * *result_capacity. For details see the base class documentation.
176 * @param min_capacity required minimum capacity of the returned buffer;
177 * must be non-negative
178 * @param desired_capacity_hint desired capacity of the returned buffer;
179 * must be non-negative
180 * @param scratch default caller-owned buffer
181 * @param scratch_capacity capacity of the scratch buffer
182 * @param result_capacity pointer to an integer which will be set to the
183 * capacity of the returned buffer
184 * @return a buffer with *result_capacity>=min_capacity
187 virtual char* GetAppendBuffer(int32_t min_capacity
,
188 int32_t desired_capacity_hint
,
189 char* scratch
, int32_t scratch_capacity
,
190 int32_t* result_capacity
);
192 * Returns the number of bytes actually written to the sink.
193 * @return number of bytes written to the buffer
196 int32_t NumberOfBytesWritten() const { return size_
; }
198 * Returns true if any bytes were discarded, i.e., if there was an
199 * attempt to write more than 'capacity' bytes.
200 * @return TRUE if more than 'capacity' bytes were Append()ed
203 UBool
Overflowed() const { return overflowed_
; }
205 * Returns the number of bytes appended to the sink.
206 * If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten()
207 * else they return the same number.
208 * @return number of bytes written to the buffer
211 int32_t NumberOfBytesAppended() const { return appended_
; }
214 const int32_t capacity_
;
218 CheckedArrayByteSink(); ///< default constructor not implemented
219 CheckedArrayByteSink(const CheckedArrayByteSink
&); ///< copy constructor not implemented
220 CheckedArrayByteSink
&operator=(const CheckedArrayByteSink
&); ///< assignment operator not implemented
223 #if U_HAVE_STD_STRING
226 * Implementation of ByteSink that writes to a "string".
227 * The StringClass is usually instantiated with a std::string.
230 template<typename StringClass
>
231 class StringByteSink
: public ByteSink
{
234 * Constructs a ByteSink that will append bytes to the dest string.
235 * @param dest pointer to string object to append to
238 StringByteSink(StringClass
* dest
) : dest_(dest
) { }
240 * Append "bytes[0,n-1]" to this.
241 * @param data the pointer to the bytes
242 * @param n the number of bytes; must be non-negative
245 virtual void Append(const char* data
, int32_t n
) { dest_
->append(data
, n
); }
248 StringByteSink(); ///< default constructor not implemented
249 StringByteSink(const StringByteSink
&); ///< copy constructor not implemented
250 StringByteSink
&operator=(const StringByteSink
&); ///< assignment operator not implemented
257 #endif // __BYTESTREAM_H__