InputStreamThread.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2012-2021 CodeLibs Project and the Others.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  13. * either express or implied. See the License for the specific language
  14. * governing permissions and limitations under the License.
  15. */
  16. package org.codelibs.fess.util;
  17. import java.io.BufferedReader;
  18. import java.io.InputStream;
  19. import java.io.InputStreamReader;
  20. import java.nio.charset.Charset;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. import java.util.function.Consumer;
  24. import org.apache.logging.log4j.LogManager;
  25. import org.apache.logging.log4j.Logger;
  26. public class InputStreamThread extends Thread {
  27. private static final Logger logger = LogManager.getLogger(InputStreamThread.class);
  28. private final BufferedReader br;
  29. public static final int MAX_BUFFER_SIZE = 1000;
  30. private final List<String> list = new LinkedList<>();
  31. private final int bufferSize;
  32. private final Consumer<String> outputCallback;
  33. @Deprecated
  34. public InputStreamThread(final InputStream is, final String charset) {
  35. this(is, Charset.forName(charset), MAX_BUFFER_SIZE, null);
  36. }
  37. public InputStreamThread(final InputStream is, final Charset charset, final int bufferSize, final Consumer<String> outputCallback) {
  38. super("InputStreamThread");
  39. this.bufferSize = bufferSize;
  40. this.outputCallback = outputCallback;
  41. br = new BufferedReader(new InputStreamReader(is, charset));
  42. }
  43. @Override
  44. public void run() {
  45. boolean running = true;
  46. while (running) {
  47. try {
  48. final String line = br.readLine();
  49. if (line == null) {
  50. running = false;
  51. } else {
  52. if (logger.isDebugEnabled()) {
  53. logger.debug(line);
  54. }
  55. if (bufferSize > 0) {
  56. list.add(line);
  57. }
  58. if (outputCallback != null) {
  59. outputCallback.accept(line);
  60. }
  61. if (list.size() > bufferSize) {
  62. list.remove(0);
  63. }
  64. }
  65. } catch (final Exception e) {
  66. running = false;
  67. if (logger.isDebugEnabled()) {
  68. logger.debug("Failed to process an input stream.", e);
  69. }
  70. }
  71. }
  72. }
  73. public String getOutput() {
  74. final StringBuilder buf = new StringBuilder(100);
  75. for (final String value : list) {
  76. buf.append(value).append("\n");
  77. }
  78. return buf.toString();
  79. }
  80. public boolean contains(final String value) {
  81. for (final String line : list) {
  82. if (line.trim().equals(value)) {
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. }