- synchronized void fillCache(int howMuch) throws IOException {
- if (filling) { try { wait(); } catch (InterruptedException e) { }; return; }
- filling = true;
- grow(size + howMuch);
- int ret = is.read(cache, size, howMuch);
- if (ret == -1) {
- eof = true;
- // FIXME: probably a race here
- if (diskCache != null && !diskCache.exists())
- try {
- File cacheFile = new File(diskCache + ".incomplete");
- FileOutputStream cacheFileStream = new FileOutputStream(cacheFile);
- cacheFileStream.write(cache, 0, size);
- cacheFileStream.close();
- cacheFile.renameTo(diskCache);
- } catch (IOException e) {
- Log.info(this, "exception thrown while writing disk cache");
- Log.info(this, e);
- }
+ if (out == null) {
+ if (size + need > cache.length) {
+ byte[] newCache = new byte[Math.max(cache.length * 2, (int)(size + need))];
+ System.arraycopy(cache, 0, newCache, 0, (int)size);
+ cache = newCache;
+ }
+ ret = is.read(cache, (int)size, need);
+ } else {
+ ret = is.read(cache, 0, Math.min(cache.length, need));
+ if (ret > 0) out.write(cache, 0, ret);