title: How do I get `Data` objects into Swift-NIO without making copies?
body: <p>I'm fairly new to Swift and <em>very</em> new to NIO.</p>
  <p>I'm adding Swift code to a large project that needs to up/down load a lot of data (GBs) to AWS. To that end, I've imported the GitHub project <a href="https://github.com/soto-project/soto" rel="nofollow noreferrer">Soto</a>, which relies heavily on NIO.</p>
  <p>Most methods that send/receive data do so through <code>ByteBuffer</code> structs. My application already has the data to upload in Foundation <code>Data</code> objects. I'm having trouble figuring out the best way to get these <code>Data</code> objects into NIO.</p>
  <p>In the documentation for <a href="https://apple.github.io/swift-nio/docs/current/NIO/Structs/ByteBuffer.html#" rel="nofollow noreferrer">NIO's <code>ByteBuffer</code> (2.26.0)</a> it states</p>
  <p><em>Supported types:
  A variety of types can be read/written from/to a ByteBuffer. ... Out of the box, ByteBuffer supports for example the following types (non-exhaustive list):</em></p>
  <ul>
  <li><em>String/StaticString</em></li>
  <li><em>Swift’s various (unsigned) integer types</em></li>
  <li><em><strong>Foundation‘s Data</strong></em></li>
  <li><em>[UInt8] and generally any Collection of UInt8</em></li>
  </ul>
  <p>However, the latest swift-nil package has no <code>ByteBuffer</code> support for Foundation <code>Data</code> objects. Instead, it supports <code>DispatchData</code> objects, which in turn seem to have no interoperability with <code>Data</code> objects.</p>
  <p>What I want to avoid is making a copy of every block of data (100's of MB at a time), just to convert between <code>Data</code> and <code>DispatchData</code> types.</p>
  <p>So...</p>
  <p>Right now my thinking is one of</p>
  <ul>
  <li><p>I'm completely lost, and there's a simple solution I haven't found</p>
  </li>
  <li><p>The solution is to create a subclass of <code>DispatchData</code> backed by a <code>Data</code> object</p>
  </li>
  <li><p>Initialize the <code>ByteBuffer</code> structure using a <code>DispatchData</code> created using the no-copy initializer pointing to the raw byte array in the <code>Data</code> object, along with a custom deallocator that simply retains the <code>Data</code> object until the <code>ByteBuffer</code> and <code>DispatchData</code> objects are destroyed.</p>
  </li>
  </ul>
  <p>I would appreciate any thoughts, experience, or suggestions (particularly if it's option #1).</p>