Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/common/unicode/bytestream.h
38827 views
// Copyright (C) 2009-2012, International Business Machines1// Corporation and others. All Rights Reserved.2//3// Copyright 2007 Google Inc. All Rights Reserved.4// Author: [email protected] (Sanjay Ghemawat)5//6// Abstract interface that consumes a sequence of bytes (ByteSink).7//8// Used so that we can write a single piece of code that can operate9// on a variety of output string types.10//11// Various implementations of this interface are provided:12// ByteSink:13// CheckedArrayByteSink Write to a flat array, with bounds checking14// StringByteSink Write to an STL string1516// This code is a contribution of Google code, and the style used here is17// a compromise between the original Google code and the ICU coding guidelines.18// For example, data types are ICU-ified (size_t,int->int32_t),19// and API comments doxygen-ified, but function names and behavior are20// as in the original, if possible.21// Assertion-style error handling, not available in ICU, was changed to22// parameter "pinning" similar to UnicodeString.23//24// In addition, this is only a partial port of the original Google code,25// limited to what was needed so far. The (nearly) complete original code26// is in the ICU svn repository at icuhtml/trunk/design/strings/contrib27// (see ICU ticket 6765, r25517).2829#ifndef __BYTESTREAM_H__30#define __BYTESTREAM_H__3132/**33* \file34* \brief C++ API: Interface for writing bytes, and implementation classes.35*/3637#include "unicode/utypes.h"38#include "unicode/uobject.h"39#include "unicode/std_string.h"4041U_NAMESPACE_BEGIN4243/**44* A ByteSink can be filled with bytes.45* @stable ICU 4.246*/47class U_COMMON_API ByteSink : public UMemory {48public:49/**50* Default constructor.51* @stable ICU 4.252*/53ByteSink() { }54/**55* Virtual destructor.56* @stable ICU 4.257*/58virtual ~ByteSink();5960/**61* Append "bytes[0,n-1]" to this.62* @param bytes the pointer to the bytes63* @param n the number of bytes; must be non-negative64* @stable ICU 4.265*/66virtual void Append(const char* bytes, int32_t n) = 0;6768/**69* Returns a writable buffer for appending and writes the buffer's capacity to70* *result_capacity. Guarantees *result_capacity>=min_capacity.71* May return a pointer to the caller-owned scratch buffer which must have72* scratch_capacity>=min_capacity.73* The returned buffer is only valid until the next operation74* on this ByteSink.75*76* After writing at most *result_capacity bytes, call Append() with the77* pointer returned from this function and the number of bytes written.78* Many Append() implementations will avoid copying bytes if this function79* returned an internal buffer.80*81* Partial usage example:82* int32_t capacity;83* char* buffer = sink->GetAppendBuffer(..., &capacity);84* ... Write n bytes into buffer, with n <= capacity.85* sink->Append(buffer, n);86* In many implementations, that call to Append will avoid copying bytes.87*88* If the ByteSink allocates or reallocates an internal buffer, it should use89* the desired_capacity_hint if appropriate.90* If a caller cannot provide a reasonable guess at the desired capacity,91* it should pass desired_capacity_hint=0.92*93* If a non-scratch buffer is returned, the caller may only pass94* a prefix to it to Append().95* That is, it is not correct to pass an interior pointer to Append().96*97* The default implementation always returns the scratch buffer.98*99* @param min_capacity required minimum capacity of the returned buffer;100* must be non-negative101* @param desired_capacity_hint desired capacity of the returned buffer;102* must be non-negative103* @param scratch default caller-owned buffer104* @param scratch_capacity capacity of the scratch buffer105* @param result_capacity pointer to an integer which will be set to the106* capacity of the returned buffer107* @return a buffer with *result_capacity>=min_capacity108* @stable ICU 4.2109*/110virtual char* GetAppendBuffer(int32_t min_capacity,111int32_t desired_capacity_hint,112char* scratch, int32_t scratch_capacity,113int32_t* result_capacity);114115/**116* Flush internal buffers.117* Some byte sinks use internal buffers or provide buffering118* and require calling Flush() at the end of the stream.119* The ByteSink should be ready for further Append() calls after Flush().120* The default implementation of Flush() does nothing.121* @stable ICU 4.2122*/123virtual void Flush();124125private:126ByteSink(const ByteSink &); // copy constructor not implemented127ByteSink &operator=(const ByteSink &); // assignment operator not implemented128};129130// -------------------------------------------------------------131// Some standard implementations132133/**134* Implementation of ByteSink that writes to a flat byte array,135* with bounds-checking:136* This sink will not write more than capacity bytes to outbuf.137* If more than capacity bytes are Append()ed, then excess bytes are ignored,138* and Overflowed() will return true.139* Overflow does not cause a runtime error.140* @stable ICU 4.2141*/142class U_COMMON_API CheckedArrayByteSink : public ByteSink {143public:144/**145* Constructs a ByteSink that will write to outbuf[0..capacity-1].146* @param outbuf buffer to write to147* @param capacity size of the buffer148* @stable ICU 4.2149*/150CheckedArrayByteSink(char* outbuf, int32_t capacity);151/**152* Destructor.153* @stable ICU 4.2154*/155virtual ~CheckedArrayByteSink();156/**157* Returns the sink to its original state, without modifying the buffer.158* Useful for reusing both the buffer and the sink for multiple streams.159* Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0160* and Overflowed()=FALSE.161* @return *this162* @stable ICU 4.6163*/164virtual CheckedArrayByteSink& Reset();165/**166* Append "bytes[0,n-1]" to this.167* @param bytes the pointer to the bytes168* @param n the number of bytes; must be non-negative169* @stable ICU 4.2170*/171virtual void Append(const char* bytes, int32_t n);172/**173* Returns a writable buffer for appending and writes the buffer's capacity to174* *result_capacity. For details see the base class documentation.175* @param min_capacity required minimum capacity of the returned buffer;176* must be non-negative177* @param desired_capacity_hint desired capacity of the returned buffer;178* must be non-negative179* @param scratch default caller-owned buffer180* @param scratch_capacity capacity of the scratch buffer181* @param result_capacity pointer to an integer which will be set to the182* capacity of the returned buffer183* @return a buffer with *result_capacity>=min_capacity184* @stable ICU 4.2185*/186virtual char* GetAppendBuffer(int32_t min_capacity,187int32_t desired_capacity_hint,188char* scratch, int32_t scratch_capacity,189int32_t* result_capacity);190/**191* Returns the number of bytes actually written to the sink.192* @return number of bytes written to the buffer193* @stable ICU 4.2194*/195int32_t NumberOfBytesWritten() const { return size_; }196/**197* Returns true if any bytes were discarded, i.e., if there was an198* attempt to write more than 'capacity' bytes.199* @return TRUE if more than 'capacity' bytes were Append()ed200* @stable ICU 4.2201*/202UBool Overflowed() const { return overflowed_; }203/**204* Returns the number of bytes appended to the sink.205* If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten()206* else they return the same number.207* @return number of bytes written to the buffer208* @stable ICU 4.6209*/210int32_t NumberOfBytesAppended() const { return appended_; }211private:212char* outbuf_;213const int32_t capacity_;214int32_t size_;215int32_t appended_;216UBool overflowed_;217CheckedArrayByteSink(); ///< default constructor not implemented218CheckedArrayByteSink(const CheckedArrayByteSink &); ///< copy constructor not implemented219CheckedArrayByteSink &operator=(const CheckedArrayByteSink &); ///< assignment operator not implemented220};221222#if U_HAVE_STD_STRING223224/**225* Implementation of ByteSink that writes to a "string".226* The StringClass is usually instantiated with a std::string.227* @stable ICU 4.2228*/229template<typename StringClass>230class StringByteSink : public ByteSink {231public:232/**233* Constructs a ByteSink that will append bytes to the dest string.234* @param dest pointer to string object to append to235* @stable ICU 4.2236*/237StringByteSink(StringClass* dest) : dest_(dest) { }238/**239* Append "bytes[0,n-1]" to this.240* @param data the pointer to the bytes241* @param n the number of bytes; must be non-negative242* @stable ICU 4.2243*/244virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }245private:246StringClass* dest_;247StringByteSink(); ///< default constructor not implemented248StringByteSink(const StringByteSink &); ///< copy constructor not implemented249StringByteSink &operator=(const StringByteSink &); ///< assignment operator not implemented250};251252#endif253254U_NAMESPACE_END255256#endif // __BYTESTREAM_H__257258259