|
@@ -0,0 +1,113 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package com.dmdirc.util;
|
|
24
|
+
|
|
25
|
+import java.io.BufferedReader;
|
|
26
|
+import java.io.IOException;
|
|
27
|
+import java.io.InputStream;
|
|
28
|
+import java.io.InputStreamReader;
|
|
29
|
+import java.util.List;
|
|
30
|
+
|
|
31
|
+/**
|
|
32
|
+ * Simple stream reader to read a stream and save/discard the data.
|
|
33
|
+ */
|
|
34
|
+public class StreamReader extends Thread {
|
|
35
|
+
|
|
36
|
+ /** This is the Input Stream we are reading. */
|
|
37
|
+ private InputStream stream;
|
|
38
|
+
|
|
39
|
+ /** List to store output in. */
|
|
40
|
+ private List<String> list = null;
|
|
41
|
+
|
|
42
|
+ /** StringBuffer to store output in. */
|
|
43
|
+ private StringBuffer buffer = null;
|
|
44
|
+
|
|
45
|
+ /**
|
|
46
|
+ * Create a new Stream Reader that discards output.
|
|
47
|
+ *
|
|
48
|
+ * @param stream The stream to read
|
|
49
|
+ */
|
|
50
|
+ public StreamReader(final InputStream stream) {
|
|
51
|
+ super("StreamReader");
|
|
52
|
+
|
|
53
|
+ this.stream = stream;
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ /**
|
|
57
|
+ * Create a new Stream Reader that outputs to a List.
|
|
58
|
+ *
|
|
59
|
+ * @param stream The stream to read
|
|
60
|
+ * @param list The list to store the output from the stream in
|
|
61
|
+ */
|
|
62
|
+ public StreamReader(final InputStream stream, final List<String> list) {
|
|
63
|
+ super("StreamReader");
|
|
64
|
+
|
|
65
|
+ this.stream = stream;
|
|
66
|
+ this.list = list;
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ /**
|
|
70
|
+ * Create a new Stream Reader that outputs to a StringBuffer.
|
|
71
|
+ *
|
|
72
|
+ * @param stream The stream to read
|
|
73
|
+ * @param buffer The StringBuffer to store the output from the stream in
|
|
74
|
+ */
|
|
75
|
+ public StreamReader(final InputStream stream, final StringBuffer buffer) {
|
|
76
|
+ super("StreamReader");
|
|
77
|
+
|
|
78
|
+ this.stream = stream;
|
|
79
|
+ this.buffer = buffer;
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ /**
|
|
83
|
+ * Get the list that the output is being stored in.
|
|
84
|
+ *
|
|
85
|
+ * @return The output list
|
|
86
|
+ */
|
|
87
|
+ public List<String> getList() {
|
|
88
|
+ return list;
|
|
89
|
+ }
|
|
90
|
+
|
|
91
|
+ /**
|
|
92
|
+ * Wait for input on stream, and output/throw away/save to list
|
|
93
|
+ */
|
|
94
|
+ @Override
|
|
95
|
+ public void run() {
|
|
96
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
|
|
97
|
+ try {
|
|
98
|
+ String line;
|
|
99
|
+ while ((line = reader.readLine()) != null) {
|
|
100
|
+ if (list != null) {
|
|
101
|
+ list.add(line);
|
|
102
|
+ } else if (buffer != null) {
|
|
103
|
+ if (buffer.length() > 0) { buffer.append('\n'); }
|
|
104
|
+ buffer.append(line);
|
|
105
|
+ }
|
|
106
|
+ }
|
|
107
|
+ } catch (IOException ex) {
|
|
108
|
+ // OH WELL
|
|
109
|
+ } finally {
|
|
110
|
+ StreamUtil.close(stream);
|
|
111
|
+ }
|
|
112
|
+ }
|
|
113
|
+}
|