Charset charset = null;
  private static int writeString(OutputStream out, int tag, String str)    throws IOException  {    Charset charset = null;    byte[] b = null;    switch (tag & 0x1F)      {        case NUMERIC_STRING:        case PRINTABLE_STRING:        case T61_STRING:        case VIDEOTEX_STRING:        case IA5_STRING:        case GRAPHIC_STRING:        case ISO646_STRING:        case GENERAL_STRING:          charset = Charset.forName("ISO-8859-1");          break;        case UNIVERSAL_STRING:        case BMP_STRING:          charset = Charset.forName("UTF-16BE");          break;        case UTF8_STRING:        default:          charset = Charset.forName("UTF-8");          break;      }    if (charset == null)      throw new DEREncodingException("no charset");    CharsetEncoder encoder = charset.newEncoder();    ByteBuffer result = encoder.encode(CharBuffer.wrap(str));    if (result.hasArray())      {        b = result.array();      }    else      {        b = new byte[result.remaining()];        result.get(b);      }    writeLength(out, b.length);    out.write(b);    return b.length;  }