public static string ToStackTrace(Exception error, int cutoff) { // default initial size is 32 chars CharArrayWriter buffer = new CharArrayWriter(8 * 1024); Sharpen.Runtime.PrintStackTrace(error, new PrintWriter(buffer)); return(buffer.Size() < cutoff?buffer.ToString() : Sharpen.Runtime.Substring(buffer .ToString(), 0, cutoff)); }
/// <summary> /// Translates a string into {@code application/x-www-form-urlencoded} /// format using a specific encoding scheme. This method uses the /// supplied encoding scheme to obtain the bytes for unsafe /// characters. /// <para> /// <em><strong>Note:</strong> The <a href= /// "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars"> /// World Wide Web Consortium Recommendation</a> states that /// UTF-8 should be used. Not doing so may introduce /// incompatibilities.</em> /// /// </para> /// </summary> /// <param name="s"> {@code String} to be translated. </param> /// <param name="enc"> The name of a supported /// <a href="../lang/package-summary.html#charenc">character /// encoding</a>. </param> /// <returns> the translated {@code String}. </returns> /// <exception cref="UnsupportedEncodingException"> /// If the named encoding is not supported </exception> /// <seealso cref= URLDecoder#decode(java.lang.String, java.lang.String) /// @since 1.4 </seealso> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public static String encode(String s, String enc) throws java.io.UnsupportedEncodingException public static String Encode(String s, String enc) { bool needToChange = false; StringBuffer @out = new StringBuffer(s.Length()); Charset charset; CharArrayWriter charArrayWriter = new CharArrayWriter(); if (enc == null) { throw new NullPointerException("charsetName"); } try { charset = Charset.ForName(enc); } catch (IllegalCharsetNameException) { throw new UnsupportedEncodingException(enc); } catch (UnsupportedCharsetException) { throw new UnsupportedEncodingException(enc); } for (int i = 0; i < s.Length();) { int c = (int)s.CharAt(i); //System.out.println("Examining character: " + c); if (DontNeedEncoding.Get(c)) { if (c == ' ') { c = '+'; needToChange = true; } //System.out.println("Storing: " + c); @out.Append((char)c); i++; } else { // convert to external encoding before hex conversion do { charArrayWriter.Write(c); /* * If this character represents the start of a Unicode * surrogate pair, then pass in two characters. It's not * clear what should be done if a bytes reserved in the * surrogate pairs range occurs outside of a legal * surrogate pair. For now, just treat it as if it were * any other character. */ if (c >= 0xD800 && c <= 0xDBFF) { /* * System.out.println(Integer.toHexString(c) + " is high surrogate"); */ if ((i + 1) < s.Length()) { int d = (int)s.CharAt(i + 1); /* * System.out.println("\tExamining " + Integer.toHexString(d)); */ if (d >= 0xDC00 && d <= 0xDFFF) { /* * System.out.println("\t" + Integer.toHexString(d) + " is low surrogate"); */ charArrayWriter.Write(d); i++; } } } i++; } while (i < s.Length() && !DontNeedEncoding.Get((c = (int)s.CharAt(i)))); charArrayWriter.Flush(); String str = new String(charArrayWriter.ToCharArray()); sbyte[] ba = str.GetBytes(charset); for (int j = 0; j < ba.Length; j++) { @out.Append('%'); char ch = Character.ForDigit((ba[j] >> 4) & 0xF, 16); // converting to use uppercase letter as part of // the hex value if ch is a letter. if (char.IsLetter(ch)) { ch -= CaseDiff; } @out.Append(ch); ch = Character.ForDigit(ba[j] & 0xF, 16); if (char.IsLetter(ch)) { ch -= CaseDiff; } @out.Append(ch); } charArrayWriter.Reset(); needToChange = true; } } return(needToChange? @out.ToString() : s); }