File Source: StreamReader.java
/*
P/P * Method: com.dmdirc.addons.dcc.kde.StreamReader__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 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.addons.dcc.kde;
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 public class StreamReader extends Thread {
32 /** This is the Input Stream we are reading */
33 private InputStream stream;
34
35 /** This is the output Prefix */
36 private String prefix = null;
37
38 /** List to store output in */
39 private List<String> list = null;
40
41 /**
42 * Create a new Stream Reader
43 *
44 * @param stream The stream to read
45 * @param list The list to store the output from the stream in (null for no saving)
46 */
/*
P/P * Method: void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List)
*
* Postconditions:
* this.list == list
* init'ed(this.list)
* this.prefix == null
* this.stream == stream
* init'ed(this.stream)
*/
47 public StreamReader(final InputStream stream, final List<String> list) {
48 this.stream = stream;
49 this.list = list;
50 }
51
52 /**
53 * Create a new Stream Reader that outputs what it reads
54 *
55 * @param stream The stream to read
56 * @param list The list to store the output from the stream in (null for no saving)
57 * @param prefix Prefix of outputed messages
58 */
/*
P/P * Method: void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)
*
* Presumptions:
* java.lang.System.out != null
*
* Postconditions:
* this.list == list
* init'ed(this.list)
* this.prefix == prefix
* init'ed(this.prefix)
* this.stream == stream
* init'ed(this.stream)
*/
59 public StreamReader(final InputStream stream, final List<String> list, final String prefix) {
60 this.stream = stream;
61 this.prefix = prefix;
62 this.list = list;
63
64 System.out.printf("[%s] Started%n", prefix);
65 }
66
67 /**
68 * Get the list that the output is being stored in.
69 */
70 public List<String> getList() {
/*
P/P * Method: List getList()
*
* Preconditions:
* init'ed(this.list)
*
* Postconditions:
* return_value == this.list
* init'ed(return_value)
*/
71 return list;
72 }
73
74 /**
75 * Wait for input on stream, and output/throw away/save to list
76 */
77 public void run() {
/*
P/P * Method: void run()
*
* Preconditions:
* (soft) init'ed(this.list)
* (soft) init'ed(this.prefix)
* (soft) this.stream != null
*
* Presumptions:
* java.lang.System.out != null
*
* Test Vectors:
* this.list: Addr_Set{null}, Inverse{null}
* this.prefix: Addr_Set{null}, Inverse{null}
*/
78 BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
79 try {
80 String line;
81 while ((line = reader.readLine()) != null) {
82 if (prefix != null) {
83 System.out.printf("[%s] %s%n", prefix, line);
84 }
85 if (list != null) {
86 list.add(line);
87 }
88 }
89 } catch (IOException e) {
90 e.printStackTrace();
91 } finally {
92 try {
93 stream.close();
94 } catch (IOException e) {
95 e.printStackTrace();
96 }
97 }
98 }
99 }
SofCheck Inspector Build Version : 2.17854
| StreamReader.java |
2009-Jun-25 01:54:24 |
| StreamReader.class |
2009-Sep-02 17:04:15 |