code: public static byte[] encode(String s, Charset charset)
      {
          CharsetEncoder encoder = charset.newEncoder();
          try
          {
              ByteBuffer buffer = encoder.encode(CharBuffer.wrap(s));
              byte[] bytes = buffer.array();
  
              // Make another copy iff there's garbage after the limit.
              int limit = buffer.limit();
              if (limit < bytes.length)
              {
                  bytes = copyOf(bytes, limit);
              }
  
              return bytes;
          }
          catch (CharacterCodingException e)
          {
              throw new IonException("Invalid string data", e);
          }
      }
docs: Encodes a String into bytes of a given encoding.
  <p>
  This method is preferred to {@link Charset#encode(String)} and
  {@link String#getBytes(String)} (<em>etc.</em>)
  since those methods will replace or ignore bad input, and here we throw
  an exception.
  
  @param s the string to encode.
  
  @return the encoded string, not null.
  
  @throws IonException if there's a {@link CharacterCodingException}.