BlackHolePrintStreamBuilder.java 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package cn.com.qmth.examcloud.commons.helpers;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.PrintStream;
  4. /**
  5. * 黑洞(PrintStream)构建器
  6. *
  7. * @author WANGWEI
  8. * @date 2019年3月28日
  9. * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
  10. */
  11. public class BlackHolePrintStreamBuilder {
  12. /**
  13. * 创建BlackHolePrintStream
  14. *
  15. * @author WANGWEI
  16. * @return
  17. */
  18. public static PrintStream build() {
  19. return new BlackHolePrintStream(new ByteArrayOutputStream());
  20. }
  21. /**
  22. * 黑洞(PrintStream)
  23. *
  24. * @author WANGWEI
  25. * @date 2019年3月28日
  26. * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
  27. */
  28. private static class BlackHolePrintStream extends PrintStream {
  29. @Override
  30. public void close() {
  31. super.close();
  32. }
  33. ByteArrayOutputStream outputStream;
  34. /**
  35. * 私有构造函数
  36. *
  37. * @param out
  38. */
  39. private BlackHolePrintStream(ByteArrayOutputStream outputStream) {
  40. super(outputStream);
  41. this.outputStream = outputStream;
  42. }
  43. @Override
  44. public void println(String x) {
  45. outputStream.reset();
  46. }
  47. @Override
  48. public void print(String x) {
  49. outputStream.reset();
  50. }
  51. }
  52. }