選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

StreamReader.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2006-2013 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. public 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. public 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. public StreamReader(final InputStream stream, final List<String> list) {
  54. super("StreamReader");
  55. this.stream = stream;
  56. this.list = list;
  57. }
  58. /**
  59. * Create a new Stream Reader that outputs to a StringBuffer.
  60. *
  61. * @param stream The stream to read
  62. * @param buffer The StringBuffer to store the output from the stream in
  63. */
  64. public StreamReader(final InputStream stream, final StringBuffer buffer) {
  65. super("StreamReader");
  66. this.stream = stream;
  67. this.buffer = buffer;
  68. }
  69. /**
  70. * Get the list that the output is being stored in.
  71. *
  72. * @return The output list
  73. */
  74. public List<String> getList() {
  75. return list;
  76. }
  77. /**
  78. * Wait for input on stream, and output/throw away/save to list.
  79. */
  80. @Override
  81. public void run() {
  82. final BufferedReader reader = new BufferedReader(
  83. new InputStreamReader(stream));
  84. try {
  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. } finally {
  97. StreamUtils.close(stream);
  98. }
  99. }
  100. }