Skip to content

Instantly share code, notes, and snippets.

@bugparty
Created June 29, 2017 07:44
Show Gist options
  • Save bugparty/6d1f1084485d61eb91b616b7cd14e023 to your computer and use it in GitHub Desktop.
Save bugparty/6d1f1084485d61eb91b616b7cd14e023 to your computer and use it in GitHub Desktop.
Write Exception object to string
import android.support.annotation.NonNull;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* Created by bowman_apple on 2017/5/22.
*/
public class ExceptionWriter{
ByteArrayOutputStream baos = new ByteArrayOutputStream(128);
public PrintWriter getPrintWriter() {
return new PrintWriter(baos);
}
public void append(CharSequence cs){
PrintWriter pw = new PrintWriter(baos).append(cs);
pw.flush();
pw.close();
}
public void append(String str) {
append((CharSequence) str);
}
public String toString(){
try {
return baos.toString("UTF-8");
} catch (UnsupportedEncodingException e) {
return "UnsupportedEncodingException" + e.getMessage();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment