Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

StreamReader.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2006-2010 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. package com.dmdirc.addons.dcc.kde;
  23. import java.io.BufferedReader;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.InputStreamReader;
  27. import java.util.List;
  28. public class StreamReader extends Thread {
  29. /** This is the Input Stream we are reading */
  30. private InputStream stream;
  31. /** This is the output Prefix */
  32. private String prefix = null;
  33. /** List to store output in */
  34. private List<String> list = null;
  35. /**
  36. * Create a new Stream Reader
  37. *
  38. * @param stream The stream to read
  39. * @param list The list to store the output from the stream in (null for no saving)
  40. */
  41. public StreamReader(final InputStream stream, final List<String> list) {
  42. this.stream = stream;
  43. this.list = list;
  44. }
  45. /**
  46. * Create a new Stream Reader that outputs what it reads
  47. *
  48. * @param stream The stream to read
  49. * @param list The list to store the output from the stream in (null for no saving)
  50. * @param prefix Prefix of outputed messages
  51. */
  52. public StreamReader(final InputStream stream, final List<String> list, final String prefix) {
  53. this.stream = stream;
  54. this.prefix = prefix;
  55. this.list = list;
  56. System.out.printf("[%s] Started%n", prefix);
  57. }
  58. /**
  59. * Get the list that the output is being stored in.
  60. */
  61. public List<String> getList() {
  62. return list;
  63. }
  64. /**
  65. * Wait for input on stream, and output/throw away/save to list
  66. */
  67. public void run() {
  68. BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
  69. try {
  70. String line;
  71. while ((line = reader.readLine()) != null) {
  72. if (prefix != null) {
  73. System.out.printf("[%s] %s%n", prefix, line);
  74. }
  75. if (list != null) {
  76. list.add(line);
  77. }
  78. }
  79. } catch (IOException e) {
  80. e.printStackTrace();
  81. } finally {
  82. try {
  83. stream.close();
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
  89. }