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.1KB

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.FilterInputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import javax.annotation.Nonnull;
  27. /**
  28. *
  29. */
  30. public class ListenerInputStream extends FilterInputStream {
  31. private final DownloadListener listener;
  32. private final int length;
  33. private int count;
  34. private int mark;
  35. public ListenerInputStream(@Nonnull final InputStream in, final DownloadListener listener,
  36. final int length) {
  37. super(in);
  38. this.listener = listener;
  39. this.length = length;
  40. count = 0;
  41. mark = count;
  42. }
  43. @Override
  44. public int read() throws IOException {
  45. final int read = super.read();
  46. return update(read);
  47. }
  48. @Override
  49. public int read(@Nonnull final byte[] b) throws IOException {
  50. final int read = super.read(b);
  51. return update(read);
  52. }
  53. @Override
  54. public int read(@Nonnull final byte[] b, final int off, final int len) throws IOException {
  55. final int read = super.read(b, off, len);
  56. return update(read);
  57. }
  58. @Override
  59. public long skip(final long n) throws IOException {
  60. final long read = super.skip(n);
  61. return update((int) read);
  62. }
  63. @Override
  64. public int available() throws IOException {
  65. return super.available();
  66. }
  67. @Override
  68. public void close() throws IOException {
  69. super.close();
  70. }
  71. @Override
  72. public synchronized void mark(final int readlimit) {
  73. mark = count;
  74. super.mark(readlimit);
  75. }
  76. @Override
  77. public synchronized void reset() throws IOException {
  78. update(mark - count);
  79. super.reset();
  80. }
  81. @Override
  82. public boolean markSupported() {
  83. return super.markSupported();
  84. }
  85. private int update(final int read) {
  86. if (read > 0) {
  87. count += read;
  88. if (listener != null && length != -1) {
  89. listener.downloadProgress(100 * (float) count / length);
  90. }
  91. }
  92. return read;
  93. }
  94. }