return out;
byte[] resized = new byte[out.remaining()]; out.get(resized); return ByteBuffer.wrap(resized);
  public final ByteBuffer encode (CharBuffer in)    throws CharacterCodingException  {    // XXX: Sun's Javadoc seems to contradict itself saying an    // IllegalStateException is thrown "if a decoding operation is already    // in progress" and also that "it resets this Encoder".    // Should we check to see that the state is reset, or should we    // call reset()?    if (state != STATE_RESET)      throw new IllegalStateException ();    // REVIEW: Using max instead of average may allocate a very large    // buffer.  Maybe we should do something more efficient?    int remaining = in.remaining ();    int n = (int) (remaining * maxBytesPerChar ());    ByteBuffer out = ByteBuffer.allocate (n);    if (remaining == 0)      {        state = STATE_FLUSHED;        return out;      }    CoderResult cr = encode (in, out, true);    if (cr.isError ())      cr.throwException ();    cr = flush (out);    if (cr.isError ())      cr.throwException ();    out.flip ();    return out;  }