File Source: StreamReader.java

         /* 
    P/P   *  Method: com.dmdirc.installer.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.installer;
    24  
    25  import java.io.BufferedReader;
    26  import java.io.IOException;
    27  import java.io.InputStream;
    28  import java.io.InputStreamReader;
    29  
    30  /**
    31   * Simple stream reader to read a stream and add it to a text step
    32   */
    33  public class StreamReader extends Thread {
    34  
    35      /** This is the Input Stream we are reading */
    36      private final InputStream stream;
    37      /** This is the output Prefix */
    38      private String prefix = null;
    39      /** This is the StringBuffer to store data in if wanted */
    40      private StringBuffer data = null;
    41      /** This is the Step we are outputting to, */
    42      private TextStep step = null;
    43  
    44      /**
    45       * Create a new Stream Reader
    46       *
    47       * @param stream The stream to read
    48       */
             /* 
    P/P       *  Method: void com.dmdirc.installer.StreamReader(InputStream)
              * 
              *  Postconditions:
              *    this.data == null
              *    this.prefix == null
              *    this.step == null
              *    this.stream == stream
              *    init'ed(this.stream)
              */
    49      public StreamReader(final InputStream stream) {
    50          this.stream = stream;
    51      }
    52  
    53      /**
    54       * Create a new Stream Reader that saves what it reads
    55       *
    56       * @param stream The stream to read
    57       * @param data The stringbuffer to store the output in
    58       * @since 0.6
    59       */
             /* 
    P/P       *  Method: void com.dmdirc.installer.StreamReader(InputStream, StringBuffer)
              * 
              *  Postconditions:
              *    this.data == data
              *    init'ed(this.data)
              *    this.prefix == null
              *    this.step == null
              *    this.stream == stream
              *    init'ed(this.stream)
              */
    60      public StreamReader(final InputStream stream, final StringBuffer data) {
    61          this.stream = stream;
    62          this.data = data;
    63      }
    64  
    65      /**
    66       * Create a new Stream Reader that outputs what it reads
    67       *
    68       * @param stream The stream to read
    69       * @param prefix Prefix of outputed messages
    70       * @param step Step to output to (null = console)
    71       */
    72      public StreamReader(final InputStream stream, final String prefix,
                                 /* 
    P/P                           *  Method: void com.dmdirc.installer.StreamReader(InputStream, String, TextStep)
                                  * 
                                  *  Presumptions:
                                  *    java.lang.System.out != null
                                  * 
                                  *  Postconditions:
                                  *    this.data == null
                                  *    this.prefix == prefix
                                  *    init'ed(this.prefix)
                                  *    this.step == step
                                  *    init'ed(this.step)
                                  *    this.stream == stream
                                  *    init'ed(this.stream)
                                  * 
                                  *  Test Vectors:
                                  *    step: Inverse{null}, Addr_Set{null}
                                  */
    73                          final TextStep step) {
    74          this.stream = stream;
    75          this.prefix = prefix;
    76          this.step = step;
    77  
    78          if (step == null) {
    79              System.out.printf("[%s] Started%n", prefix);
    80          } else {
    81              step.addText(String.format(" - -[%s] Started", prefix));
    82          }
    83      }
    84  
    85      /** {@inheritDoc} */
    86      @Override
    87      public void run() {
                 /* 
    P/P           *  Method: void run()
                  * 
                  *  Preconditions:
                  *    (soft) init'ed(this.data)
                  *    (soft) init'ed(this.prefix)
                  *    (soft) init'ed(this.step)
                  *    (soft) this.stream != null
                  * 
                  *  Presumptions:
                  *    java.lang.System.out != null
                  * 
                  *  Postconditions:
                  *    init'ed(this.data._tainted)
                  * 
                  *  Test Vectors:
                  *    this.data: Addr_Set{null}, Inverse{null}
                  *    this.prefix: Addr_Set{null}, Inverse{null}
                  *    this.step: Inverse{null}, Addr_Set{null}
                  *    java.lang.StringBuffer:length(...)@94: {-231..0}, {1..232-1}
                  */
    88          final BufferedReader reader = new BufferedReader(new InputStreamReader(
    89                  stream));
    90          try {
    91              String line;
    92              while ((line = reader.readLine()) != null) {
    93                  if (data != null) {
    94                      if (data.length() > 0) {
    95                          data.append("\n");
    96                      }
    97                      data.append(line);
    98                  }
    99                  if (prefix != null) {
   100                      if (step == null) {
   101                          System.out.printf("[%s] %s%n", prefix, line);
   102                      } else {
   103                          step.addText(String.format(" - -[%s] %s", prefix, line));
   104                      }
   105                  }
   106              }
   107          } catch (IOException e) {
   108              e.printStackTrace();
   109          } finally {
   110              try {
   111                  stream.close();
   112              } catch (IOException e) {
   113                  e.printStackTrace();
   114              }
   115          }
   116      }
   117  }








SofCheck Inspector Build Version : 2.17854
StreamReader.java 2009-Jun-25 01:54:24
StreamReader.class 2009-Sep-02 17:04:16