ShellLogOutputStream.java 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package cn.exlive.monitor.utils;
  2. import org.apache.commons.lang3.SystemUtils;
  3. import java.io.Closeable;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.nio.charset.Charset;
  7. import java.nio.charset.StandardCharsets;
  8. /**
  9. * 收集日志
  10. *
  11. * <pre>
  12. *
  13. * Created by zhaopx.
  14. * Date: 2025/6/9
  15. * Time: 14:06
  16. * Vendor: exlive.cn
  17. *
  18. * </pre>
  19. *
  20. * @author zhaopx
  21. */
  22. public class ShellLogOutputStream extends OutputStream implements Closeable {
  23. final StringBuffer lines = new StringBuffer();
  24. /**
  25. * 系统编码
  26. */
  27. Charset charset = SystemUtils.IS_OS_LINUX ? StandardCharsets.UTF_8 : Charset.forName("GBK");
  28. @Override
  29. public void write(int b) throws IOException {
  30. }
  31. /**
  32. * write one line
  33. * @param line one line
  34. * @return
  35. * @throws IOException
  36. */
  37. public void write(String line) throws IOException {
  38. lines.append(line);
  39. }
  40. @Override
  41. public void write(byte[] b, int off, int len) throws IOException {
  42. String line = new String(b, off, len, charset);
  43. write(line);
  44. }
  45. @Override
  46. public void flush() throws IOException {
  47. }
  48. @Override
  49. public void close() throws IOException {
  50. }
  51. @Override
  52. public String toString() {
  53. return lines.toString();
  54. }
  55. public Charset getCharset() {
  56. return charset;
  57. }
  58. public void setCharset(Charset charset) {
  59. this.charset = charset;
  60. }
  61. }