X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Forg%2Fxwt%2Futil%2FCachedInputStream.java;h=cd855630730e39a15f1b9c004e20b0a38e3990d7;hb=8d5c8bc003e624cb59618a516367e1f1e68382fe;hp=fdf8a78ce4cb9bb2f95c1d64791fa519eae58cfc;hpb=663e3ff353507696eb6161516bac435e92006192;p=org.ibex.core.git diff --git a/src/org/xwt/util/CachedInputStream.java b/src/org/xwt/util/CachedInputStream.java index fdf8a78..cd85563 100644 --- a/src/org/xwt/util/CachedInputStream.java +++ b/src/org/xwt/util/CachedInputStream.java @@ -1,6 +1,14 @@ +// Copyright (C) 2003 Adam Megacz all rights reserved. +// +// You may modify, copy, and redistribute this code under the terms of +// the GNU Library Public License version 2.1, with the exception of +// the portion of clause 6a after the semicolon (aka the "obnoxious +// relink clause") + package org.xwt.util; import java.io.*; +// FEATURE: don't use a byte[] if we have a diskCache file /** * Wraps around an InputStream, caching the stream in a byte[] as it * is read and permitting multiple simultaneous readers @@ -12,9 +20,19 @@ public class CachedInputStream { byte[] cache = new byte[1024 * 128]; int size = 0; final InputStream is; + File diskCache; - public CachedInputStream(InputStream is) { this.is = is; } - public InputStream getInputStream() { return new SubStream(); } + public CachedInputStream(InputStream is) { this(is, null); } + public CachedInputStream(InputStream is, File diskCache) { + this.is = is; + this.diskCache = diskCache; + } + public InputStream getInputStream() throws IOException { + System.out.println("diskCache == " + diskCache); + System.out.println("diskCache.exists() == " + diskCache.exists()); + if (diskCache != null && diskCache.exists()) return new FileInputStream(diskCache); + return new SubStream(); + } public void grow(int newLength) { if (newLength < cache.length) return; @@ -28,16 +46,31 @@ public class CachedInputStream { filling = true; grow(size + howMuch); int ret = is.read(cache, size, howMuch); - if (ret == -1) eof = true; + if (ret == -1) { + eof = true; + // FIXME: probably a race here + if (diskCache != null && !diskCache.exists()) + try { + File cacheFile = new File(diskCache + ".tmp"); + FileOutputStream cacheFileStream = new FileOutputStream(cacheFile); + cacheFileStream.write(cache, 0, size); + cacheFileStream.close(); + cacheFile.renameTo(diskCache); + } catch (IOException e) { + Log.log(this, "exception thrown while writing disk cache"); + Log.log(this, e); + } + } else size += ret; filling = false; notifyAll(); } - private class SubStream extends InputStream { + private class SubStream extends InputStream implements KnownLength { int pos = 0; public int available() { return Math.max(0, size - pos); } public long skip(long n) throws IOException { pos += (int)n; return n; } // FEATURE: don't skip past EOF + public int getLength() { return eof ? size : is instanceof KnownLength ? ((KnownLength)is).getLength() : 0; } public int read() throws IOException { // FEATURE: be smarter here byte[] b = new byte[1]; int ret = read(b, 0, 1);