You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StreamReader.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  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.util.io;
  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. /**
  29. * Simple stream reader to read a stream and save/discard the data.
  30. */
  31. class StreamReader extends Thread {
  32. /** This is the Input Stream we are reading. */
  33. private final InputStream stream;
  34. /** List to store output in. */
  35. private List<String> list;
  36. /** StringBuffer to store output in. */
  37. private StringBuffer buffer; // NOPMD
  38. /**
  39. * Create a new Stream Reader that discards output.
  40. *
  41. * @param stream The stream to read
  42. */
  43. StreamReader(final InputStream stream) {
  44. super("StreamReader");
  45. this.stream = stream;
  46. }
  47. /**
  48. * Create a new Stream Reader that outputs to a List.
  49. *
  50. * @param stream The stream to read
  51. * @param list The list to store the output from the stream in
  52. */
  53. @SuppressWarnings("AssignmentToCollectionOrArrayFieldFromParameter")
  54. StreamReader(final InputStream stream, final List<String> list) {
  55. super("StreamReader");
  56. this.stream = stream;
  57. this.list = list;
  58. }
  59. /**
  60. * Create a new Stream Reader that outputs to a StringBuffer.
  61. *
  62. * @param stream The stream to read
  63. * @param buffer The StringBuffer to store the output from the stream in
  64. */
  65. StreamReader(final InputStream stream, final StringBuffer buffer) {
  66. super("StreamReader");
  67. this.stream = stream;
  68. this.buffer = buffer;
  69. }
  70. /**
  71. * Get the list that the output is being stored in.
  72. *
  73. * @return The output list
  74. */
  75. @SuppressWarnings("ReturnOfCollectionOrArrayField")
  76. List<String> getList() {
  77. return list;
  78. }
  79. /**
  80. * Wait for input on stream, and output/throw away/save to list.
  81. */
  82. @Override
  83. public void run() {
  84. try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
  85. String line;
  86. while ((line = reader.readLine()) != null) {
  87. if (list != null) {
  88. list.add(line);
  89. } else if (buffer != null) {
  90. if (buffer.length() > 0) { buffer.append('\n'); }
  91. buffer.append(line);
  92. }
  93. }
  94. } catch (IOException ex) {
  95. // OH WELL
  96. }
  97. }
  98. }