Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ListenerInputStream.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. * 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. }
  51. @Override
  52. public int read() throws IOException {
  53. final int read = super.read();
  54. return update(read);
  55. }
  56. @Override
  57. public int read(@Nonnull final byte[] b) throws IOException {
  58. final int read = super.read(b);
  59. return update(read);
  60. }
  61. @Override
  62. public int read(@Nonnull final byte[] b, final int off, final int len) throws IOException {
  63. final int read = super.read(b, off, len);
  64. return update(read);
  65. }
  66. @Override
  67. public long skip(final long n) throws IOException {
  68. final long read = super.skip(n);
  69. return update((int) read);
  70. }
  71. @Override
  72. public int available() throws IOException {
  73. return super.available();
  74. }
  75. @Override
  76. public void close() throws IOException {
  77. super.close();
  78. }
  79. @Override
  80. public synchronized void mark(final int readlimit) {
  81. mark = count;
  82. super.mark(readlimit);
  83. }
  84. @Override
  85. public synchronized void reset() throws IOException {
  86. update(mark - count);
  87. super.reset();
  88. }
  89. @Override
  90. public boolean markSupported() {
  91. return super.markSupported();
  92. }
  93. /**
  94. * Updates the listener and total read count.
  95. *
  96. * @param read Number of bytes further into stream.
  97. *
  98. * @return Returns the number of bytes read.
  99. */
  100. private int update(final int read) {
  101. if (read > 0) {
  102. count += read;
  103. if (listener != null && length != -1) {
  104. listener.downloadProgress(100 * (float) count / length);
  105. }
  106. }
  107. return read;
  108. }
  109. }