<!-- This comment will put IE 6, 7 and 8 in quirks mode --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>ICU 4.4.1: bytestream.h Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="search/search.css" rel="stylesheet" type="text/css"/> <script type="text/javaScript" src="search/search.js"></script> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body onload='searchBox.OnSelectItem(0);'> <!-- Generated by Doxygen 1.6.2-20100208 --> <script type="text/javascript"><!-- var searchBox = new SearchBox("searchBox", "search",false,'Search'); --></script> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li> <div id="MSearchBox" class="MSearchBoxInactive"> <img id="MSearchSelect" src="search/search.png" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" alt=""/> <input type="text" id="MSearchField" value="Search" accesskey="S" onfocus="searchBox.OnSearchFieldFocus(true)" onblur="searchBox.OnSearchFieldFocus(false)" onkeyup="searchBox.OnSearchFieldChange(event)"/> <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a> </div> </li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>Globals</span></a></li> </ul> </div> <h1>bytestream.h</h1><a href="bytestream_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// Copyright (C) 2009-2010, International Business Machines</span> <a name="l00002"></a>00002 <span class="comment">// Corporation and others. All Rights Reserved.</span> <a name="l00003"></a>00003 <span class="comment">//</span> <a name="l00004"></a>00004 <span class="comment">// Copyright 2007 Google Inc. All Rights Reserved.</span> <a name="l00005"></a>00005 <span class="comment">// Author: sanjay@google.com (Sanjay Ghemawat)</span> <a name="l00006"></a>00006 <span class="comment">//</span> <a name="l00007"></a>00007 <span class="comment">// Abstract interface that consumes a sequence of bytes (ByteSink).</span> <a name="l00008"></a>00008 <span class="comment">//</span> <a name="l00009"></a>00009 <span class="comment">// Used so that we can write a single piece of code that can operate</span> <a name="l00010"></a>00010 <span class="comment">// on a variety of output string types.</span> <a name="l00011"></a>00011 <span class="comment">//</span> <a name="l00012"></a>00012 <span class="comment">// Various implementations of this interface are provided:</span> <a name="l00013"></a>00013 <span class="comment">// ByteSink:</span> <a name="l00014"></a>00014 <span class="comment">// CheckedArrayByteSink Write to a flat array, with bounds checking</span> <a name="l00015"></a>00015 <span class="comment">// StringByteSink Write to an STL string</span> <a name="l00016"></a>00016 <a name="l00017"></a>00017 <span class="comment">// This code is a contribution of Google code, and the style used here is</span> <a name="l00018"></a>00018 <span class="comment">// a compromise between the original Google code and the ICU coding guidelines.</span> <a name="l00019"></a>00019 <span class="comment">// For example, data types are ICU-ified (size_t,int->int32_t),</span> <a name="l00020"></a>00020 <span class="comment">// and API comments doxygen-ified, but function names and behavior are</span> <a name="l00021"></a>00021 <span class="comment">// as in the original, if possible.</span> <a name="l00022"></a>00022 <span class="comment">// Assertion-style error handling, not available in ICU, was changed to</span> <a name="l00023"></a>00023 <span class="comment">// parameter "pinning" similar to UnicodeString.</span> <a name="l00024"></a>00024 <span class="comment">//</span> <a name="l00025"></a>00025 <span class="comment">// In addition, this is only a partial port of the original Google code,</span> <a name="l00026"></a>00026 <span class="comment">// limited to what was needed so far. The (nearly) complete original code</span> <a name="l00027"></a>00027 <span class="comment">// is in the ICU svn repository at icuhtml/trunk/design/strings/contrib</span> <a name="l00028"></a>00028 <span class="comment">// (see ICU ticket 6765, r25517).</span> <a name="l00029"></a>00029 <a name="l00030"></a>00030 <span class="preprocessor">#ifndef __BYTESTREAM_H__</span> <a name="l00031"></a>00031 <span class="preprocessor"></span><span class="preprocessor">#define __BYTESTREAM_H__</span> <a name="l00032"></a>00032 <span class="preprocessor"></span> <a name="l00038"></a>00038 <span class="preprocessor">#include "<a class="code" href="utypes_8h.html" title="Basic definitions for ICU, for both C and C++ APIs.">unicode/utypes.h</a>"</span> <a name="l00039"></a>00039 <span class="preprocessor">#include "<a class="code" href="uobject_8h.html" title="C++ API: Common ICU base class UObject.">unicode/uobject.h</a>"</span> <a name="l00040"></a>00040 <span class="preprocessor">#include "<a class="code" href="std__string_8h.html" title="C++ API: Central ICU header for including the C++ standard &lt;string&gt; header...">unicode/std_string.h</a>"</span> <a name="l00041"></a>00041 <a name="l00042"></a>00042 <a class="code" href="uversion_8h.html#a7d9374e037299886d4192b763b4c5e8b" title="This is used to begin a declaration of a public ICU C++ API.">U_NAMESPACE_BEGIN</a> <a name="l00043"></a>00043 <a name="l00048"></a><a class="code" href="classByteSink.html">00048</a> <span class="keyword">class </span><a class="code" href="utypes_8h.html#a559955ec3bc692657c5d8b1c6de8e334" title="Set to export library symbols from inside the common library, and to import them...">U_COMMON_API</a> <a class="code" href="classByteSink.html" title="A ByteSink can be filled with bytes.">ByteSink</a> : <span class="keyword">public</span> <a class="code" href="classUMemory.html" title="UMemory is the common ICU base class.">UMemory</a> { <a name="l00049"></a>00049 <span class="keyword">public</span>: <a name="l00054"></a><a class="code" href="classByteSink.html#a6247961c1b73f897b276a540cd0288f9">00054</a> <a class="code" href="classByteSink.html#a6247961c1b73f897b276a540cd0288f9" title="Default constructor.">ByteSink</a>() { } <a name="l00059"></a><a class="code" href="classByteSink.html#ac2e07401e236bcb8545ce9ee96d801fc">00059</a> <span class="keyword">virtual</span> <a class="code" href="classByteSink.html#ac2e07401e236bcb8545ce9ee96d801fc" title="Virtual destructor.">~ByteSink</a>() { } <a name="l00060"></a>00060 <a name="l00067"></a>00067 <span class="keyword">virtual</span> <span class="keywordtype">void</span> Append(<span class="keyword">const</span> <span class="keywordtype">char</span>* bytes, int32_t n) = 0; <a name="l00068"></a>00068 <a name="l00111"></a>00111 <span class="keyword">virtual</span> <span class="keywordtype">char</span>* GetAppendBuffer(int32_t min_capacity, <a name="l00112"></a>00112 int32_t desired_capacity_hint, <a name="l00113"></a>00113 <span class="keywordtype">char</span>* scratch, int32_t scratch_capacity, <a name="l00114"></a>00114 int32_t* result_capacity); <a name="l00115"></a>00115 <a name="l00123"></a>00123 <span class="keyword">virtual</span> <span class="keywordtype">void</span> Flush(); <a name="l00124"></a>00124 <a name="l00125"></a>00125 <span class="keyword">private</span>: <a name="l00126"></a>00126 <a class="code" href="classByteSink.html" title="A ByteSink can be filled with bytes.">ByteSink</a>(<span class="keyword">const</span> <a class="code" href="classByteSink.html" title="A ByteSink can be filled with bytes.">ByteSink</a> &); <span class="comment">// copy constructor not implemented</span> <a name="l00127"></a>00127 <a class="code" href="classByteSink.html" title="A ByteSink can be filled with bytes.">ByteSink</a> &operator=(<span class="keyword">const</span> <a class="code" href="classByteSink.html" title="A ByteSink can be filled with bytes.">ByteSink</a> &); <span class="comment">// assignment operator not implemented</span> <a name="l00128"></a>00128 }; <a name="l00129"></a>00129 <a name="l00130"></a>00130 <span class="comment">// -------------------------------------------------------------</span> <a name="l00131"></a>00131 <span class="comment">// Some standard implementations</span> <a name="l00132"></a>00132 <a name="l00142"></a><a class="code" href="classCheckedArrayByteSink.html">00142</a> <span class="keyword">class </span><a class="code" href="utypes_8h.html#a559955ec3bc692657c5d8b1c6de8e334" title="Set to export library symbols from inside the common library, and to import them...">U_COMMON_API</a> <a class="code" href="classCheckedArrayByteSink.html" title="Implementation of ByteSink that writes to a flat byte array, with bounds-checking:...">CheckedArrayByteSink</a> : <span class="keyword">public</span> <a class="code" href="classByteSink.html" title="A ByteSink can be filled with bytes.">ByteSink</a> { <a name="l00143"></a>00143 <span class="keyword">public</span>: <a name="l00150"></a>00150 <a class="code" href="classCheckedArrayByteSink.html" title="Implementation of ByteSink that writes to a flat byte array, with bounds-checking:...">CheckedArrayByteSink</a>(<span class="keywordtype">char</span>* outbuf, int32_t capacity); <a name="l00157"></a>00157 <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classByteSink.html#a1f04f7474f4822c29b3a20fde509a3d8" title="Append &quot;bytes[0,n-1]&quot; to this.">Append</a>(<span class="keyword">const</span> <span class="keywordtype">char</span>* bytes, int32_t n); <a name="l00172"></a>00172 <span class="keyword">virtual</span> <span class="keywordtype">char</span>* <a class="code" href="classByteSink.html#afbff3e7aca11c652464153b955071201" title="Returns a writable buffer for appending and writes the buffer&#39;s capacity to *result_capacity...">GetAppendBuffer</a>(int32_t min_capacity, <a name="l00173"></a>00173 int32_t desired_capacity_hint, <a name="l00174"></a>00174 <span class="keywordtype">char</span>* scratch, int32_t scratch_capacity, <a name="l00175"></a>00175 int32_t* result_capacity); <a name="l00181"></a><a class="code" href="classCheckedArrayByteSink.html#a83068a3fcf59cc7af120de540b423371">00181</a> int32_t <a class="code" href="classCheckedArrayByteSink.html#a83068a3fcf59cc7af120de540b423371" title="Returns the number of bytes actually written to the sink.">NumberOfBytesWritten</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> size_; } <a name="l00188"></a><a class="code" href="classCheckedArrayByteSink.html#adb0ed4a2592e3c8e09fff38d2fe94981">00188</a> <a class="code" href="umachine_8h.html#a349ef00011f20ccd1d3b424445681aa5" title="The ICU boolean type.">UBool</a> <a class="code" href="classCheckedArrayByteSink.html#adb0ed4a2592e3c8e09fff38d2fe94981" title="Returns true if any bytes were discarded, i.e., if there was an attempt to write...">Overflowed</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> overflowed_; } <a name="l00189"></a>00189 <span class="keyword">private</span>: <a name="l00190"></a>00190 <span class="keywordtype">char</span>* outbuf_; <a name="l00191"></a>00191 <span class="keyword">const</span> int32_t capacity_; <a name="l00192"></a>00192 int32_t size_; <a name="l00193"></a>00193 <span class="keywordtype">bool</span> overflowed_; <a name="l00194"></a>00194 <a class="code" href="classCheckedArrayByteSink.html" title="Implementation of ByteSink that writes to a flat byte array, with bounds-checking:...">CheckedArrayByteSink</a>(); <a name="l00195"></a>00195 <a class="code" href="classCheckedArrayByteSink.html" title="Implementation of ByteSink that writes to a flat byte array, with bounds-checking:...">CheckedArrayByteSink</a>(<span class="keyword">const</span> <a class="code" href="classCheckedArrayByteSink.html" title="Implementation of ByteSink that writes to a flat byte array, with bounds-checking:...">CheckedArrayByteSink</a> &); <a name="l00196"></a>00196 <a class="code" href="classCheckedArrayByteSink.html" title="Implementation of ByteSink that writes to a flat byte array, with bounds-checking:...">CheckedArrayByteSink</a> &operator=(<span class="keyword">const</span> <a class="code" href="classCheckedArrayByteSink.html" title="Implementation of ByteSink that writes to a flat byte array, with bounds-checking:...">CheckedArrayByteSink</a> &); <a name="l00197"></a>00197 }; <a name="l00198"></a>00198 <a name="l00199"></a>00199 <span class="preprocessor">#if U_HAVE_STD_STRING</span> <a name="l00200"></a>00200 <span class="preprocessor"></span> <a name="l00206"></a>00206 <span class="keyword">template</span><<span class="keyword">typename</span> StringClass> <a name="l00207"></a><a class="code" href="classStringByteSink.html">00207</a> <span class="keyword">class </span><a class="code" href="classStringByteSink.html" title="Implementation of ByteSink that writes to a &quot;string&quot;.">StringByteSink</a> : <span class="keyword">public</span> <a class="code" href="classByteSink.html" title="A ByteSink can be filled with bytes.">ByteSink</a> { <a name="l00208"></a>00208 <span class="keyword">public</span>: <a name="l00214"></a><a class="code" href="classStringByteSink.html#a632e52941abe840947888682ddd29d97">00214</a> <a class="code" href="classStringByteSink.html#a632e52941abe840947888682ddd29d97" title="Constructs a ByteSink that will append bytes to the dest string.">StringByteSink</a>(StringClass* dest) : dest_(dest) { } <a name="l00221"></a><a class="code" href="classStringByteSink.html#ac377073d20cc64ac21ef35c6f4901ba4">00221</a> <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classStringByteSink.html#ac377073d20cc64ac21ef35c6f4901ba4" title="Append &quot;bytes[0,n-1]&quot; to this.">Append</a>(<span class="keyword">const</span> <span class="keywordtype">char</span>* data, int32_t n) { dest_->append(data, n); } <a name="l00222"></a>00222 <span class="keyword">private</span>: <a name="l00223"></a>00223 StringClass* dest_; <a name="l00224"></a>00224 <a class="code" href="classStringByteSink.html" title="Implementation of ByteSink that writes to a &quot;string&quot;.">StringByteSink</a>(); <a name="l00225"></a>00225 <a class="code" href="classStringByteSink.html" title="Implementation of ByteSink that writes to a &quot;string&quot;.">StringByteSink</a>(<span class="keyword">const</span> <a class="code" href="classStringByteSink.html" title="Implementation of ByteSink that writes to a &quot;string&quot;.">StringByteSink</a> &); <a name="l00226"></a>00226 <a class="code" href="classStringByteSink.html" title="Implementation of ByteSink that writes to a &quot;string&quot;.">StringByteSink</a> &operator=(<span class="keyword">const</span> <a class="code" href="classStringByteSink.html" title="Implementation of ByteSink that writes to a &quot;string&quot;.">StringByteSink</a> &); <a name="l00227"></a>00227 }; <a name="l00228"></a>00228 <a name="l00229"></a>00229 <span class="preprocessor">#endif</span> <a name="l00230"></a>00230 <span class="preprocessor"></span> <a name="l00231"></a>00231 <a class="code" href="uversion_8h.html#a3da1f00f3b02976ac53c014a3e4c0933" title="This is used to end a declaration of a public ICU C++ API.">U_NAMESPACE_END</a> <a name="l00232"></a>00232 <a name="l00233"></a>00233 <span class="preprocessor">#endif // __BYTESTREAM_H__</span> </pre></div></div> <!--- window showing the filter options --> <div id="MSearchSelectWindow" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" onkeydown="return searchBox.OnSearchSelectKey(event)"> <a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark"> </span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark"> </span>Defines</a></div> <!-- iframe showing the search results (closed by default) --> <div id="MSearchResultsWindow"> <iframe src="" frameborder="0" name="MSearchResults" id="MSearchResults"> </iframe> </div> <hr class="footer"/><address style="text-align: right;"><small>Generated by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2-20100208 </small></address> </body> </html>