1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>zlib::deflate_stream::write</title> 5<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../index.html" title="Chapter 1. Boost.Beast"> 8<link rel="up" href="../boost__beast__zlib__deflate_stream.html" title="zlib::deflate_stream"> 9<link rel="prev" href="tune.html" title="zlib::deflate_stream::tune"> 10<link rel="next" href="params.html" title="zlib::deflate_stream::params"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="tune.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost__beast__zlib__deflate_stream.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="params.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h5 class="title"> 27<a name="beast.ref.boost__beast__zlib__deflate_stream.write"></a><a class="link" href="write.html" title="zlib::deflate_stream::write">zlib::deflate_stream::write</a> 28</h5></div></div></div> 29<p> 30 <a class="indexterm" name="idm46057540347584"></a> 31 </p> 32<p> 33 Compress input and write output. 34 </p> 35<h6> 36<a name="beast.ref.boost__beast__zlib__deflate_stream.write.h0"></a> 37 <span class="phrase"><a name="beast.ref.boost__beast__zlib__deflate_stream.write.synopsis"></a></span><a class="link" href="write.html#beast.ref.boost__beast__zlib__deflate_stream.write.synopsis">Synopsis</a> 38 </h6> 39<pre class="programlisting"><span class="keyword">void</span> 40<span class="identifier">write</span><span class="special">(</span> 41 <span class="identifier">z_params</span><span class="special">&</span> <span class="identifier">zs</span><span class="special">,</span> 42 <span class="identifier">Flush</span> <span class="identifier">flush</span><span class="special">,</span> 43 <span class="identifier">error_code</span><span class="special">&</span> <span class="identifier">ec</span><span class="special">);</span> 44</pre> 45<h6> 46<a name="beast.ref.boost__beast__zlib__deflate_stream.write.h1"></a> 47 <span class="phrase"><a name="beast.ref.boost__beast__zlib__deflate_stream.write.description"></a></span><a class="link" href="write.html#beast.ref.boost__beast__zlib__deflate_stream.write.description">Description</a> 48 </h6> 49<p> 50 This function compresses as much data as possible, and stops when the input 51 buffer becomes empty or the output buffer becomes full. It may introduce 52 some output latency (reading input without producing any output) except 53 when forced to flush. In each call, one or both of these actions are performed: 54 </p> 55<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 56<li class="listitem"> 57 Compress more input starting at <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">next_in</span></code> 58 and update <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">next_in</span></code> and <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_in</span></code> 59 accordingly. If not all input can be processed (because there is not 60 enough room in the output buffer), <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">next_in</span></code> 61 and <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_in</span></code> are updated and processing 62 will resume at this point for the next call. 63 </li> 64<li class="listitem"> 65 Provide more output starting at <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">next_out</span></code> 66 and update <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">next_out</span></code> and <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_out</span></code> 67 accordingly. This action is forced if the parameter flush is not <a class="link" href="../boost__beast__zlib__Flush.html" title="zlib::Flush"><code class="computeroutput"><span class="identifier">Flush</span><span class="special">::</span><span class="identifier">none</span></code></a>. 68 Forcing flush frequently degrades the compression ratio, so this parameter 69 should be set only when necessary (in interactive applications). Some 70 output may be provided even if flush is not set. 71 </li> 72</ul></div> 73<p> 74 Before the call, the application must ensure that at least one of the actions 75 is possible, by providing more input and/or consuming more output, and 76 updating <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_in</span></code> or <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_out</span></code> 77 accordingly; <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_out</span></code> should never be zero before 78 the call. The application can consume the compressed output when it wants, 79 for example when the output buffer is full (<code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_out</span> 80 <span class="special">==</span> <span class="number">0</span></code>), 81 or after each call of <code class="computeroutput"><span class="identifier">write</span></code>. 82 If <code class="computeroutput"><span class="identifier">write</span></code> returns no error 83 with zero <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_out</span></code>, it must be called again after 84 making room in the output buffer because there might be more output pending. 85 Normally the parameter flush is set to <a class="link" href="../boost__beast__zlib__Flush.html" title="zlib::Flush"><code class="computeroutput"><span class="identifier">Flush</span><span class="special">::</span><span class="identifier">none</span></code></a>, which allows deflate to 86 decide how much data to accumulate before producing output, in order to 87 maximize compression. If the parameter flush is set to <a class="link" href="../boost__beast__zlib__Flush.html" title="zlib::Flush"><code class="computeroutput"><span class="identifier">Flush</span><span class="special">::</span><span class="identifier">sync</span></code></a>, all pending output is flushed 88 to the output buffer and the output is aligned on a byte boundary, so that 89 the decompressor can get all input data available so far. In particular 90 <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_in</span></code> is zero after the call if enough 91 output space has been provided before the call. Flushing may degrade compression 92 for some compression algorithms and so it should be used only when necessary. 93 This completes the current deflate block and follows it with an empty stored 94 block that is three bits plus filler bits to the next byte, followed by 95 the four bytes <code class="computeroutput"><span class="special">{</span> <span class="number">0x00</span><span class="special">,</span> <span class="number">0x00</span> <span class="number">0xff</span> 96 <span class="number">0xff</span> <span class="special">}</span></code>. 97 If flush is set to <a class="link" href="../boost__beast__zlib__Flush.html" title="zlib::Flush"><code class="computeroutput"><span class="identifier">Flush</span><span class="special">::</span><span class="identifier">partial</span></code></a>, all pending output is 98 flushed to the output buffer, but the output is not aligned to a byte boundary. 99 All of the input data so far will be available to the decompressor, as 100 for Z_SYNC_FLUSH. This completes the current deflate block and follows 101 it with an empty fixed codes block that is 10 bits long. This assures that 102 enough bytes are output in order for the decompressor to finish the block 103 before the empty fixed code block. If flush is set to <a class="link" href="../boost__beast__zlib__Flush.html" title="zlib::Flush"><code class="computeroutput"><span class="identifier">Flush</span><span class="special">::</span><span class="identifier">block</span></code></a>, a deflate block is completed 104 and emitted, as for <a class="link" href="../boost__beast__zlib__Flush.html" title="zlib::Flush"><code class="computeroutput"><span class="identifier">Flush</span><span class="special">::</span><span class="identifier">sync</span></code></a>, but the output is not aligned 105 on a byte boundary, and up to seven bits of the current block are held 106 to be written as the next byte after the next deflate block is completed. 107 In this case, the decompressor may not be provided enough bits at this 108 point in order to complete decompression of the data provided so far to 109 the compressor. It may need to wait for the next block to be emitted. This 110 is for advanced applications that need to control the emission of deflate 111 blocks. If flush is set to <a class="link" href="../boost__beast__zlib__Flush.html" title="zlib::Flush"><code class="computeroutput"><span class="identifier">Flush</span><span class="special">::</span><span class="identifier">full</span></code></a>, all output is flushed as 112 with <a class="link" href="../boost__beast__zlib__Flush.html" title="zlib::Flush"><code class="computeroutput"><span class="identifier">Flush</span><span class="special">::</span><span class="identifier">sync</span></code></a>, and the compression state 113 is reset so that decompression can restart from this point if previous 114 compressed data has been damaged or if random access is desired. Using 115 <a class="link" href="../boost__beast__zlib__Flush.html" title="zlib::Flush"><code class="computeroutput"><span class="identifier">Flush</span><span class="special">::</span><span class="identifier">full</span></code></a> 116 too often can seriously degrade compression. If <code class="computeroutput"><span class="identifier">write</span></code> 117 returns with <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_out</span> <span class="special">==</span> 118 <span class="number">0</span></code>, this function must be called again 119 with the same value of the flush parameter and more output space (updated 120 <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_out</span></code>), until the flush is complete 121 (<code class="computeroutput"><span class="identifier">write</span></code> returns with non-zero 122 <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_out</span></code>). In the case of a <a class="link" href="../boost__beast__zlib__Flush.html" title="zlib::Flush"><code class="computeroutput"><span class="identifier">Flush</span><span class="special">::</span><span class="identifier">full</span></code></a>or <a class="link" href="../boost__beast__zlib__Flush.html" title="zlib::Flush"><code class="computeroutput"><span class="identifier">Flush</span><span class="special">::</span><span class="identifier">sync</span></code></a>, make sure that <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_out</span></code> 123 is greater than six to avoid repeated flush markers due to <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_out</span> 124 <span class="special">==</span> <span class="number">0</span></code> 125 on return. If the parameter flush is set to <a class="link" href="../boost__beast__zlib__Flush.html" title="zlib::Flush"><code class="computeroutput"><span class="identifier">Flush</span><span class="special">::</span><span class="identifier">finish</span></code></a>, pending input is processed, 126 pending output is flushed and deflate returns the error <a class="link" href="../boost__beast__zlib__error.html" title="zlib::error"><code class="computeroutput"><span class="identifier">error</span><span class="special">::</span><span class="identifier">end_of_stream</span></code></a> if there was enough 127 output space; if deflate returns with no error, this function must be called 128 again with <a class="link" href="../boost__beast__zlib__Flush.html" title="zlib::Flush"><code class="computeroutput"><span class="identifier">Flush</span><span class="special">::</span><span class="identifier">finish</span></code></a> and more output space (updated 129 <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_out</span></code>) but no more input data, until 130 it returns the error <a class="link" href="../boost__beast__zlib__error.html" title="zlib::error"><code class="computeroutput"><span class="identifier">error</span><span class="special">::</span><span class="identifier">end_of_stream</span></code></a> or another error. 131 After <code class="computeroutput"><span class="identifier">write</span></code> has returned 132 the <a class="link" href="../boost__beast__zlib__error.html" title="zlib::error"><code class="computeroutput"><span class="identifier">error</span><span class="special">::</span><span class="identifier">end_of_stream</span></code></a> 133 error, the only possible operations on the stream are to reset or destroy. 134 <a class="link" href="../boost__beast__zlib__Flush.html" title="zlib::Flush"><code class="computeroutput"><span class="identifier">Flush</span><span class="special">::</span><span class="identifier">finish</span></code></a> 135 can be used immediately after initialization if all the compression is 136 to be done in a single step. In this case, <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_out</span></code> 137 must be at least value returned by <code class="computeroutput"><span class="identifier">upper_bound</span></code> 138 (see below). Then <code class="computeroutput"><span class="identifier">write</span></code> 139 is guaranteed to return the <a class="link" href="../boost__beast__zlib__error.html" title="zlib::error"><code class="computeroutput"><span class="identifier">error</span><span class="special">::</span><span class="identifier">end_of_stream</span></code></a> error. If not enough 140 output space is provided, deflate will not return <a class="link" href="../boost__beast__zlib__error.html" title="zlib::error"><code class="computeroutput"><span class="identifier">error</span><span class="special">::</span><span class="identifier">end_of_stream</span></code></a>, and it must be 141 called again as described above. <code class="computeroutput"><span class="identifier">write</span></code> 142 returns no error if some progress has been made (more input processed or 143 more output produced), <a class="link" href="../boost__beast__zlib__error.html" title="zlib::error"><code class="computeroutput"><span class="identifier">error</span><span class="special">::</span><span class="identifier">end_of_stream</span></code></a> if all input has 144 been consumed and all output has been produced (only when flush is set 145 to <a class="link" href="../boost__beast__zlib__Flush.html" title="zlib::Flush"><code class="computeroutput"><span class="identifier">Flush</span><span class="special">::</span><span class="identifier">finish</span></code></a>), 146 <a class="link" href="../boost__beast__zlib__error.html" title="zlib::error"><code class="computeroutput"><span class="identifier">error</span><span class="special">::</span><span class="identifier">stream_error</span></code></a> 147 if the stream state was inconsistent (for example if <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">next_in</span></code> 148 or <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">next_out</span></code> was <code class="computeroutput"><span class="keyword">nullptr</span></code>), 149 <a class="link" href="../boost__beast__zlib__error.html" title="zlib::error"><code class="computeroutput"><span class="identifier">error</span><span class="special">::</span><span class="identifier">need_buffers</span></code></a> 150 if no progress is possible (for example <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_in</span></code> 151 or <code class="computeroutput"><span class="identifier">zs</span><span class="special">.</span><span class="identifier">avail_out</span></code> was zero). Note that <a class="link" href="../boost__beast__zlib__error.html" title="zlib::error"><code class="computeroutput"><span class="identifier">error</span><span class="special">::</span><span class="identifier">need_buffers</span></code></a> 152 is not fatal, and <code class="computeroutput"><span class="identifier">write</span></code> 153 can be called again with more input and more output space to continue compressing. 154 </p> 155</div> 156<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 157<td align="left"></td> 158<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie 159 Falco<p> 160 Distributed under the Boost Software License, Version 1.0. (See accompanying 161 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 162 </p> 163</div></td> 164</tr></table> 165<hr> 166<div class="spirit-nav"> 167<a accesskey="p" href="tune.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost__beast__zlib__deflate_stream.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="params.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a> 168</div> 169</body> 170</html> 171