View Javadoc
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  
18  import java.io.BufferedReader;
19  import java.io.InputStream;
20  import java.io.InputStreamReader;
21  import java.io.UnsupportedEncodingException;
22  import java.util.LinkedList;
23  import java.util.List;
24  import java.util.function.Consumer;
25  
26  import org.apache.logging.log4j.LogManager;
27  import org.apache.logging.log4j.Logger;
28  import org.codelibs.fess.exception.FessSystemException;
29  
30  public class InputStreamThread extends Thread {
31      private static final Logger logger = LogManager.getLogger(InputStreamThread.class);
32  
33      private BufferedReader br;
34  
35      public static final int MAX_BUFFER_SIZE = 1000;
36  
37      private final List<String> list = new LinkedList<>();
38  
39      private final int bufferSize;
40  
41      private final Consumer<String> outputCallback;
42  
43      public InputStreamThread(final InputStream is, final String charset) {
44          this(is, charset, MAX_BUFFER_SIZE, null);
45      }
46  
47      public InputStreamThread(final InputStream is, final String charset, final int bufferSize, final Consumer<String> outputCallback) {
48          super("InputStreamThread");
49          this.bufferSize = bufferSize;
50          this.outputCallback = outputCallback;
51  
52          try {
53              br = new BufferedReader(new InputStreamReader(is, charset));
54          } catch (final UnsupportedEncodingException e) {
55              throw new FessSystemException(e);
56          }
57      }
58  
59      @Override
60      public void run() {
61          boolean running = true;
62          while (running) {
63              try {
64                  final String line = br.readLine();
65                  if (line == null) {
66                      running = false;
67                  } else {
68                      if (logger.isDebugEnabled()) {
69                          logger.debug(line);
70                      }
71                      list.add(line);
72                      if (outputCallback != null) {
73                          outputCallback.accept(line);
74                      }
75                      if (list.size() > bufferSize) {
76                          list.remove(0);
77                      }
78                  }
79              } catch (final Exception e) {
80                  running = false;
81                  if (logger.isDebugEnabled()) {
82                      logger.debug("Failed to process an input stream.", e);
83                  }
84              }
85          }
86      }
87  
88      public String getOutput() {
89          final StringBuilder buf = new StringBuilder(100);
90          for (final String value : list) {
91              buf.append(value).append("\n");
92          }
93          return buf.toString();
94      }
95  
96      public boolean contains(final String value) {
97          for (final String line : list) {
98              if (line.trim().equals(value)) {
99                  return true;
100             }
101         }
102         return false;
103     }
104 }