//# 0 errors, 61 messages
//#
/*
    //#StreamReader.java:1:1: class: com.dmdirc.addons.dcc.kde.StreamReader
    //#StreamReader.java:1:1: method: com.dmdirc.addons.dcc.kde.StreamReader.com.dmdirc.addons.dcc.kde.StreamReader__static_init
 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.dcc.kde;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

public class StreamReader extends Thread {
	/** This is the Input Stream we are reading */
	private InputStream stream;
	
	/** This is the output Prefix */
	private String prefix = null;
	
	/** List to store output in */
	private List<String> list = null;
	
	/**
	 * Create a new Stream Reader
	 *
	 * @param stream The stream to read
	 * @param list The list to store the output from the stream in (null for no saving)
	 */
	public StreamReader(final InputStream stream, final List<String> list) {
    //#StreamReader.java:47: method: void com.dmdirc.addons.dcc.kde.StreamReader.com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List)
    //#input(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List)): list
    //#input(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List)): stream
    //#input(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List)): this
    //#output(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List)): this.list
    //#output(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List)): this.prefix
    //#output(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List)): this.stream
    //#post(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List)): this.list == list
    //#post(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List)): init'ed(this.list)
    //#post(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List)): this.prefix == null
    //#post(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List)): this.stream == stream
    //#post(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List)): init'ed(this.stream)
		this.stream = stream;
		this.list = list;
	}
    //#StreamReader.java:50: end of method: void com.dmdirc.addons.dcc.kde.StreamReader.com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List)
	
	/**
	 * Create a new Stream Reader that outputs what it reads
	 *
	 * @param stream The stream to read
	 * @param list The list to store the output from the stream in (null for no saving)
	 * @param prefix Prefix of outputed messages
	 */
	public StreamReader(final InputStream stream, final List<String> list, final String prefix) {
    //#StreamReader.java:59: method: void com.dmdirc.addons.dcc.kde.StreamReader.com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)
    //#input(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)): java.lang.System.out
    //#input(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)): list
    //#input(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)): prefix
    //#input(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)): stream
    //#input(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)): this
    //#output(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)): this.list
    //#output(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)): this.prefix
    //#output(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)): this.stream
    //#presumption(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)): java.lang.System.out != null
    //#post(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)): this.list == list
    //#post(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)): init'ed(this.list)
    //#post(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)): this.prefix == prefix
    //#post(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)): init'ed(this.prefix)
    //#post(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)): this.stream == stream
    //#post(void com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)): init'ed(this.stream)
		this.stream = stream;
		this.prefix = prefix;
		this.list = list;
		
		System.out.printf("[%s] Started%n", prefix);
	}
    //#StreamReader.java:65: end of method: void com.dmdirc.addons.dcc.kde.StreamReader.com.dmdirc.addons.dcc.kde.StreamReader(InputStream, List, String)

	/**
	 * Get the list that the output is being stored in.
	 */
	public List<String> getList() {
		return list;
    //#StreamReader.java:71: method: List com.dmdirc.addons.dcc.kde.StreamReader.getList()
    //#input(List getList()): this
    //#input(List getList()): this.list
    //#output(List getList()): return_value
    //#pre[2] (List getList()): init'ed(this.list)
    //#post(List getList()): return_value == this.list
    //#post(List getList()): init'ed(return_value)
    //#StreamReader.java:71: end of method: List com.dmdirc.addons.dcc.kde.StreamReader.getList()
	}

	/**
	 * Wait for input on stream, and output/throw away/save to list
	 */
	public void run() {
		BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
    //#StreamReader.java:78: method: void com.dmdirc.addons.dcc.kde.StreamReader.run()
    //#input(void run()): java.lang.System.out
    //#input(void run()): this
    //#input(void run()): this.list
    //#input(void run()): this.prefix
    //#input(void run()): this.stream
    //#pre[2] (void run()): (soft) init'ed(this.list)
    //#pre[3] (void run()): (soft) init'ed(this.prefix)
    //#pre[4] (void run()): (soft) this.stream != null
    //#presumption(void run()): java.lang.System.out != null
    //#test_vector(void run()): this.list: Addr_Set{null}, Inverse{null}
    //#test_vector(void run()): this.prefix: Addr_Set{null}, Inverse{null}
		try {
			String line;
			while ((line = reader.readLine()) != null) {
				if (prefix != null) {
					System.out.printf("[%s] %s%n", prefix, line);
				}
				if (list != null) {
					list.add(line);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				stream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
    //#StreamReader.java:98: end of method: void com.dmdirc.addons.dcc.kde.StreamReader.run()
}    //#output(com.dmdirc.addons.dcc.kde.StreamReader__static_init): __Descendant_Table[com/dmdirc/addons/dcc/kde/StreamReader]
    //#output(com.dmdirc.addons.dcc.kde.StreamReader__static_init): __Dispatch_Table.getList()Ljava/util/List;
    //#output(com.dmdirc.addons.dcc.kde.StreamReader__static_init): __Dispatch_Table.run()V
    //#post(com.dmdirc.addons.dcc.kde.StreamReader__static_init): __Descendant_Table[com/dmdirc/addons/dcc/kde/StreamReader] == &__Dispatch_Table
    //#post(com.dmdirc.addons.dcc.kde.StreamReader__static_init): __Dispatch_Table.getList()Ljava/util/List; == &getList
    //#post(com.dmdirc.addons.dcc.kde.StreamReader__static_init): __Dispatch_Table.run()V == &run
    //#StreamReader.java:: end of method: com.dmdirc.addons.dcc.kde.StreamReader.com.dmdirc.addons.dcc.kde.StreamReader__static_init
    //#StreamReader.java:: end of class: com.dmdirc.addons.dcc.kde.StreamReader
