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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2006-2015 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.FilterInputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import javax.annotation.Nonnull;
  27. /**
  28. * Wrapped {@link InputStream} that provides updates to a {@link DownloadListener} as it reads a
  29. * stream.
  30. */
  31. public class ListenerInputStream extends FilterInputStream {
  32. private final DownloadListener listener;
  33. private final int length;
  34. private int count;
  35. private int mark;
  36. /**
  37. * Creates a new stream.
  38. *
  39. * @param in Stream to wrap
  40. * @param listener Listener to gives up dates to
  41. * @param length Length of the stream, if -1 the listener will be indeterminate
  42. */
  43. public ListenerInputStream(@Nonnull final InputStream in, final DownloadListener listener,
  44. final int length) {
  45. super(in);
  46. this.listener = listener;
  47. this.length = length;
  48. count = 0;
  49. mark = count;
  50. if (listener != null) {
  51. listener.setIndeterminate(length == -1);
  52. }
  53. }
  54. @Override
  55. public int read() throws IOException {
  56. final int read = super.read();
  57. return update(read);
  58. }
  59. @Override
  60. public int read(@Nonnull final byte[] b) throws IOException {
  61. final int read = super.read(b);
  62. return update(read);
  63. }
  64. @Override
  65. public int read(@Nonnull final byte[] b, final int off, final int len) throws IOException {
  66. final int read = super.read(b, off, len);
  67. return update(read);
  68. }
  69. @Override
  70. public long skip(final long n) throws IOException {
  71. final long read = super.skip(n);
  72. return update((int) read);
  73. }
  74. @Override
  75. public int available() throws IOException {
  76. return super.available();
  77. }
  78. @Override
  79. public void close() throws IOException {
  80. super.close();
  81. }
  82. @Override
  83. public synchronized void mark(final int readlimit) {
  84. mark = count;
  85. super.mark(readlimit);
  86. }
  87. @Override
  88. public synchronized void reset() throws IOException {
  89. update(mark - count);
  90. super.reset();
  91. }
  92. @Override
  93. public boolean markSupported() {
  94. return super.markSupported();
  95. }
  96. /**
  97. * Updates the listener and total read count.
  98. *
  99. * @param read Number of bytes further into stream.
  100. *
  101. * @return Returns the number of bytes read.
  102. */
  103. private int update(final int read) {
  104. if (read > 0) {
  105. count += read;
  106. if (listener != null && length != -1) {
  107. listener.downloadProgress(100 * (float) count / length);
  108. }
  109. }
  110. return read;
  111. }
  112. }