//# 0 errors, 99 messages
//#
/*
    //#StreamReader.java:1:1: class: com.dmdirc.installer.StreamReader
    //#StreamReader.java:1:1: method: com.dmdirc.installer.StreamReader.com.dmdirc.installer.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.installer;

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

/**
 * Simple stream reader to read a stream and add it to a text step
 */
public class StreamReader extends Thread {

    /** This is the Input Stream we are reading */
    private final InputStream stream;
    /** This is the output Prefix */
    private String prefix = null;
    /** This is the StringBuffer to store data in if wanted */
    private StringBuffer data = null;
    /** This is the Step we are outputting to, */
    private TextStep step = null;

    /**
     * Create a new Stream Reader
     *
     * @param stream The stream to read
     */
    public StreamReader(final InputStream stream) {
    //#StreamReader.java:49: method: void com.dmdirc.installer.StreamReader.com.dmdirc.installer.StreamReader(InputStream)
    //#input(void com.dmdirc.installer.StreamReader(InputStream)): stream
    //#input(void com.dmdirc.installer.StreamReader(InputStream)): this
    //#output(void com.dmdirc.installer.StreamReader(InputStream)): this.data
    //#output(void com.dmdirc.installer.StreamReader(InputStream)): this.prefix
    //#output(void com.dmdirc.installer.StreamReader(InputStream)): this.step
    //#output(void com.dmdirc.installer.StreamReader(InputStream)): this.stream
    //#post(void com.dmdirc.installer.StreamReader(InputStream)): this.data == null
    //#post(void com.dmdirc.installer.StreamReader(InputStream)): this.prefix == null
    //#post(void com.dmdirc.installer.StreamReader(InputStream)): this.step == null
    //#post(void com.dmdirc.installer.StreamReader(InputStream)): this.stream == stream
    //#post(void com.dmdirc.installer.StreamReader(InputStream)): init'ed(this.stream)
        this.stream = stream;
    }
    //#StreamReader.java:51: end of method: void com.dmdirc.installer.StreamReader.com.dmdirc.installer.StreamReader(InputStream)

    /**
     * Create a new Stream Reader that saves what it reads
     *
     * @param stream The stream to read
     * @param data The stringbuffer to store the output in
     * @since 0.6
     */
    public StreamReader(final InputStream stream, final StringBuffer data) {
    //#StreamReader.java:60: method: void com.dmdirc.installer.StreamReader.com.dmdirc.installer.StreamReader(InputStream, StringBuffer)
    //#input(void com.dmdirc.installer.StreamReader(InputStream, StringBuffer)): data
    //#input(void com.dmdirc.installer.StreamReader(InputStream, StringBuffer)): stream
    //#input(void com.dmdirc.installer.StreamReader(InputStream, StringBuffer)): this
    //#output(void com.dmdirc.installer.StreamReader(InputStream, StringBuffer)): this.data
    //#output(void com.dmdirc.installer.StreamReader(InputStream, StringBuffer)): this.prefix
    //#output(void com.dmdirc.installer.StreamReader(InputStream, StringBuffer)): this.step
    //#output(void com.dmdirc.installer.StreamReader(InputStream, StringBuffer)): this.stream
    //#post(void com.dmdirc.installer.StreamReader(InputStream, StringBuffer)): this.data == data
    //#post(void com.dmdirc.installer.StreamReader(InputStream, StringBuffer)): init'ed(this.data)
    //#post(void com.dmdirc.installer.StreamReader(InputStream, StringBuffer)): this.prefix == null
    //#post(void com.dmdirc.installer.StreamReader(InputStream, StringBuffer)): this.step == null
    //#post(void com.dmdirc.installer.StreamReader(InputStream, StringBuffer)): this.stream == stream
    //#post(void com.dmdirc.installer.StreamReader(InputStream, StringBuffer)): init'ed(this.stream)
        this.stream = stream;
        this.data = data;
    }
    //#StreamReader.java:63: end of method: void com.dmdirc.installer.StreamReader.com.dmdirc.installer.StreamReader(InputStream, StringBuffer)

    /**
     * Create a new Stream Reader that outputs what it reads
     *
     * @param stream The stream to read
     * @param prefix Prefix of outputed messages
     * @param step Step to output to (null = console)
     */
    public StreamReader(final InputStream stream, final String prefix,
                        final TextStep step) {
    //#StreamReader.java:73: method: void com.dmdirc.installer.StreamReader.com.dmdirc.installer.StreamReader(InputStream, String, TextStep)
    //#StreamReader.java:73: Warning: suspicious precondition
    //#    The precondition for step.__Tag is not a contiguous range of values
    //#    severity: SUPPRESSED
    //#    class: com.dmdirc.installer.StreamReader
    //#    method: void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)
    //#    suspicious precondition index: [4]
    //#    Attribs:  Soft
    //#input(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): com/dmdirc/installer/TextStep.__Descendant_Table[com/dmdirc/installer/TextStep]
    //#input(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): com/dmdirc/installer/TextStep.__Descendant_Table[com/dmdirc/installer/ui/StepConfirm]
    //#input(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): com/dmdirc/installer/TextStep.__Descendant_Table[com/dmdirc/installer/ui/StepInstall]
    //#input(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): com/dmdirc/installer/TextStep.__Descendant_Table[others]
    //#input(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): com/dmdirc/installer/TextStep.__Dispatch_Table.addText(Ljava/lang/String;)V
    //#input(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): com/dmdirc/installer/ui/StepConfirm.__Dispatch_Table.addText(Ljava/lang/String;)V
    //#input(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): com/dmdirc/installer/ui/StepInstall.__Dispatch_Table.addText(Ljava/lang/String;)V
    //#input(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): java.lang.System.out
    //#input(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): prefix
    //#input(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): step
    //#input(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): step.__Tag
    //#input(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): stream
    //#input(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): this
    //#output(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): this.data
    //#output(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): this.prefix
    //#output(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): this.step
    //#output(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): this.stream
    //#pre[4] (void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): (soft) step.__Tag in {com/dmdirc/installer/TextStep, com/dmdirc/installer/ui/StepConfirm, com/dmdirc/installer/ui/StepInstall}
    //#presumption(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): java.lang.System.out != null
    //#post(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): this.data == null
    //#post(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): this.prefix == prefix
    //#post(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): init'ed(this.prefix)
    //#post(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): this.step == step
    //#post(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): init'ed(this.step)
    //#post(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): this.stream == stream
    //#post(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): init'ed(this.stream)
    //#unanalyzed(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): Effects-of-calling:javax.swing.SwingUtilities:invokeLater
    //#test_vector(void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)): step: Inverse{null}, Addr_Set{null}
        this.stream = stream;
        this.prefix = prefix;
        this.step = step;

        if (step == null) {
            System.out.printf("[%s] Started%n", prefix);
        } else {
            step.addText(String.format(" - -[%s] Started", prefix));
        }
    }
    //#StreamReader.java:83: end of method: void com.dmdirc.installer.StreamReader.com.dmdirc.installer.StreamReader(InputStream, String, TextStep)

