183a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius// Copyright (C) 2009-2012, International Business Machines
2b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// Corporation and others. All Rights Reserved.
3b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru//
4b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// Copyright 2007 Google Inc. All Rights Reserved.
5b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// Author: sanjay@google.com (Sanjay Ghemawat)
6b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru//
7b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// Abstract interface that consumes a sequence of bytes (ByteSink).
8b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru//
9b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// Used so that we can write a single piece of code that can operate
10b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// on a variety of output string types.
11b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru//
12b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// Various implementations of this interface are provided:
13b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru//   ByteSink:
14b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru//      CheckedArrayByteSink    Write to a flat array, with bounds checking
15b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru//      StringByteSink          Write to an STL string
16b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru
17b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// This code is a contribution of Google code, and the style used here is
18b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// a compromise between the original Google code and the ICU coding guidelines.
19b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// For example, data types are ICU-ified (size_t,int->int32_t),
20b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// and API comments doxygen-ified, but function names and behavior are
21b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// as in the original, if possible.
22b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// Assertion-style error handling, not available in ICU, was changed to
23b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// parameter "pinning" similar to UnicodeString.
24b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru//
25b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// In addition, this is only a partial port of the original Google code,
26b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// limited to what was needed so far. The (nearly) complete original code
27b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
28b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// (see ICU ticket 6765, r25517).
29b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru
30b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru#ifndef __BYTESTREAM_H__
31b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru#define __BYTESTREAM_H__
32b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru
33b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru/**
3427f654740f2a26ad62a5c155af9199af9e69b889claireho * \file
35b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru * \brief C++ API: Interface for writing bytes, and implementation classes.
36b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru */
37b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru
38b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru#include "unicode/utypes.h"
39b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru#include "unicode/uobject.h"
40b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru#include "unicode/std_string.h"
41b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru
42b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste QueruU_NAMESPACE_BEGIN
43b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru
4427f654740f2a26ad62a5c155af9199af9e69b889claireho/**
45b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru * A ByteSink can be filled with bytes.
4650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho * @stable ICU 4.2
47b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru */
48b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queruclass U_COMMON_API ByteSink : public UMemory {
49b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Querupublic:
50b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  /**
51b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * Default constructor.
5250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho   * @stable ICU 4.2
53b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   */
54b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  ByteSink() { }
55b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  /**
56b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * Virtual destructor.
5750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho   * @stable ICU 4.2
5827f654740f2a26ad62a5c155af9199af9e69b889claireho   */
5983a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius  virtual ~ByteSink();
60b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru
61b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  /**
62b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * Append "bytes[0,n-1]" to this.
63b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param bytes the pointer to the bytes
64b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param n the number of bytes; must be non-negative
6550294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho   * @stable ICU 4.2
66b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   */
67b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  virtual void Append(const char* bytes, int32_t n) = 0;
6827f654740f2a26ad62a5c155af9199af9e69b889claireho
69b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  /**
70b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * Returns a writable buffer for appending and writes the buffer's capacity to
71b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * *result_capacity. Guarantees *result_capacity>=min_capacity.
72b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * May return a pointer to the caller-owned scratch buffer which must have
73b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * scratch_capacity>=min_capacity.
74b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * The returned buffer is only valid until the next operation
75b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * on this ByteSink.
76b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   *
77b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * After writing at most *result_capacity bytes, call Append() with the
78b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * pointer returned from this function and the number of bytes written.
79b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * Many Append() implementations will avoid copying bytes if this function
80b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * returned an internal buffer.
81b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   *
82b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * Partial usage example:
83b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   *  int32_t capacity;
84b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   *  char* buffer = sink->GetAppendBuffer(..., &capacity);
85b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   *  ... Write n bytes into buffer, with n <= capacity.
86b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   *  sink->Append(buffer, n);
87b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * In many implementations, that call to Append will avoid copying bytes.
88b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   *
89b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * If the ByteSink allocates or reallocates an internal buffer, it should use
90b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * the desired_capacity_hint if appropriate.
91b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * If a caller cannot provide a reasonable guess at the desired capacity,
92b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * it should pass desired_capacity_hint=0.
93b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   *
94b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * If a non-scratch buffer is returned, the caller may only pass
95b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * a prefix to it to Append().
96b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * That is, it is not correct to pass an interior pointer to Append().
97b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   *
98b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * The default implementation always returns the scratch buffer.
99b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   *
100b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param min_capacity required minimum capacity of the returned buffer;
101b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   *                     must be non-negative
102b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param desired_capacity_hint desired capacity of the returned buffer;
103b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   *                              must be non-negative
104b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param scratch default caller-owned buffer
105b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param scratch_capacity capacity of the scratch buffer
106b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param result_capacity pointer to an integer which will be set to the
107b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   *                        capacity of the returned buffer
108b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @return a buffer with *result_capacity>=min_capacity
10950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho   * @stable ICU 4.2
110b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   */
111b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  virtual char* GetAppendBuffer(int32_t min_capacity,
112b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru                                int32_t desired_capacity_hint,
113b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru                                char* scratch, int32_t scratch_capacity,
114b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru                                int32_t* result_capacity);
115b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru
116b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  /**
117b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * Flush internal buffers.
118b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * Some byte sinks use internal buffers or provide buffering
119b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * and require calling Flush() at the end of the stream.
12027f654740f2a26ad62a5c155af9199af9e69b889claireho   * The ByteSink should be ready for further Append() calls after Flush().
121b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * The default implementation of Flush() does nothing.
12250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho   * @stable ICU 4.2
123b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   */
124b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  virtual void Flush();
125b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru
126b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queruprivate:
127b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  ByteSink(const ByteSink &); // copy constructor not implemented
128b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  ByteSink &operator=(const ByteSink &); // assignment operator not implemented
129b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru};
130b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru
131b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// -------------------------------------------------------------
132b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru// Some standard implementations
133b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru
134b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru/**
135b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru * Implementation of ByteSink that writes to a flat byte array,
136b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru * with bounds-checking:
137b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru * This sink will not write more than capacity bytes to outbuf.
138b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru * If more than capacity bytes are Append()ed, then excess bytes are ignored,
139b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru * and Overflowed() will return true.
140b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru * Overflow does not cause a runtime error.
14150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho * @stable ICU 4.2
142b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru */
143b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queruclass U_COMMON_API CheckedArrayByteSink : public ByteSink {
144b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Querupublic:
145b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  /**
146b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * Constructs a ByteSink that will write to outbuf[0..capacity-1].
147b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param outbuf buffer to write to
148b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param capacity size of the buffer
14950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho   * @stable ICU 4.2
150b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   */
151b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  CheckedArrayByteSink(char* outbuf, int32_t capacity);
152b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  /**
15383a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius   * Destructor.
15483a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius   * @stable ICU 4.2
15583a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius   */
15683a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius  virtual ~CheckedArrayByteSink();
15783a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius  /**
15827f654740f2a26ad62a5c155af9199af9e69b889claireho   * Returns the sink to its original state, without modifying the buffer.
15927f654740f2a26ad62a5c155af9199af9e69b889claireho   * Useful for reusing both the buffer and the sink for multiple streams.
16027f654740f2a26ad62a5c155af9199af9e69b889claireho   * Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0
16127f654740f2a26ad62a5c155af9199af9e69b889claireho   * and Overflowed()=FALSE.
16227f654740f2a26ad62a5c155af9199af9e69b889claireho   * @return *this
16383a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius   * @stable ICU 4.6
16427f654740f2a26ad62a5c155af9199af9e69b889claireho   */
16527f654740f2a26ad62a5c155af9199af9e69b889claireho  virtual CheckedArrayByteSink& Reset();
16627f654740f2a26ad62a5c155af9199af9e69b889claireho  /**
167b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * Append "bytes[0,n-1]" to this.
168b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param bytes the pointer to the bytes
169b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param n the number of bytes; must be non-negative
17050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho   * @stable ICU 4.2
171b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   */
172b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  virtual void Append(const char* bytes, int32_t n);
173b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  /**
174b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * Returns a writable buffer for appending and writes the buffer's capacity to
175b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * *result_capacity. For details see the base class documentation.
176b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param min_capacity required minimum capacity of the returned buffer;
177b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   *                     must be non-negative
178b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param desired_capacity_hint desired capacity of the returned buffer;
179b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   *                              must be non-negative
180b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param scratch default caller-owned buffer
181b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param scratch_capacity capacity of the scratch buffer
182b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param result_capacity pointer to an integer which will be set to the
183b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   *                        capacity of the returned buffer
184b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @return a buffer with *result_capacity>=min_capacity
18550294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho   * @stable ICU 4.2
186b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   */
187b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  virtual char* GetAppendBuffer(int32_t min_capacity,
188b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru                                int32_t desired_capacity_hint,
189b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru                                char* scratch, int32_t scratch_capacity,
190b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru                                int32_t* result_capacity);
191b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  /**
192b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * Returns the number of bytes actually written to the sink.
193b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @return number of bytes written to the buffer
19450294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho   * @stable ICU 4.2
195b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   */
196b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  int32_t NumberOfBytesWritten() const { return size_; }
197b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  /**
198b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * Returns true if any bytes were discarded, i.e., if there was an
199b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * attempt to write more than 'capacity' bytes.
200b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @return TRUE if more than 'capacity' bytes were Append()ed
20150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho   * @stable ICU 4.2
202b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   */
203b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  UBool Overflowed() const { return overflowed_; }
20427f654740f2a26ad62a5c155af9199af9e69b889claireho  /**
20527f654740f2a26ad62a5c155af9199af9e69b889claireho   * Returns the number of bytes appended to the sink.
20627f654740f2a26ad62a5c155af9199af9e69b889claireho   * If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten()
20727f654740f2a26ad62a5c155af9199af9e69b889claireho   * else they return the same number.
20827f654740f2a26ad62a5c155af9199af9e69b889claireho   * @return number of bytes written to the buffer
20983a171d1a62abf406f7f44ae671823d5ec20db7dCraig Cornelius   * @stable ICU 4.6
21027f654740f2a26ad62a5c155af9199af9e69b889claireho   */
21127f654740f2a26ad62a5c155af9199af9e69b889claireho  int32_t NumberOfBytesAppended() const { return appended_; }
212b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queruprivate:
213b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  char* outbuf_;
214b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  const int32_t capacity_;
215b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  int32_t size_;
21627f654740f2a26ad62a5c155af9199af9e69b889claireho  int32_t appended_;
21727f654740f2a26ad62a5c155af9199af9e69b889claireho  UBool overflowed_;
218b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  CheckedArrayByteSink(); ///< default constructor not implemented
219b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  CheckedArrayByteSink(const CheckedArrayByteSink &); ///< copy constructor not implemented
220b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  CheckedArrayByteSink &operator=(const CheckedArrayByteSink &); ///< assignment operator not implemented
221b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru};
222b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru
223b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru#if U_HAVE_STD_STRING
224b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru
225b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru/**
226b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru * Implementation of ByteSink that writes to a "string".
227b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru * The StringClass is usually instantiated with a std::string.
22850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho * @stable ICU 4.2
229b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru */
230b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Querutemplate<typename StringClass>
231b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queruclass StringByteSink : public ByteSink {
232b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru public:
233b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  /**
234b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * Constructs a ByteSink that will append bytes to the dest string.
235b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param dest pointer to string object to append to
23650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho   * @stable ICU 4.2
237b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   */
238b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  StringByteSink(StringClass* dest) : dest_(dest) { }
239b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  /**
240b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * Append "bytes[0,n-1]" to this.
24150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho   * @param data the pointer to the bytes
242b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   * @param n the number of bytes; must be non-negative
24350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho   * @stable ICU 4.2
244b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru   */
245b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }
246b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru private:
247b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  StringClass* dest_;
248b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  StringByteSink(); ///< default constructor not implemented
249b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  StringByteSink(const StringByteSink &); ///< copy constructor not implemented
250b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru  StringByteSink &operator=(const StringByteSink &); ///< assignment operator not implemented
251b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru};
252b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru
253b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru#endif
254b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru
255b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste QueruU_NAMESPACE_END
256b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru
257b0ac937921a2c196d8b9da665135bf6ba01a1ccfJean-Baptiste Queru#endif  // __BYTESTREAM_H__
258