title: Java: String.getBytes(Charset) Vs. Charset.encode(String) for use with OutputStream
body: <p>I am in the situation that my algorithm has the 2 inputs of:</p>
  
  <ul>
  <li>1 utf8 String object that will be encoded </li>
  <li>1 Charset object which indicates what I need to encode the string into</li>
  </ul>
  
  <p>In the end, the returned result will be put into an OutputStream, an action which may happen multiple times, but at least once. There is no multithreading happening in this scenario.</p>
  
  <p>I have found two solutions:</p>
  
  <ol>
  <li>Calling getBytes(Charset) on the given String and supply the given Charset. This will return a byte[]</li>
  <li>Calling encode(String) on the given Charset and supply the given String. This will return a ByteBuffer.</li>
  </ol>
  
  <p>Delving into the code behind these methods shows a complex design for each underlying algorithm. I can't say I understand how to make a choice between these two options.</p>
  
  <ol>
  <li>Is there a significant performance difference for calling either method?</li>
  <li>Is there a significant performance difference for putting the result into the OutputStream?</li>
  <li>Is there a significant difference in footprint?</li>
  </ol>
  
  <p>Which solution would generally be a better choice?</p>