code: public final ByteBuffer encode(CharBuffer in)
          throws CharacterCodingException
      {
          int n = (int)(in.remaining() * averageBytesPerChar());
          ByteBuffer out = ByteBuffer.allocate(n);
  
          if ((n == 0) && (in.remaining() == 0))
              return out;
          reset();
          for (;;) {
              CoderResult cr = in.hasRemaining() ?
                  encode(in, out, true) : CoderResult.UNDERFLOW;
              if (cr.isUnderflow())
                  cr = flush(out);
  
              if (cr.isUnderflow())
                  break;
              if (cr.isOverflow()) {
                  n = 2*n + 1;    // Ensure progress; n might be 0!
                  ByteBuffer o = ByteBuffer.allocate(n);
                  out.flip();
                  o.put(out);
                  out = o;
                  continue;
              }
              cr.throwException();
          }
          out.flip();
          return out;
      }
docs: Convenience method that encodes the remaining content of a single input
  character buffer into a newly-allocated byte buffer.
  
  <p> This method implements an entire <a href="#steps">encoding
  operation</a>; that is, it resets this encoder, then it encodes the
  characters in the given character buffer, and finally it flushes this
  encoder.  This method should therefore not be invoked if an encoding
  operation is already in progress.  </p>
  
  @param  in
  The input character buffer
  
  @return A newly-allocated byte buffer containing the result of the
  encoding operation.  The buffer's position will be zero and its
  limit will follow the last byte written.
  
  @throws  IllegalStateException
  If an encoding operation is already in progress
  
  @throws  MalformedInputException
  If the character sequence starting at the input buffer's current
  position is not a legal sixteen-bit Unicode sequence and the current malformed-input action
  is {@link CodingErrorAction#REPORT}
  
  @throws  UnmappableCharacterException
  If the character sequence starting at the input buffer's current
  position cannot be mapped to an equivalent byte sequence and
  the current unmappable-character action is {@link
  CodingErrorAction#REPORT}