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.

ListenerInputStream.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.util.io;
  18. import java.io.FilterInputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import javax.annotation.Nonnull;
  22. /**
  23. * Wrapped {@link InputStream} that provides updates to a {@link DownloadListener} as it reads a
  24. * stream.
  25. */
  26. public class ListenerInputStream extends FilterInputStream {
  27. private final DownloadListener listener;
  28. private final int length;
  29. private int count;
  30. private int mark;
  31. /**
  32. * Creates a new stream.
  33. *
  34. * @param in Stream to wrap
  35. * @param listener Listener to gives up dates to
  36. * @param length Length of the stream, if -1 the listener will be indeterminate
  37. */
  38. public ListenerInputStream(@Nonnull final InputStream in, final DownloadListener listener,
  39. final int length) {
  40. super(in);
  41. this.listener = listener;
  42. this.length = length;
  43. count = 0;
  44. mark = count;
  45. if (listener != null) {
  46. listener.setIndeterminate(length == -1);
  47. }
  48. }
  49. @Override
  50. public int read() throws IOException {
  51. final int read = super.read();
  52. return update(read);
  53. }
  54. @Override
  55. public int read(@Nonnull final byte[] b) throws IOException {
  56. final int read = super.read(b);
  57. return update(read);
  58. }
  59. @Override
  60. public int read(@Nonnull final byte[] b, final int off, final int len) throws IOException {
  61. final int read = super.read(b, off, len);
  62. return update(read);
  63. }
  64. @Override
  65. public long skip(final long n) throws IOException {
  66. final long read = super.skip(n);
  67. return update((int) read);
  68. }
  69. @Override
  70. public int available() throws IOException {
  71. return super.available();
  72. }
  73. @Override
  74. public void close() throws IOException {
  75. super.close();
  76. }
  77. @Override
  78. public synchronized void mark(final int readlimit) {
  79. mark = count;
  80. super.mark(readlimit);
  81. }
  82. @Override
  83. public synchronized void reset() throws IOException {
  84. update(mark - count);
  85. super.reset();
  86. }
  87. @Override
  88. public boolean markSupported() {
  89. return super.markSupported();
  90. }
  91. /**
  92. * Updates the listener and total read count.
  93. *
  94. * @param read Number of bytes further into stream.
  95. *
  96. * @return Returns the number of bytes read.
  97. */
  98. private int update(final int read) {
  99. if (read > 0) {
  100. count += read;
  101. if (listener != null && length != -1) {
  102. listener.downloadProgress(100 * (float) count / length);
  103. }
  104. }
  105. return read;
  106. }
  107. }