View Javadoc
1   /*
2    * Copyright 2012-2017 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  
25  import org.codelibs.fess.exception.FessSystemException;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  public class InputStreamThread extends Thread {
30      private static final Logger logger = LoggerFactory.getLogger(InputStreamThread.class);
31  
32      private BufferedReader br;
33  
34      private static final int MAX_BUFFER_SIZE = 1000;
35  
36      private final List<String> list = new LinkedList<>();
37  
38      public InputStreamThread(final InputStream is, final String charset) {
39          super("InputStreamThread");
40  
41          try {
42              br = new BufferedReader(new InputStreamReader(is, charset));
43          } catch (final UnsupportedEncodingException e) {
44              throw new FessSystemException(e);
45          }
46      }
47  
48      @Override
49      public void run() {
50          boolean running = true;
51          while (running) {
52              try {
53                  final String line = br.readLine();
54                  if (line == null) {
55                      running = false;
56                  } else {
57                      if (logger.isDebugEnabled()) {
58                          logger.debug(line);
59                      }
60                      list.add(line);
61                      if (list.size() > MAX_BUFFER_SIZE) {
62                          list.remove(0);
63                      }
64                  }
65              } catch (final Exception e) {
66                  running = false;
67                  logger.error("Failed to process an input stream.", e);
68              }
69          }
70      }
71  
72      public String getOutput() {
73          final StringBuilder buf = new StringBuilder(100);
74          for (final String value : list) {
75              buf.append(value).append("\n");
76          }
77          return buf.toString();
78      }
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  }