michael@0: // Copyright (C) 2009-2012, International Business Machines michael@0: // Corporation and others. All Rights Reserved. michael@0: // michael@0: // Copyright 2007 Google Inc. All Rights Reserved. michael@0: // Author: sanjay@google.com (Sanjay Ghemawat) michael@0: // michael@0: // Abstract interface that consumes a sequence of bytes (ByteSink). michael@0: // michael@0: // Used so that we can write a single piece of code that can operate michael@0: // on a variety of output string types. michael@0: // michael@0: // Various implementations of this interface are provided: michael@0: // ByteSink: michael@0: // CheckedArrayByteSink Write to a flat array, with bounds checking michael@0: // StringByteSink Write to an STL string michael@0: michael@0: // This code is a contribution of Google code, and the style used here is michael@0: // a compromise between the original Google code and the ICU coding guidelines. michael@0: // For example, data types are ICU-ified (size_t,int->int32_t), michael@0: // and API comments doxygen-ified, but function names and behavior are michael@0: // as in the original, if possible. michael@0: // Assertion-style error handling, not available in ICU, was changed to michael@0: // parameter "pinning" similar to UnicodeString. michael@0: // michael@0: // In addition, this is only a partial port of the original Google code, michael@0: // limited to what was needed so far. The (nearly) complete original code michael@0: // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib michael@0: // (see ICU ticket 6765, r25517). michael@0: michael@0: #ifndef __BYTESTREAM_H__ michael@0: #define __BYTESTREAM_H__ michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: Interface for writing bytes, and implementation classes. michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #include "unicode/uobject.h" michael@0: #include "unicode/std_string.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /** michael@0: * A ByteSink can be filled with bytes. michael@0: * @stable ICU 4.2 michael@0: */ michael@0: class U_COMMON_API ByteSink : public UMemory { michael@0: public: michael@0: /** michael@0: * Default constructor. michael@0: * @stable ICU 4.2 michael@0: */ michael@0: ByteSink() { } michael@0: /** michael@0: * Virtual destructor. michael@0: * @stable ICU 4.2 michael@0: */ michael@0: virtual ~ByteSink(); michael@0: michael@0: /** michael@0: * Append "bytes[0,n-1]" to this. michael@0: * @param bytes the pointer to the bytes michael@0: * @param n the number of bytes; must be non-negative michael@0: * @stable ICU 4.2 michael@0: */ michael@0: virtual void Append(const char* bytes, int32_t n) = 0; michael@0: michael@0: /** michael@0: * Returns a writable buffer for appending and writes the buffer's capacity to michael@0: * *result_capacity. Guarantees *result_capacity>=min_capacity. michael@0: * May return a pointer to the caller-owned scratch buffer which must have michael@0: * scratch_capacity>=min_capacity. michael@0: * The returned buffer is only valid until the next operation michael@0: * on this ByteSink. michael@0: * michael@0: * After writing at most *result_capacity bytes, call Append() with the michael@0: * pointer returned from this function and the number of bytes written. michael@0: * Many Append() implementations will avoid copying bytes if this function michael@0: * returned an internal buffer. michael@0: * michael@0: * Partial usage example: michael@0: * int32_t capacity; michael@0: * char* buffer = sink->GetAppendBuffer(..., &capacity); michael@0: * ... Write n bytes into buffer, with n <= capacity. michael@0: * sink->Append(buffer, n); michael@0: * In many implementations, that call to Append will avoid copying bytes. michael@0: * michael@0: * If the ByteSink allocates or reallocates an internal buffer, it should use michael@0: * the desired_capacity_hint if appropriate. michael@0: * If a caller cannot provide a reasonable guess at the desired capacity, michael@0: * it should pass desired_capacity_hint=0. michael@0: * michael@0: * If a non-scratch buffer is returned, the caller may only pass michael@0: * a prefix to it to Append(). michael@0: * That is, it is not correct to pass an interior pointer to Append(). michael@0: * michael@0: * The default implementation always returns the scratch buffer. michael@0: * michael@0: * @param min_capacity required minimum capacity of the returned buffer; michael@0: * must be non-negative michael@0: * @param desired_capacity_hint desired capacity of the returned buffer; michael@0: * must be non-negative michael@0: * @param scratch default caller-owned buffer michael@0: * @param scratch_capacity capacity of the scratch buffer michael@0: * @param result_capacity pointer to an integer which will be set to the michael@0: * capacity of the returned buffer michael@0: * @return a buffer with *result_capacity>=min_capacity michael@0: * @stable ICU 4.2 michael@0: */ michael@0: virtual char* GetAppendBuffer(int32_t min_capacity, michael@0: int32_t desired_capacity_hint, michael@0: char* scratch, int32_t scratch_capacity, michael@0: int32_t* result_capacity); michael@0: michael@0: /** michael@0: * Flush internal buffers. michael@0: * Some byte sinks use internal buffers or provide buffering michael@0: * and require calling Flush() at the end of the stream. michael@0: * The ByteSink should be ready for further Append() calls after Flush(). michael@0: * The default implementation of Flush() does nothing. michael@0: * @stable ICU 4.2 michael@0: */ michael@0: virtual void Flush(); michael@0: michael@0: private: michael@0: ByteSink(const ByteSink &); // copy constructor not implemented michael@0: ByteSink &operator=(const ByteSink &); // assignment operator not implemented michael@0: }; michael@0: michael@0: // ------------------------------------------------------------- michael@0: // Some standard implementations michael@0: michael@0: /** michael@0: * Implementation of ByteSink that writes to a flat byte array, michael@0: * with bounds-checking: michael@0: * This sink will not write more than capacity bytes to outbuf. michael@0: * If more than capacity bytes are Append()ed, then excess bytes are ignored, michael@0: * and Overflowed() will return true. michael@0: * Overflow does not cause a runtime error. michael@0: * @stable ICU 4.2 michael@0: */ michael@0: class U_COMMON_API CheckedArrayByteSink : public ByteSink { michael@0: public: michael@0: /** michael@0: * Constructs a ByteSink that will write to outbuf[0..capacity-1]. michael@0: * @param outbuf buffer to write to michael@0: * @param capacity size of the buffer michael@0: * @stable ICU 4.2 michael@0: */ michael@0: CheckedArrayByteSink(char* outbuf, int32_t capacity); michael@0: /** michael@0: * Destructor. michael@0: * @stable ICU 4.2 michael@0: */ michael@0: virtual ~CheckedArrayByteSink(); michael@0: /** michael@0: * Returns the sink to its original state, without modifying the buffer. michael@0: * Useful for reusing both the buffer and the sink for multiple streams. michael@0: * Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0 michael@0: * and Overflowed()=FALSE. michael@0: * @return *this michael@0: * @stable ICU 4.6 michael@0: */ michael@0: virtual CheckedArrayByteSink& Reset(); michael@0: /** michael@0: * Append "bytes[0,n-1]" to this. michael@0: * @param bytes the pointer to the bytes michael@0: * @param n the number of bytes; must be non-negative michael@0: * @stable ICU 4.2 michael@0: */ michael@0: virtual void Append(const char* bytes, int32_t n); michael@0: /** michael@0: * Returns a writable buffer for appending and writes the buffer's capacity to michael@0: * *result_capacity. For details see the base class documentation. michael@0: * @param min_capacity required minimum capacity of the returned buffer; michael@0: * must be non-negative michael@0: * @param desired_capacity_hint desired capacity of the returned buffer; michael@0: * must be non-negative michael@0: * @param scratch default caller-owned buffer michael@0: * @param scratch_capacity capacity of the scratch buffer michael@0: * @param result_capacity pointer to an integer which will be set to the michael@0: * capacity of the returned buffer michael@0: * @return a buffer with *result_capacity>=min_capacity michael@0: * @stable ICU 4.2 michael@0: */ michael@0: virtual char* GetAppendBuffer(int32_t min_capacity, michael@0: int32_t desired_capacity_hint, michael@0: char* scratch, int32_t scratch_capacity, michael@0: int32_t* result_capacity); michael@0: /** michael@0: * Returns the number of bytes actually written to the sink. michael@0: * @return number of bytes written to the buffer michael@0: * @stable ICU 4.2 michael@0: */ michael@0: int32_t NumberOfBytesWritten() const { return size_; } michael@0: /** michael@0: * Returns true if any bytes were discarded, i.e., if there was an michael@0: * attempt to write more than 'capacity' bytes. michael@0: * @return TRUE if more than 'capacity' bytes were Append()ed michael@0: * @stable ICU 4.2 michael@0: */ michael@0: UBool Overflowed() const { return overflowed_; } michael@0: /** michael@0: * Returns the number of bytes appended to the sink. michael@0: * If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten() michael@0: * else they return the same number. michael@0: * @return number of bytes written to the buffer michael@0: * @stable ICU 4.6 michael@0: */ michael@0: int32_t NumberOfBytesAppended() const { return appended_; } michael@0: private: michael@0: char* outbuf_; michael@0: const int32_t capacity_; michael@0: int32_t size_; michael@0: int32_t appended_; michael@0: UBool overflowed_; michael@0: CheckedArrayByteSink(); ///< default constructor not implemented michael@0: CheckedArrayByteSink(const CheckedArrayByteSink &); ///< copy constructor not implemented michael@0: CheckedArrayByteSink &operator=(const CheckedArrayByteSink &); ///< assignment operator not implemented michael@0: }; michael@0: michael@0: #if U_HAVE_STD_STRING michael@0: michael@0: /** michael@0: * Implementation of ByteSink that writes to a "string". michael@0: * The StringClass is usually instantiated with a std::string. michael@0: * @stable ICU 4.2 michael@0: */ michael@0: template michael@0: class StringByteSink : public ByteSink { michael@0: public: michael@0: /** michael@0: * Constructs a ByteSink that will append bytes to the dest string. michael@0: * @param dest pointer to string object to append to michael@0: * @stable ICU 4.2 michael@0: */ michael@0: StringByteSink(StringClass* dest) : dest_(dest) { } michael@0: /** michael@0: * Append "bytes[0,n-1]" to this. michael@0: * @param data the pointer to the bytes michael@0: * @param n the number of bytes; must be non-negative michael@0: * @stable ICU 4.2 michael@0: */ michael@0: virtual void Append(const char* data, int32_t n) { dest_->append(data, n); } michael@0: private: michael@0: StringClass* dest_; michael@0: StringByteSink(); ///< default constructor not implemented michael@0: StringByteSink(const StringByteSink &); ///< copy constructor not implemented michael@0: StringByteSink &operator=(const StringByteSink &); ///< assignment operator not implemented michael@0: }; michael@0: michael@0: #endif michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif // __BYTESTREAM_H__