ProxyCache.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package com.yc.videocache;
  2. import java.util.concurrent.atomic.AtomicInteger;
  3. import static com.yc.videocache.Preconditions.checkNotNull;
  4. /**
  5. * Proxy for {@link Source} with caching support ({@link Cache}).
  6. * <p/>
  7. * Can be used only for sources with persistent data (that doesn't change with time).
  8. * Method {@link #read(byte[], long, int)} will be blocked while fetching data from source.
  9. * Useful for streaming something with caching e.g. streaming video/audio etc.
  10. */
  11. class ProxyCache {
  12. private static final int MAX_READ_SOURCE_ATTEMPTS = 1;
  13. private final Source source;
  14. private final Cache cache;
  15. private final Object wc = new Object();
  16. private final Object stopLock = new Object();
  17. private final AtomicInteger readSourceErrorsCount;
  18. private volatile Thread sourceReaderThread;
  19. private volatile boolean stopped;
  20. private volatile int percentsAvailable = -1;
  21. public ProxyCache(Source source, Cache cache) {
  22. this.source = checkNotNull(source);
  23. this.cache = checkNotNull(cache);
  24. this.readSourceErrorsCount = new AtomicInteger();
  25. }
  26. public int read(byte[] buffer, long offset, int length) throws ProxyCacheException {
  27. ProxyCacheUtils.assertBuffer(buffer, offset, length);
  28. while (!cache.isCompleted() && cache.available() < (offset + length) && !stopped) {
  29. readSourceAsync();
  30. waitForSourceData();
  31. checkReadSourceErrorsCount();
  32. }
  33. int read = cache.read(buffer, offset, length);
  34. if (cache.isCompleted() && percentsAvailable != 100) {
  35. percentsAvailable = 100;
  36. onCachePercentsAvailableChanged(100);
  37. }
  38. return read;
  39. }
  40. private void checkReadSourceErrorsCount() throws ProxyCacheException {
  41. int errorsCount = readSourceErrorsCount.get();
  42. if (errorsCount >= MAX_READ_SOURCE_ATTEMPTS) {
  43. readSourceErrorsCount.set(0);
  44. throw new ProxyCacheException("Error reading source " + errorsCount + " times");
  45. }
  46. }
  47. public void shutdown() {
  48. synchronized (stopLock) {
  49. Logger.debug("Shutdown proxy for " + source);
  50. try {
  51. stopped = true;
  52. if (sourceReaderThread != null) {
  53. sourceReaderThread.interrupt();
  54. }
  55. cache.close();
  56. } catch (ProxyCacheException e) {
  57. onError(e);
  58. }
  59. }
  60. }
  61. private synchronized void readSourceAsync() throws ProxyCacheException {
  62. boolean readingInProgress = sourceReaderThread != null && sourceReaderThread.getState() != Thread.State.TERMINATED;
  63. if (!stopped && !cache.isCompleted() && !readingInProgress) {
  64. sourceReaderThread = new Thread(new SourceReaderRunnable(), "Source reader for " + source);
  65. sourceReaderThread.start();
  66. }
  67. }
  68. private void waitForSourceData() throws ProxyCacheException {
  69. synchronized (wc) {
  70. try {
  71. wc.wait(1000);
  72. } catch (InterruptedException e) {
  73. throw new ProxyCacheException("Waiting source data is interrupted!", e);
  74. }
  75. }
  76. }
  77. private void notifyNewCacheDataAvailable(long cacheAvailable, long sourceAvailable) {
  78. onCacheAvailable(cacheAvailable, sourceAvailable);
  79. synchronized (wc) {
  80. wc.notifyAll();
  81. }
  82. }
  83. protected void onCacheAvailable(long cacheAvailable, long sourceLength) {
  84. boolean zeroLengthSource = sourceLength == 0;
  85. int percents = zeroLengthSource ? 100 : (int) ((float) cacheAvailable / sourceLength * 100);
  86. boolean percentsChanged = percents != percentsAvailable;
  87. boolean sourceLengthKnown = sourceLength >= 0;
  88. if (sourceLengthKnown && percentsChanged) {
  89. onCachePercentsAvailableChanged(percents);
  90. }
  91. percentsAvailable = percents;
  92. }
  93. protected void onCachePercentsAvailableChanged(int percentsAvailable) {
  94. }
  95. private void readSource() {
  96. long sourceAvailable = -1;
  97. long offset = 0;
  98. try {
  99. offset = cache.available();
  100. source.open(offset);
  101. sourceAvailable = source.length();
  102. byte[] buffer = new byte[ProxyCacheUtils.DEFAULT_BUFFER_SIZE];
  103. int readBytes;
  104. while ((readBytes = source.read(buffer)) != -1) {
  105. synchronized (stopLock) {
  106. if (isStopped()) {
  107. return;
  108. }
  109. cache.append(buffer, readBytes);
  110. }
  111. offset += readBytes;
  112. notifyNewCacheDataAvailable(offset, sourceAvailable);
  113. }
  114. tryComplete();
  115. onSourceRead();
  116. } catch (Throwable e) {
  117. readSourceErrorsCount.incrementAndGet();
  118. onError(e);
  119. } finally {
  120. closeSource();
  121. notifyNewCacheDataAvailable(offset, sourceAvailable);
  122. }
  123. }
  124. private void onSourceRead() {
  125. // guaranteed notify listeners after source read and cache completed
  126. percentsAvailable = 100;
  127. onCachePercentsAvailableChanged(percentsAvailable);
  128. }
  129. private void tryComplete() throws ProxyCacheException {
  130. synchronized (stopLock) {
  131. if (!isStopped() && cache.available() == source.length()) {
  132. cache.complete();
  133. }
  134. }
  135. }
  136. private boolean isStopped() {
  137. return Thread.currentThread().isInterrupted() || stopped;
  138. }
  139. private void closeSource() {
  140. try {
  141. source.close();
  142. } catch (ProxyCacheException e) {
  143. onError(new ProxyCacheException("Error closing source " + source, e));
  144. }
  145. }
  146. protected final void onError(final Throwable e) {
  147. boolean interruption = e instanceof InterruptedProxyCacheException;
  148. if (interruption) {
  149. Logger.debug("ProxyCache is interrupted");
  150. } else {
  151. Logger.error("ProxyCache error");
  152. }
  153. }
  154. private class SourceReaderRunnable implements Runnable {
  155. @Override
  156. public void run() {
  157. readSource();
  158. }
  159. }
  160. }