/* (non-Javadoc)
@see EncodingStateRegistry#getEncodingStateFor(java.lang.CharSequence)
555054
public EncodingState getEncodingStateFor(CharSequence string) {
        int identityHashCode = System.identityHashCode(string);
        Set<Encoder> result = null;
        for (Map.Entry<Encoder, Set<Integer>> entry : encodingTagIdentityHashCodes.entrySet()) {
            if (entry.getValue().contains(identityHashCode)) {
                if (result == null) {
                    result = Collections.singleton(entry.getKey());
                }
                else {
                    if (result.size() == 1) {
                        result = new HashSet<Encoder>(result);
                    }
                    result.add(entry.getKey());
                }
            }
        }
        return result != null ? new EncodingStateImpl(result, null) : EncodingStateImpl.UNDEFINED_ENCODING_STATE;
    }
461914
@Trivial
    @Override
    public String getCharacterEncoding() {
        if (null != this.encoding) {
            return this.encoding;
        }

        EncodingUtils encodingUtils = connection.getEncodingUtils();

        String enc = null;
        // parse content-type to see if it's set there
        String type = this.request.getHeader("Content-Type");
        if (null != type) {
            enc = encodingUtils.getCharsetFromContentType(type);
            if (!encodingUtils.isCharsetSupported(enc)) {
                enc = null;
            }
        }

        // now get the appropriate match (may be set in the properties)
        this.encoding = encodingUtils.getJvmConverter(enc);

        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
            Tr.debug(tc, "getCharacterEncoding: set to " + this.encoding);
        }
        return this.encoding;
    }
398076
private static int getSingleCP(CharSequence s) {
        if (s.length() < 1) {
            throw new IllegalArgumentException("Can't use zero-length strings in UnicodeSet");
        }
        if (s.length() > 2) return -1;
        if (s.length() == 1) return s.charAt(0);

        // at this point, len = 2
        int cp = UTF16.charAt(s, 0);
        if (cp > 0xFFFF) { // is surrogate pair
            return cp;
        }
        return -1;
    }
651536
@Override
    public void setCharacterEncoding(String charset) {
        if (null != this.outWriter || isCommitted()) {
            return;
        }
        if (null != charset) {
            this.encoding = connection.getEncodingUtils().stripQuotes(charset);
        }
        if (null != this.contentType) {
            int index = this.contentType.indexOf("charset=");
            StringBuilder sb = new StringBuilder();
            if (-1 != index) {
                sb.append(this.contentType.substring(0, index - 1).trim());
            } else {
                sb.append(this.contentType);
            }
            if (this.encoding != null) {
                sb.append(";charset=").append(this.encoding);
            }

            this.contentType = sb.toString();
            this.response.setHeader("Content-Type", this.contentType);
        }
    }
1292075
protected String getEncoding(String content) {

        String encoding = content;
        int index = encoding.toLowerCase().indexOf("encoding=\"");
        // encoding attribute found, get the value
        if (index != -1) {
            encoding = encoding.substring(index + 10);
            index = encoding.indexOf("\"");
            if (index != -1) {
                encoding = encoding.substring(0, index);
                return encoding.toUpperCase();
            }
        }
        // no encoding attribute found
        return "";
    }
1176626
@Override
    public String getEncodingFromLocale(Locale locale) {
        if (null == locale) {
            return null;
        }
        if (locale.equals(this.cachedLocale)) {
            return this.cachedEncoding;
        }

        String encoding = localeMap.get(locale.toString());
        if (null == encoding) {
            final String lang = locale.getLanguage();
            encoding = localeMap.get(lang + '_' + locale.getCountry());
            if (null == encoding) {
                encoding = localeMap.get(lang);
            }
        }

        this.cachedEncoding = encoding;
        this.cachedLocale = locale;

        return encoding;
    }
577064
public int number(CharSequence seq) {
        StateInfo info = getStateInfo(seq);
        return info.isInFinalState() ? info.getHash() : -1;
    }
699926
public void setCharacterEncoding(String encoding)
        throws UnsupportedEncodingException
    {
        if (_inputState!=0)
            throw new IllegalStateException("getReader() or getInputStream() called");
        "".getBytes(encoding);
        _httpRequest.setCharacterEncoding(encoding,false);
    }
471666
public final int readChar() throws IOException
  {
    if (_readEncoding != null) {
      int ch = _readEncoding.read();
      return ch;
    }

    if (_readLength <= _readOffset) {
      if (! readBuffer())
        return -1;
    }

    return _readBuffer[_readOffset++] & 0xff;
  }
666957
public boolean shouldEncodeWith(Encoder encoderToApply, CharSequence string) {
        if(isNoneEncoder(encoderToApply)) return false;
        EncodingState encodingState = getEncodingStateFor(string);
        return shouldEncodeWith(encoderToApply, encodingState);
    }
1022521
private static CharSequence decode0(final CharSequence s) {
    final int length = s.length();
    final CharBuffer cb = CharBuffer.allocate(length);

    for (int i = 0; i < length; ) {
      final char c = s.charAt(i);

      // Not encoded?
      if (c != '%') {
        cb.append(c);
        i++;
        continue;
      }

      // UTF8 - 1 Byte
      int b1 = decodePercent(s, length, i);
      if (b1 == INVALID) {
        return null;
      }
      i += 3;
      final int n = utf8Length(b1);
      if (n == INVALID) {
        return null;
      }
      if (n == 1) {
        cb.append((char) b1);
        continue;
      }

      // UTF8 - 2 Bytes
      final int b2 = decodePercent(s, length, i);
      if (b2 == INVALID) {
        return null;
      }
      i += 3;
      if (n == 2) {
        final int cp = utf8Read2(b1, b2);
        if (cp == INVALID) {
          return null;
        }
        cb.append((char) cp);
        continue;
      }

      // UTF8 - 3 Bytes
      final int b3 = decodePercent(s, length, i);
      if (b3 == INVALID) {
        return null;
      }
      i += 3;
      if (n == 3) {
        final int cp = utf8Read3(b1, b2, b3);
        if (cp == INVALID) {
          return null;
        }
        cb.append((char) cp);
        continue;
      }

      // UTF8 - 4 Bytes
      final int b4 = decodePercent(s, length, i);
      if (b4 == INVALID) {
        return null;
      }
      i += 3;
      final int cp = utf8Read4(b1, b2, b3, b4);
      if (cp == INVALID) {
        return null;
      }
      final int offset = cp - MIN_SUPPLEMENTARY_CODE_POINT;
      cb.append((char) ((offset >>> 10) + MIN_HIGH_SURROGATE));
      cb.append((char) ((offset & 0x3ff) + MIN_LOW_SURROGATE));
    }

    cb.flip();
    return cb;
  }
539272
@Deprecated
    public static int getSingleCodePoint(CharSequence s) {
        int length = s.length();
        if (length < 1 || length > 2) {
            return Integer.MAX_VALUE;
        }
        int result = Character.codePointAt(s, 0);
        return (result < 0x10000) == (length == 1) ? result : Integer.MAX_VALUE;
    }
293366
final Integer getEncoding() {
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            SibTr.entry(this, tc, "getEncoding");

        Integer value = (Integer) jmo.getPayloadPart().getField(JsPayloadAccess.ENCODING_DATA);

        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            SibTr.exit(this, tc, "getEncoding", value);
        return value;
    }
354358
public static String charset(final String encoding)
    {
        final String charsetName = STD_CHARSETS.get(encoding);
        if (charsetName != null)
        {
            return "java.nio.charset.StandardCharsets." + charsetName;
        }
        else
        {
            return "java.nio.charset.Charset.forName(\"" + encoding + "\")";
        }
    }
89521
public synchronized String encode(String charset, boolean equalsForNullValue)
    {
        if (charset==null)
            charset=StringUtil.__ISO_8859_1;
        
        StringBuffer result = new StringBuffer(128);
        synchronized(result)
        {
            Iterator iter = entrySet().iterator();
            while(iter.hasNext())
            {
                Map.Entry entry = (Map.Entry)iter.next();
                
                String key = entry.getKey().toString();
                Object list = entry.getValue();
                int s=LazyList.size(list);
                
                if (s==0)
                {
                    result.append(encodeString(key,charset));
                    if(equalsForNullValue)
                        result.append('=');
                }
                else
                {
                    for (int i=0;i<s;i++)
                    {
                        if (i>0)
                            result.append('&');
                        Object val=LazyList.get(list,i);
                        result.append(encodeString(key,charset));

                        if (val!=null)
                        {
                            String str=val.toString();
                            if (str.length()>0)
                            {
                                result.append('=');
                                result.append(encodeString(str,charset));
                            }
                            else if (equalsForNullValue)
                                result.append('=');
                        }
                        else if (equalsForNullValue)
                            result.append('=');
                    }
                }
                if (iter.hasNext())
                    result.append('&');
            }
            return result.toString();
        }
    }
5851
public static byte[] decodeToBytes(CharSequence in) throws IOException{
		if(in == null) return null;
		ByteArrayOutputStream baos = new ByteArrayOutputStream(in.length());
		decode(in, baos);
		return baos.toByteArray();
	}
287562
@Override
  public int encodedLength(CharSequence in) {
    final Cache cache = CACHE.get();
    int estimated = (int) (in.length() * cache.encoder.maxBytesPerChar());
    if (cache.lastOutput == null || cache.lastOutput.capacity() < estimated) {
      cache.lastOutput = ByteBuffer.allocate(Math.max(128, estimated));
    }
    cache.lastOutput.clear();
    cache.lastInput = in;
    CharBuffer wrap = (in instanceof CharBuffer) ?
                          (CharBuffer) in : CharBuffer.wrap(in);
    CoderResult result = cache.encoder.encode(wrap, cache.lastOutput, true);
    if (result.isError()) {
      try {
        result.throwException();
      } catch (CharacterCodingException e) {
        throw new IllegalArgumentException("bad character encoding", e);
      }
    }
    return cache.lastOutput.remaining();
  }