    /** {@inheritDoc} */
    @Override
    public void run() {
        final BufferedReader reader = new BufferedReader(new InputStreamReader(
    //#StreamReader.java:88: method: void com.dmdirc.installer.StreamReader.run()
    //#StreamReader.java:88: Warning: suspicious precondition
    //#    The precondition for this.step.__Tag is not a contiguous range of values
    //#    severity: SUPPRESSED
    //#    class: com.dmdirc.installer.StreamReader
    //#    method: void run()
    //#    suspicious precondition index: [7]
    //#    Attribs:  Soft
    //#input(void run()): "&#10;"._tainted
    //#input(void run()): com/dmdirc/installer/TextStep.__Descendant_Table[com/dmdirc/installer/TextStep]
    //#input(void run()): com/dmdirc/installer/TextStep.__Descendant_Table[com/dmdirc/installer/ui/StepConfirm]
    //#input(void run()): com/dmdirc/installer/TextStep.__Descendant_Table[com/dmdirc/installer/ui/StepInstall]
    //#input(void run()): com/dmdirc/installer/TextStep.__Descendant_Table[others]
    //#input(void run()): com/dmdirc/installer/TextStep.__Dispatch_Table.addText(Ljava/lang/String;)V
    //#input(void run()): com/dmdirc/installer/ui/StepConfirm.__Dispatch_Table.addText(Ljava/lang/String;)V
    //#input(void run()): com/dmdirc/installer/ui/StepInstall.__Dispatch_Table.addText(Ljava/lang/String;)V
    //#input(void run()): java.lang.System.out
    //#input(void run()): this
    //#input(void run()): this.data
    //#input(void run()): this.data._tainted
    //#input(void run()): this.prefix
    //#input(void run()): this.step
    //#input(void run()): this.step.__Tag
    //#input(void run()): this.stream
    //#output(void run()): this.data._tainted
    //#pre[3] (void run()): (soft) init'ed(this.data)
    //#pre[4] (void run()): (soft) init'ed(this.prefix)
    //#pre[5] (void run()): (soft) init'ed(this.step)
    //#pre[7] (void run()): (soft) this.step.__Tag in {com/dmdirc/installer/TextStep, com/dmdirc/installer/ui/StepConfirm, com/dmdirc/installer/ui/StepInstall}
    //#pre[8] (void run()): (soft) this.stream != null
    //#presumption(void run()): java.lang.System.out != null
    //#post(void run()): init'ed(this.data._tainted)
    //#unanalyzed(void run()): Effects-of-calling:javax.swing.SwingUtilities:invokeLater
    //#test_vector(void run()): this.data: Addr_Set{null}, Inverse{null}
    //#test_vector(void run()): this.prefix: Addr_Set{null}, Inverse{null}
    //#test_vector(void run()): this.step: Inverse{null}, Addr_Set{null}
    //#test_vector(void run()): java.lang.StringBuffer:length(...)@94: {-2_147_483_648..0}, {1..4_294_967_295}
                stream));
        try {
            String line;
            while ((line = reader.readLine()) != null) {
                if (data != null) {
                    if (data.length() > 0) {
                        data.append("\n");
                    }
                    data.append(line);
                }
                if (prefix != null) {
                    if (step == null) {
                        System.out.printf("[%s] %s%n", prefix, line);
                    } else {
                        step.addText(String.format(" - -[%s] %s", prefix, line));
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //#StreamReader.java:116: end of method: void com.dmdirc.installer.StreamReader.run()
}    //#output(com.dmdirc.installer.StreamReader__static_init): __Descendant_Table[com/dmdirc/installer/StreamReader]
    //#output(com.dmdirc.installer.StreamReader__static_init): __Dispatch_Table.run()V
    //#post(com.dmdirc.installer.StreamReader__static_init): __Descendant_Table[com/dmdirc/installer/StreamReader] == &__Dispatch_Table
    //#post(com.dmdirc.installer.StreamReader__static_init): __Dispatch_Table.run()V == &run
    //#StreamReader.java:: end of method: com.dmdirc.installer.StreamReader.com.dmdirc.installer.StreamReader__static_init
    //#StreamReader.java:: end of class: com.dmdirc.installer.StreamReader
