16f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// Copyright (C) 2009-2012, International Business Machines
26f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// Corporation and others. All Rights Reserved.
36f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org//
46f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// Copyright 2007 Google Inc. All Rights Reserved.
56f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// Author: sanjay@google.com (Sanjay Ghemawat)
66f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org//
76f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// Abstract interface that consumes a sequence of bytes (ByteSink).
86f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org//
96f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// Used so that we can write a single piece of code that can operate
106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// on a variety of output string types.
116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org//
126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// Various implementations of this interface are provided:
136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org//   ByteSink:
146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org//      CheckedArrayByteSink    Write to a flat array, with bounds checking
156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org//      StringByteSink          Write to an STL string
166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// This code is a contribution of Google code, and the style used here is
186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// a compromise between the original Google code and the ICU coding guidelines.
196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// For example, data types are ICU-ified (size_t,int->int32_t),
206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// and API comments doxygen-ified, but function names and behavior are
216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// as in the original, if possible.
226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// Assertion-style error handling, not available in ICU, was changed to
236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// parameter "pinning" similar to UnicodeString.
246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org//
256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// In addition, this is only a partial port of the original Google code,
266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// limited to what was needed so far. The (nearly) complete original code
276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// (see ICU ticket 6765, r25517).
296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifndef __BYTESTREAM_H__
316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define __BYTESTREAM_H__
326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/**
346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * \file
356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * \brief C++ API: Interface for writing bytes, and implementation classes.
366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "unicode/utypes.h"
396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "unicode/uobject.h"
406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "unicode/std_string.h"
416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_NAMESPACE_BEGIN
436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/**
456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * A ByteSink can be filled with bytes.
466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * @stable ICU 4.2
476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgclass U_COMMON_API ByteSink : public UMemory {
496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgpublic:
506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  /**
516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Default constructor.
526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @stable ICU 4.2
536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   */
546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  ByteSink() { }
556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  /**
566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Virtual destructor.
576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @stable ICU 4.2
586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   */
596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  virtual ~ByteSink();
606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  /**
626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Append "bytes[0,n-1]" to this.
636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param bytes the pointer to the bytes
646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param n the number of bytes; must be non-negative
656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @stable ICU 4.2
666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   */
676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  virtual void Append(const char* bytes, int32_t n) = 0;
686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  /**
706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Returns a writable buffer for appending and writes the buffer's capacity to
716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * *result_capacity. Guarantees *result_capacity>=min_capacity.
726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * May return a pointer to the caller-owned scratch buffer which must have
736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * scratch_capacity>=min_capacity.
746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * The returned buffer is only valid until the next operation
756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * on this ByteSink.
766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   *
776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * After writing at most *result_capacity bytes, call Append() with the
786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * pointer returned from this function and the number of bytes written.
796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Many Append() implementations will avoid copying bytes if this function
806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * returned an internal buffer.
816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   *
826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Partial usage example:
836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   *  int32_t capacity;
846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   *  char* buffer = sink->GetAppendBuffer(..., &capacity);
856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   *  ... Write n bytes into buffer, with n <= capacity.
866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   *  sink->Append(buffer, n);
876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * In many implementations, that call to Append will avoid copying bytes.
886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   *
896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * If the ByteSink allocates or reallocates an internal buffer, it should use
906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * the desired_capacity_hint if appropriate.
916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * If a caller cannot provide a reasonable guess at the desired capacity,
926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * it should pass desired_capacity_hint=0.
936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   *
946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * If a non-scratch buffer is returned, the caller may only pass
956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * a prefix to it to Append().
966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * That is, it is not correct to pass an interior pointer to Append().
976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   *
986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * The default implementation always returns the scratch buffer.
996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   *
1006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param min_capacity required minimum capacity of the returned buffer;
1016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   *                     must be non-negative
1026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param desired_capacity_hint desired capacity of the returned buffer;
1036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   *                              must be non-negative
1046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param scratch default caller-owned buffer
1056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param scratch_capacity capacity of the scratch buffer
1066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param result_capacity pointer to an integer which will be set to the
1076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   *                        capacity of the returned buffer
1086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @return a buffer with *result_capacity>=min_capacity
1096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @stable ICU 4.2
1106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   */
1116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  virtual char* GetAppendBuffer(int32_t min_capacity,
1126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                                int32_t desired_capacity_hint,
1136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                                char* scratch, int32_t scratch_capacity,
1146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                                int32_t* result_capacity);
1156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  /**
1176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Flush internal buffers.
1186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Some byte sinks use internal buffers or provide buffering
1196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * and require calling Flush() at the end of the stream.
1206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * The ByteSink should be ready for further Append() calls after Flush().
1216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * The default implementation of Flush() does nothing.
1226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @stable ICU 4.2
1236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   */
1246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  virtual void Flush();
1256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgprivate:
1276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  ByteSink(const ByteSink &); // copy constructor not implemented
1286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  ByteSink &operator=(const ByteSink &); // assignment operator not implemented
1296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org};
1306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// -------------------------------------------------------------
1326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// Some standard implementations
1336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/**
1356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * Implementation of ByteSink that writes to a flat byte array,
1366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * with bounds-checking:
1376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * This sink will not write more than capacity bytes to outbuf.
1386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * If more than capacity bytes are Append()ed, then excess bytes are ignored,
1396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * and Overflowed() will return true.
1406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * Overflow does not cause a runtime error.
1416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * @stable ICU 4.2
1426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
1436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgclass U_COMMON_API CheckedArrayByteSink : public ByteSink {
1446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgpublic:
1456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  /**
1466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Constructs a ByteSink that will write to outbuf[0..capacity-1].
1476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param outbuf buffer to write to
1486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param capacity size of the buffer
1496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @stable ICU 4.2
1506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   */
1516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  CheckedArrayByteSink(char* outbuf, int32_t capacity);
1526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  /**
1536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Destructor.
1546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @stable ICU 4.2
1556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   */
1566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  virtual ~CheckedArrayByteSink();
1576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  /**
1586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Returns the sink to its original state, without modifying the buffer.
1596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Useful for reusing both the buffer and the sink for multiple streams.
1606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0
1616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * and Overflowed()=FALSE.
1626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @return *this
1636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @stable ICU 4.6
1646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   */
1656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  virtual CheckedArrayByteSink& Reset();
1666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  /**
1676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Append "bytes[0,n-1]" to this.
1686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param bytes the pointer to the bytes
1696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param n the number of bytes; must be non-negative
1706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @stable ICU 4.2
1716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   */
1726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  virtual void Append(const char* bytes, int32_t n);
1736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  /**
1746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Returns a writable buffer for appending and writes the buffer's capacity to
1756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * *result_capacity. For details see the base class documentation.
1766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param min_capacity required minimum capacity of the returned buffer;
1776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   *                     must be non-negative
1786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param desired_capacity_hint desired capacity of the returned buffer;
1796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   *                              must be non-negative
1806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param scratch default caller-owned buffer
1816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param scratch_capacity capacity of the scratch buffer
1826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param result_capacity pointer to an integer which will be set to the
1836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   *                        capacity of the returned buffer
1846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @return a buffer with *result_capacity>=min_capacity
1856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @stable ICU 4.2
1866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   */
1876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  virtual char* GetAppendBuffer(int32_t min_capacity,
1886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                                int32_t desired_capacity_hint,
1896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                                char* scratch, int32_t scratch_capacity,
1906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                                int32_t* result_capacity);
1916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  /**
1926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Returns the number of bytes actually written to the sink.
1936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @return number of bytes written to the buffer
1946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @stable ICU 4.2
1956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   */
1966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  int32_t NumberOfBytesWritten() const { return size_; }
1976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  /**
1986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Returns true if any bytes were discarded, i.e., if there was an
1996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * attempt to write more than 'capacity' bytes.
2006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @return TRUE if more than 'capacity' bytes were Append()ed
2016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @stable ICU 4.2
2026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   */
2036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  UBool Overflowed() const { return overflowed_; }
2046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  /**
2056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Returns the number of bytes appended to the sink.
2066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten()
2076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * else they return the same number.
2086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @return number of bytes written to the buffer
2096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @stable ICU 4.6
2106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   */
2116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  int32_t NumberOfBytesAppended() const { return appended_; }
2126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgprivate:
2136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  char* outbuf_;
2146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  const int32_t capacity_;
2156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  int32_t size_;
2166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  int32_t appended_;
2176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  UBool overflowed_;
2186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  CheckedArrayByteSink(); ///< default constructor not implemented
2196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  CheckedArrayByteSink(const CheckedArrayByteSink &); ///< copy constructor not implemented
2206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  CheckedArrayByteSink &operator=(const CheckedArrayByteSink &); ///< assignment operator not implemented
2216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org};
2226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_HAVE_STD_STRING
2246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/**
2266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * Implementation of ByteSink that writes to a "string".
2276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * The StringClass is usually instantiated with a std::string.
2286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * @stable ICU 4.2
2296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
2306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgtemplate<typename StringClass>
2316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgclass StringByteSink : public ByteSink {
2326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org public:
2336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  /**
2346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Constructs a ByteSink that will append bytes to the dest string.
2356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param dest pointer to string object to append to
2366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @stable ICU 4.2
2376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   */
2386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  StringByteSink(StringClass* dest) : dest_(dest) { }
2396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  /**
2406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * Append "bytes[0,n-1]" to this.
2416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param data the pointer to the bytes
2426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @param n the number of bytes; must be non-negative
2436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   * @stable ICU 4.2
2446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   */
2456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }
2466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org private:
2476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  StringClass* dest_;
2486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  StringByteSink(); ///< default constructor not implemented
2496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  StringByteSink(const StringByteSink &); ///< copy constructor not implemented
2506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  StringByteSink &operator=(const StringByteSink &); ///< assignment operator not implemented
2516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org};
2526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
2546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_NAMESPACE_END
2566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif  // __BYTESTREAM_H__
258