1. /*
  2. * @(#)MemoryCache.java 1.15 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.imageio.stream;
  8. import java.util.ArrayList;
  9. import java.io.InputStream;
  10. import java.io.OutputStream;
  11. import java.io.IOException;
  12. /**
  13. * Package-visible class consolidating common code for
  14. * <code>MemoryCacheImageInputStream</code> and
  15. * <code>MemoryCacheImageOutputStream</code>.
  16. * This class keeps an <code>ArrayList</code> of 8K blocks,
  17. * loaded sequentially. Blocks may only be disposed of
  18. * from the index 0 forward. As blocks are freed, the
  19. * corresponding entries in the array list are set to
  20. * <code>null</code>, but no compacting is performed.
  21. * This allows the index for each block to never change,
  22. * and the length of the cache is always the same as the
  23. * total amount of data ever cached. Cached data is
  24. * therefore always contiguous from the point of last
  25. * disposal to the current length.
  26. *
  27. * <p> The total number of blocks resident in the cache must not
  28. * exceed <code>Integer.MAX_VALUE</code>. In practice, the limit of
  29. * available memory will be exceeded long before this becomes an
  30. * issue, since a full cache would contain 8192*2^31 = 16 terabytes of
  31. * data.
  32. *
  33. * A <code>MemoryCache</code> may be reused after a call
  34. * to <code>reset()</code>.
  35. */
  36. class MemoryCache {
  37. private static final int BUFFER_LENGTH = 8192;
  38. private ArrayList cache = new ArrayList();
  39. private long cacheStart = 0L;
  40. /**
  41. * The largest position ever written to the cache.
  42. */
  43. private long length = 0L;
  44. private byte[] getCacheBlock(long blockNum) throws IOException {
  45. long blockOffset = blockNum - cacheStart;
  46. if (blockOffset > Integer.MAX_VALUE) {
  47. // This can only happen when the cache hits 16 terabytes of
  48. // contiguous data...
  49. throw new IOException("Cache addressing limit exceeded!");
  50. }
  51. return (byte[])cache.get((int)blockOffset);
  52. }
  53. /**
  54. * Ensures that at least <code>pos</code> bytes are cached,
  55. * or the end of the source is reached. The return value
  56. * is equal to the smaller of <code>pos</code> and the
  57. * length of the source.
  58. */
  59. public long loadFromStream(InputStream stream, long pos)
  60. throws IOException {
  61. // We've already got enough data cached
  62. if (pos < length) {
  63. return pos;
  64. }
  65. int offset = (int)(length % BUFFER_LENGTH);
  66. byte [] buf = null;
  67. long len = pos - length;
  68. if (offset != 0) {
  69. buf = getCacheBlock(lengthBUFFER_LENGTH);
  70. }
  71. while (len > 0) {
  72. if (buf == null) {
  73. try {
  74. buf = new byte[BUFFER_LENGTH];
  75. } catch (OutOfMemoryError e) {
  76. throw new IOException("No memory left for cache!");
  77. }
  78. offset = 0;
  79. }
  80. int left = BUFFER_LENGTH - offset;
  81. int nbytes = (int)Math.min(len, (long)left);
  82. nbytes = stream.read(buf, offset, nbytes);
  83. if (nbytes == -1) {
  84. return length; // EOF
  85. }
  86. if (offset == 0) {
  87. cache.add(buf);
  88. }
  89. len -= nbytes;
  90. length += nbytes;
  91. offset += nbytes;
  92. if (offset >= BUFFER_LENGTH) {
  93. // we've filled the current buffer, so a new one will be
  94. // allocated next time around (and offset will be reset to 0)
  95. buf = null;
  96. }
  97. }
  98. return pos;
  99. }
  100. /**
  101. * Writes out a portion of the cache to an <code>OutputStream</code>.
  102. * This method preserves no state about the output stream, and does
  103. * not dispose of any blocks containing bytes written. To dispose
  104. * blocks, use {@link #disposeBefore <code>disposeBefore()</code>}.
  105. *
  106. * @exception IndexOutOfBoundsException if any portion of
  107. * the requested data is not in the cache (including if <code>pos</code>
  108. * is in a block already disposed), or if either <code>pos</code> or
  109. * <code>len</code> is < 0.
  110. */
  111. public void writeToStream(OutputStream stream, long pos, long len)
  112. throws IOException {
  113. if (pos + len > length) {
  114. throw new IndexOutOfBoundsException("Argument out of cache");
  115. }
  116. if ((pos < 0) || (len < 0)) {
  117. throw new IndexOutOfBoundsException("Negative pos or len");
  118. }
  119. if (len == 0) {
  120. return;
  121. }
  122. long bufIndex = posBUFFER_LENGTH;
  123. if (bufIndex < cacheStart) {
  124. throw new IndexOutOfBoundsException("pos already disposed");
  125. }
  126. int offset = (int)(pos % BUFFER_LENGTH);
  127. byte[] buf = getCacheBlock(bufIndex++);
  128. while (len > 0) {
  129. if (buf == null) {
  130. buf = getCacheBlock(bufIndex++);
  131. offset = 0;
  132. }
  133. int nbytes = (int)Math.min(len, (long)(BUFFER_LENGTH - offset));
  134. stream.write(buf, offset, nbytes);
  135. buf = null;
  136. len -= nbytes;
  137. }
  138. }
  139. /**
  140. * Ensure that there is space to write a byte at the given position.
  141. */
  142. private void pad(long pos) throws IOException {
  143. long currIndex = cacheStart + cache.size() - 1;
  144. long lastIndex = posBUFFER_LENGTH;
  145. long numNewBuffers = lastIndex - currIndex;
  146. for (long i = 0; i < numNewBuffers; i++) {
  147. try {
  148. cache.add(new byte[BUFFER_LENGTH]);
  149. } catch (OutOfMemoryError e) {
  150. throw new IOException("No memory left for cache!");
  151. }
  152. }
  153. }
  154. /**
  155. * Overwrites and/or appends the cache from a byte array.
  156. * The length of the cache will be extended as needed to hold
  157. * the incoming data.
  158. *
  159. * @param b an array of bytes containing data to be written.
  160. * @param off the starting offset withing the data array.
  161. * @param len the number of bytes to be written.
  162. * @param pos the cache position at which to begin writing.
  163. *
  164. * @exception NullPointerException if <code>b</code> is <code>null</code>.
  165. * @exception IndexOutOfBoundsException if <code>off</code>,
  166. * <code>len</code>, or <code>pos</code> are negative,
  167. * or if <code>off+len > b.length</code>.
  168. */
  169. public void write(byte[] b, int off, int len, long pos)
  170. throws IOException {
  171. if (b == null) {
  172. throw new NullPointerException("b == null!");
  173. }
  174. // Fix 4430357 - if off + len < 0, overflow occurred
  175. if ((off < 0) || (len < 0) || (pos < 0) ||
  176. (off + len > b.length) || (off + len < 0)) {
  177. throw new IndexOutOfBoundsException();
  178. }
  179. // Ensure there is space for the incoming data
  180. long lastPos = pos + len - 1;
  181. if (lastPos >= length) {
  182. pad(lastPos);
  183. length = lastPos + 1;
  184. }
  185. // Copy the data into the cache, block by block
  186. int offset = (int)(pos % BUFFER_LENGTH);
  187. while (len > 0) {
  188. byte[] buf = getCacheBlock(posBUFFER_LENGTH);
  189. int nbytes = Math.min(len, BUFFER_LENGTH - offset);
  190. System.arraycopy(b, off, buf, offset, nbytes);
  191. pos += nbytes;
  192. off += nbytes;
  193. len -= nbytes;
  194. offset = 0; // Always after the first time
  195. }
  196. }
  197. /**
  198. * Overwrites or appends a single byte to the cache.
  199. * The length of the cache will be extended as needed to hold
  200. * the incoming data.
  201. *
  202. * @param b an <code>int</code> whose 8 least significant bits
  203. * will be written.
  204. * @param pos the cache position at which to begin writing.
  205. *
  206. * @exception IndexOutOfBoundsException if <code>pos</code> is negative.
  207. */
  208. public void write(int b, long pos) throws IOException {
  209. if (pos < 0) {
  210. throw new ArrayIndexOutOfBoundsException("pos < 0");
  211. }
  212. // Ensure there is space for the incoming data
  213. if (pos >= length) {
  214. pad(pos);
  215. length = pos + 1;
  216. }
  217. // Insert the data.
  218. byte[] buf = getCacheBlock(posBUFFER_LENGTH);
  219. int offset = (int)(pos % BUFFER_LENGTH);
  220. buf[offset] = (byte)b;
  221. }
  222. /**
  223. * Returns the total length of data that has been cached,
  224. * regardless of whether any early blocks have been disposed.
  225. * This value will only ever increase.
  226. */
  227. public long getLength() {
  228. return length;
  229. }
  230. /**
  231. * Returns the single byte at the given position, as an
  232. * <code>int</code>. Returns -1 if this position has
  233. * not been cached or has been disposed.
  234. */
  235. public int read(long pos) throws IOException {
  236. if (pos >= length) {
  237. return -1;
  238. }
  239. byte[] buf = getCacheBlock(posBUFFER_LENGTH);
  240. if (buf == null) {
  241. return -1;
  242. }
  243. return buf[(int)(pos % BUFFER_LENGTH)] & 0xff;
  244. }
  245. /**
  246. * Copy <code>len</code> bytes from the cache, starting
  247. * at cache position <code>pos</code>, into the array
  248. * <code>b</code> at offset <code>off</code>.
  249. *
  250. * @exception NullPointerException if b is <code>null</code>
  251. * @exception IndexOutOfBoundsException if <code>off</code>,
  252. * <code>len</code> or <code>pos</code> are negative or if
  253. * <code>off + len > b.length</code> or if any portion of the
  254. * requested data is not in the cache (including if
  255. * <code>pos</code> is in a block that has already been disposed).
  256. */
  257. public void read(byte[] b, int off, int len, long pos)
  258. throws IOException {
  259. if (b == null) {
  260. throw new NullPointerException("b == null!");
  261. }
  262. // Fix 4430357 - if off + len < 0, overflow occurred
  263. if ((off < 0) || (len < 0) || (pos < 0) ||
  264. (off + len > b.length) || (off + len < 0)) {
  265. throw new IndexOutOfBoundsException();
  266. }
  267. if (pos + len > length) {
  268. throw new IndexOutOfBoundsException();
  269. }
  270. long index = posBUFFER_LENGTH;
  271. int offset = (int)pos % BUFFER_LENGTH;
  272. while (len > 0) {
  273. int nbytes = Math.min(len, BUFFER_LENGTH - offset);
  274. byte[] buf = getCacheBlock(index++);
  275. System.arraycopy(buf, offset, b, off, nbytes);
  276. len -= nbytes;
  277. off += nbytes;
  278. offset = 0; // Always after the first time
  279. }
  280. }
  281. /**
  282. * Free the blocks up to the position <code>pos</code>.
  283. * The byte at <code>pos</code> remains available.
  284. *
  285. * @exception IndexOutOfBoundsException if <code>pos</code>
  286. * is in a block that has already been disposed.
  287. */
  288. public void disposeBefore(long pos) {
  289. long index = posBUFFER_LENGTH;
  290. if (index < cacheStart) {
  291. throw new IndexOutOfBoundsException("pos already disposed");
  292. }
  293. long numBlocks = Math.min(index - cacheStart, cache.size());
  294. for (long i = 0; i < numBlocks; i++) {
  295. cache.remove(0);
  296. }
  297. this.cacheStart = index;
  298. }
  299. /**
  300. * Erase the entire cache contents and reset the length to 0.
  301. * The cache object may subsequently be reused as though it had just
  302. * been allocated.
  303. */
  304. public void reset() {
  305. cache.clear();
  306. cacheStart = 0;
  307. length = 0L;
  308. }
  309. }