Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/share/native/common/unicode/bytestream.h
48729 views
// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2// Copyright (C) 2009-2012, International Business Machines3// Corporation and others. All Rights Reserved.4//5// Copyright 2007 Google Inc. All Rights Reserved.6// Author: [email protected] (Sanjay Ghemawat)7//8// Abstract interface that consumes a sequence of bytes (ByteSink).9//10// Used so that we can write a single piece of code that can operate11// on a variety of output string types.12//13// Various implementations of this interface are provided:14// ByteSink:15// CheckedArrayByteSink Write to a flat array, with bounds checking16// StringByteSink Write to an STL string1718// This code is a contribution of Google code, and the style used here is19// a compromise between the original Google code and the ICU coding guidelines.20// For example, data types are ICU-ified (size_t,int->int32_t),21// and API comments doxygen-ified, but function names and behavior are22// as in the original, if possible.23// Assertion-style error handling, not available in ICU, was changed to24// parameter "pinning" similar to UnicodeString.25//26// In addition, this is only a partial port of the original Google code,27// limited to what was needed so far. The (nearly) complete original code28// is in the ICU svn repository at icuhtml/trunk/design/strings/contrib29// (see ICU ticket 6765, r25517).3031#ifndef __BYTESTREAM_H__32#define __BYTESTREAM_H__3334/**35* \file36* \brief C++ API: Interface for writing bytes, and implementation classes.37*/3839#include "unicode/utypes.h"40#include "unicode/uobject.h"41#include "unicode/std_string.h"4243U_NAMESPACE_BEGIN4445/**46* A ByteSink can be filled with bytes.47* @stable ICU 4.248*/49class U_COMMON_API ByteSink : public UMemory {50public:51/**52* Default constructor.53* @stable ICU 4.254*/55ByteSink() { }56/**57* Virtual destructor.58* @stable ICU 4.259*/60virtual ~ByteSink();6162/**63* Append "bytes[0,n-1]" to this.64* @param bytes the pointer to the bytes65* @param n the number of bytes; must be non-negative66* @stable ICU 4.267*/68virtual void Append(const char* bytes, int32_t n) = 0;6970/**71* Returns a writable buffer for appending and writes the buffer's capacity to72* *result_capacity. Guarantees *result_capacity>=min_capacity.73* May return a pointer to the caller-owned scratch buffer which must have74* scratch_capacity>=min_capacity.75* The returned buffer is only valid until the next operation76* on this ByteSink.77*78* After writing at most *result_capacity bytes, call Append() with the79* pointer returned from this function and the number of bytes written.80* Many Append() implementations will avoid copying bytes if this function81* returned an internal buffer.82*83* Partial usage example:84* int32_t capacity;85* char* buffer = sink->GetAppendBuffer(..., &capacity);86* ... Write n bytes into buffer, with n <= capacity.87* sink->Append(buffer, n);88* In many implementations, that call to Append will avoid copying bytes.89*90* If the ByteSink allocates or reallocates an internal buffer, it should use91* the desired_capacity_hint if appropriate.92* If a caller cannot provide a reasonable guess at the desired capacity,93* it should pass desired_capacity_hint=0.94*95* If a non-scratch buffer is returned, the caller may only pass96* a prefix to it to Append().97* That is, it is not correct to pass an interior pointer to Append().98*99* The default implementation always returns the scratch buffer.100*101* @param min_capacity required minimum capacity of the returned buffer;102* must be non-negative103* @param desired_capacity_hint desired capacity of the returned buffer;104* must be non-negative105* @param scratch default caller-owned buffer106* @param scratch_capacity capacity of the scratch buffer107* @param result_capacity pointer to an integer which will be set to the108* capacity of the returned buffer109* @return a buffer with *result_capacity>=min_capacity110* @stable ICU 4.2111*/112virtual char* GetAppendBuffer(int32_t min_capacity,113int32_t desired_capacity_hint,114char* scratch, int32_t scratch_capacity,115int32_t* result_capacity);116117/**118* Flush internal buffers.119* Some byte sinks use internal buffers or provide buffering120* and require calling Flush() at the end of the stream.121* The ByteSink should be ready for further Append() calls after Flush().122* The default implementation of Flush() does nothing.123* @stable ICU 4.2124*/125virtual void Flush();126127private:128ByteSink(const ByteSink &) = delete;129ByteSink &operator=(const ByteSink &) = delete;130};131132// -------------------------------------------------------------133// Some standard implementations134135/**136* Implementation of ByteSink that writes to a flat byte array,137* with bounds-checking:138* This sink will not write more than capacity bytes to outbuf.139* If more than capacity bytes are Append()ed, then excess bytes are ignored,140* and Overflowed() will return true.141* Overflow does not cause a runtime error.142* @stable ICU 4.2143*/144class U_COMMON_API CheckedArrayByteSink : public ByteSink {145public:146/**147* Constructs a ByteSink that will write to outbuf[0..capacity-1].148* @param outbuf buffer to write to149* @param capacity size of the buffer150* @stable ICU 4.2151*/152CheckedArrayByteSink(char* outbuf, int32_t capacity);153/**154* Destructor.155* @stable ICU 4.2156*/157virtual ~CheckedArrayByteSink();158/**159* Returns the sink to its original state, without modifying the buffer.160* Useful for reusing both the buffer and the sink for multiple streams.161* Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0162* and Overflowed()=FALSE.163* @return *this164* @stable ICU 4.6165*/166virtual CheckedArrayByteSink& Reset();167/**168* Append "bytes[0,n-1]" to this.169* @param bytes the pointer to the bytes170* @param n the number of bytes; must be non-negative171* @stable ICU 4.2172*/173virtual void Append(const char* bytes, int32_t n);174/**175* Returns a writable buffer for appending and writes the buffer's capacity to176* *result_capacity. For details see the base class documentation.177* @param min_capacity required minimum capacity of the returned buffer;178* must be non-negative179* @param desired_capacity_hint desired capacity of the returned buffer;180* must be non-negative181* @param scratch default caller-owned buffer182* @param scratch_capacity capacity of the scratch buffer183* @param result_capacity pointer to an integer which will be set to the184* capacity of the returned buffer185* @return a buffer with *result_capacity>=min_capacity186* @stable ICU 4.2187*/188virtual char* GetAppendBuffer(int32_t min_capacity,189int32_t desired_capacity_hint,190char* scratch, int32_t scratch_capacity,191int32_t* result_capacity);192/**193* Returns the number of bytes actually written to the sink.194* @return number of bytes written to the buffer195* @stable ICU 4.2196*/197int32_t NumberOfBytesWritten() const { return size_; }198/**199* Returns true if any bytes were discarded, i.e., if there was an200* attempt to write more than 'capacity' bytes.201* @return TRUE if more than 'capacity' bytes were Append()ed202* @stable ICU 4.2203*/204UBool Overflowed() const { return overflowed_; }205/**206* Returns the number of bytes appended to the sink.207* If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten()208* else they return the same number.209* @return number of bytes written to the buffer210* @stable ICU 4.6211*/212int32_t NumberOfBytesAppended() const { return appended_; }213private:214char* outbuf_;215const int32_t capacity_;216int32_t size_;217int32_t appended_;218UBool overflowed_;219220CheckedArrayByteSink() = delete;221CheckedArrayByteSink(const CheckedArrayByteSink &) = delete;222CheckedArrayByteSink &operator=(const CheckedArrayByteSink &) = delete;223};224225/**226* Implementation of ByteSink that writes to a "string".227* The StringClass is usually instantiated with a std::string.228* @stable ICU 4.2229*/230template<typename StringClass>231class StringByteSink : public ByteSink {232public:233/**234* Constructs a ByteSink that will append bytes to the dest string.235* @param dest pointer to string object to append to236* @stable ICU 4.2237*/238StringByteSink(StringClass* dest) : dest_(dest) { }239/**240* Constructs a ByteSink that reserves append capacity and will append bytes to the dest string.241*242* @param dest pointer to string object to append to243* @param initialAppendCapacity capacity beyond dest->length() to be reserve()d244* @stable ICU 60245*/246StringByteSink(StringClass* dest, int32_t initialAppendCapacity) : dest_(dest) {247if (initialAppendCapacity > 0 &&248(uint32_t)initialAppendCapacity > (dest->capacity() - dest->length())) {249dest->reserve(dest->length() + initialAppendCapacity);250}251}252/**253* Append "bytes[0,n-1]" to this.254* @param data the pointer to the bytes255* @param n the number of bytes; must be non-negative256* @stable ICU 4.2257*/258virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }259private:260StringClass* dest_;261262StringByteSink() = delete;263StringByteSink(const StringByteSink &) = delete;264StringByteSink &operator=(const StringByteSink &) = delete;265};266267U_NAMESPACE_END268269#endif // __BYTESTREAM_H__270271272