1. /*
  2. * @(#)MemoryCache.java 1.13 03/01/23
  3. *
  4. * Copyright 2003 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. buf = null;
  90. len -= nbytes;
  91. length += nbytes;
  92. }
  93. return pos;
  94. }
  95. /**
  96. * Writes out a portion of the cache to an <code>OutputStream</code>.
  97. * This method preserves no state about the output stream, and does
  98. * not dispose of any blocks containing bytes written. To dispose
  99. * blocks, use {@link #disposeBefore <code>disposeBefore()</code>}.
  100. *
  101. * @exception IndexOutOfBoundsException if any portion of
  102. * the requested data is not in the cache (including if <code>pos</code>
  103. * is in a block already disposed), or if either <code>pos</code> or
  104. * <code>len</code> is < 0.
  105. */
  106. public void writeToStream(OutputStream stream, long pos, long len)
  107. throws IOException {
  108. if (pos + len > length) {
  109. throw new IndexOutOfBoundsException("Argument out of cache");
  110. }
  111. if ((pos < 0) || (len < 0)) {
  112. throw new IndexOutOfBoundsException("Negative pos or len");
  113. }
  114. if (len == 0) {
  115. return;
  116. }
  117. long bufIndex = posBUFFER_LENGTH;
  118. if (bufIndex < cacheStart) {
  119. throw new IndexOutOfBoundsException("pos already disposed");
  120. }
  121. int offset = (int)(pos % BUFFER_LENGTH);
  122. byte[] buf = getCacheBlock(bufIndex++);
  123. while (len > 0) {
  124. if (buf == null) {
  125. buf = getCacheBlock(bufIndex++);
  126. offset = 0;
  127. }
  128. int nbytes = (int)Math.min(len, (long)(BUFFER_LENGTH - offset));
  129. stream.write(buf, offset, nbytes);
  130. buf = null;
  131. len -= nbytes;
  132. }
  133. }
  134. /**
  135. * Ensure that there is space to write a byte at the given position.
  136. */
  137. private void pad(long pos) throws IOException {
  138. long currIndex = cacheStart + cache.size() - 1;
  139. long lastIndex = posBUFFER_LENGTH;
  140. long numNewBuffers = lastIndex - currIndex;
  141. for (long i = 0; i < numNewBuffers; i++) {
  142. try {
  143. cache.add(new byte[BUFFER_LENGTH]);
  144. } catch (OutOfMemoryError e) {
  145. throw new IOException("No memory left for cache!");
  146. }
  147. }
  148. }
  149. /**
  150. * Overwrites and/or appends the cache from a byte array.
  151. * The length of the cache will be extended as needed to hold
  152. * the incoming data.
  153. *
  154. * @param b an array of bytes containing data to be written.
  155. * @param off the starting offset withing the data array.
  156. * @param len the number of bytes to be written.
  157. * @param pos the cache position at which to begin writing.
  158. *
  159. * @exception NullPointerException if <code>b</code> is <code>null</code>.
  160. * @exception IndexOutOfBoundsException if <code>off</code>,
  161. * <code>len</code>, or <code>pos</code> are negative,
  162. * or if <code>off+len > b.length</code>.
  163. */
  164. public void write(byte[] b, int off, int len, long pos)
  165. throws IOException {
  166. if (b == null) {
  167. throw new NullPointerException("b == null!");
  168. }
  169. // Fix 4430357 - if off + len < 0, overflow occurred
  170. if ((off < 0) || (len < 0) || (pos < 0) ||
  171. (off + len > b.length) || (off + len < 0)) {
  172. throw new IndexOutOfBoundsException();
  173. }
  174. // Ensure there is space for the incoming data
  175. long lastPos = pos + len - 1;
  176. if (lastPos >= length) {
  177. pad(lastPos);
  178. length = lastPos + 1;
  179. }
  180. // Copy the data into the cache, block by block
  181. int offset = (int)(pos % BUFFER_LENGTH);
  182. while (len > 0) {
  183. byte[] buf = getCacheBlock(posBUFFER_LENGTH);
  184. int nbytes = Math.min(len, BUFFER_LENGTH - offset);
  185. System.arraycopy(b, off, buf, offset, nbytes);
  186. pos += nbytes;
  187. off += nbytes;
  188. len -= nbytes;
  189. offset = 0; // Always after the first time
  190. }
  191. }
  192. /**
  193. * Overwrites or appends a single byte to the cache.
  194. * The length of the cache will be extended as needed to hold
  195. * the incoming data.
  196. *
  197. * @param b an <code>int</code> whose 8 least significant bits
  198. * will be written.
  199. * @param pos the cache position at which to begin writing.
  200. *
  201. * @exception IndexOutOfBoundsException if <code>pos</code> is negative.
  202. */
  203. public void write(int b, long pos) throws IOException {
  204. if (pos < 0) {
  205. throw new ArrayIndexOutOfBoundsException("pos < 0");
  206. }
  207. // Ensure there is space for the incoming data
  208. if (pos >= length) {
  209. pad(pos);
  210. length = pos + 1;
  211. }
  212. // Insert the data.
  213. byte[] buf = getCacheBlock(posBUFFER_LENGTH);
  214. int offset = (int)(pos % BUFFER_LENGTH);
  215. buf[offset] = (byte)b;
  216. }
  217. /**
  218. * Returns the total length of data that has been cached,
  219. * regardless of whether any early blocks have been disposed.
  220. * This value will only ever increase.
  221. */
  222. public long getLength() {
  223. return length;
  224. }
  225. /**
  226. * Returns the single byte at the given position, as an
  227. * <code>int</code>. Returns -1 if this position has
  228. * not been cached or has been disposed.
  229. */
  230. public int read(long pos) throws IOException {
  231. if (pos >= length) {
  232. return -1;
  233. }
  234. byte[] buf = getCacheBlock(posBUFFER_LENGTH);
  235. if (buf == null) {
  236. return -1;
  237. }
  238. return buf[(int)(pos % BUFFER_LENGTH)] & 0xff;
  239. }
  240. /**
  241. * Copy <code>len</code> bytes from the cache, starting
  242. * at cache position <code>pos</code>, into the array
  243. * <code>b</code> at offset <code>off</code>.
  244. *
  245. * @exception NullPointerException if b is <code>null</code>
  246. * @exception IndexOutOfBoundsException if <code>off</code>,
  247. * <code>len</code> or <code>pos</code> are negative or if
  248. * <code>off + len > b.length</code> or if any portion of the
  249. * requested data is not in the cache (including if
  250. * <code>pos</code> is in a block that has already been disposed).
  251. */
  252. public void read(byte[] b, int off, int len, long pos)
  253. throws IOException {
  254. if (b == null) {
  255. throw new NullPointerException("b == null!");
  256. }
  257. // Fix 4430357 - if off + len < 0, overflow occurred
  258. if ((off < 0) || (len < 0) || (pos < 0) ||
  259. (off + len > b.length) || (off + len < 0)) {
  260. throw new IndexOutOfBoundsException();
  261. }
  262. if (pos + len > length) {
  263. throw new IndexOutOfBoundsException();
  264. }
  265. long index = posBUFFER_LENGTH;
  266. int offset = (int)pos % BUFFER_LENGTH;
  267. while (len > 0) {
  268. int nbytes = Math.min(len, BUFFER_LENGTH - offset);
  269. byte[] buf = getCacheBlock(index++);
  270. System.arraycopy(buf, offset, b, off, nbytes);
  271. len -= nbytes;
  272. off += nbytes;
  273. offset = 0; // Always after the first time
  274. }
  275. }
  276. /**
  277. * Free the blocks up to the position <code>pos</code>.
  278. * The byte at <code>pos</code> remains available.
  279. *
  280. * @exception IndexOutOfBoundsException if <code>pos</code>
  281. * is in a block that has already been disposed.
  282. */
  283. public void disposeBefore(long pos) {
  284. long index = posBUFFER_LENGTH;
  285. if (index < cacheStart) {
  286. throw new IndexOutOfBoundsException("pos already disposed");
  287. }
  288. long numBlocks = Math.min(index - cacheStart, cache.size());
  289. for (long i = 0; i < numBlocks; i++) {
  290. cache.remove(0);
  291. }
  292. this.cacheStart = index;
  293. }
  294. /**
  295. * Erase the entire cache contents and reset the length to 0.
  296. * The cache object may subsequently be reused as though it had just
  297. * been allocated.
  298. */
  299. public void reset() {
  300. cache.clear();
  301. cacheStart = 0;
  302. length = 0L;
  303. }
  304. }