public static Charset parseEncoding(byte[] b) {String enc = parseEncodingName(b);if (enc == null) {return UTF_8;}String name = enc.trim();try {return Charset.forName(name);} catch (IllegalCharsetNameException| UnsupportedCharsetException badName) {Charset aliased = charsetForAlias(name);if (aliased != null) {return aliased;}throw badName;}}

public static System.Text.Encoding ParseEncoding(byte[] b){int enc = Encoding(b, 0);if (enc < 0){return Constants.CHARSET;}int lf = NextLF(b, enc);string decoded = Decode(Constants.CHARSET, b, enc, lf - 1);try{return Sharpen.Extensions.GetEncoding(decoded);}catch (IllegalCharsetNameException badName){System.Text.Encoding aliased = CharsetForAlias(decoded);if (aliased != null){return aliased;}throw;}catch (UnsupportedCharsetException badName){System.Text.Encoding aliased = CharsetForAlias(decoded);if (aliased != null){return aliased;}throw;}}
