Source for javax.imageio.stream.FileCacheImageOutputStream

   1: /* FileCacheImageOutputStream.java --
   2:    Copyright (C) 2004  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.imageio.stream;
  40: 
  41: import java.io.File;
  42: import java.io.IOException;
  43: import java.io.OutputStream;
  44: import java.io.RandomAccessFile;
  45: 
  46: /**
  47:  * @author Michael Koch (konqueror@gmx.de)
  48:  */
  49: public class FileCacheImageOutputStream extends ImageOutputStreamImpl
  50: {
  51:   private OutputStream stream;
  52:   private File cacheFile;
  53:   private RandomAccessFile cache;
  54:   private long maxPos;
  55: 
  56:   public FileCacheImageOutputStream(OutputStream s, File cacheDir)
  57:     throws IOException
  58:   {
  59:     stream = s;
  60:     cacheFile = File.createTempFile("imageio", ".tmp", cacheDir);
  61:     cache = new RandomAccessFile(cacheFile, "rw");
  62:     maxPos = 0;
  63:   }
  64: 
  65:   public void close()
  66:     throws IOException
  67:   {
  68:     maxPos = cache.length();
  69:     seek(maxPos);
  70:     flushBefore(maxPos);
  71:     super.close();
  72:     cache.close();
  73:     cacheFile.delete();
  74:     stream.flush();
  75:     stream = null;
  76:   }
  77: 
  78:   public boolean isCached()
  79:   {
  80:     return true;
  81:   }
  82: 
  83:   public boolean isCachedFile()
  84:   {
  85:     return true;
  86:   }
  87: 
  88:   public boolean isCachedMemory()
  89:   {
  90:     return false;
  91:   }
  92: 
  93:   public int read()
  94:     throws IOException
  95:   {
  96:     bitOffset = 0;
  97:     int val = cache.read();
  98:     if (val != -1)
  99:       streamPos++;
 100:     return val;
 101:   }
 102: 
 103:   public int read(byte[] data, int offset, int len)
 104:     throws IOException
 105:   {
 106:     bitOffset = 0;
 107:     int num = cache.read(data, offset, len);
 108:     if (num != -1)
 109:       streamPos += num;
 110:     return num;
 111:   }
 112: 
 113:   public void write(byte[] data, int offset, int len)
 114:     throws IOException
 115:   {
 116:     flushBits();
 117:     cache.write(data, offset, len);
 118:     streamPos += len;
 119:     maxPos = Math.max(streamPos, maxPos);
 120:   }
 121: 
 122:   public void write(int value)
 123:     throws IOException
 124:   {
 125:     flushBits();
 126:     cache.write(value);
 127:     streamPos++;
 128:     maxPos = Math.max(streamPos, maxPos);
 129:   }
 130: 
 131:   public long length()
 132:   {
 133:     long l;
 134:     try
 135:       {
 136:         l = cache.length();
 137:       }
 138:     catch (IOException ex)
 139:       {
 140:         l = -1;
 141:       }
 142:     return l;
 143:   }
 144: 
 145:   public void seek(long pos)
 146:     throws IOException
 147:   {
 148:     checkClosed();
 149:     if (pos < flushedPos)
 150:       throw new IndexOutOfBoundsException();
 151:     cache.seek(pos);
 152:     streamPos = cache.getFilePointer();
 153:     maxPos = Math.max(streamPos, maxPos);
 154:     bitOffset = 0;
 155:   }
 156: 
 157:   public void flushBefore(long pos)
 158:     throws IOException
 159:   {
 160:     long oldPos = flushedPos;
 161:     super.flushBefore(pos);
 162:     long flushed = flushedPos - oldPos;
 163:     if (flushed > 0)
 164:       {
 165:         int len = 512;
 166:         byte[] buf = new byte[len];
 167:         cache.seek(oldPos);
 168:         while (flushed > 0)
 169:           {
 170:             int l = (int) Math.min(flushed, len);
 171:             cache.readFully(buf, 0, l);
 172:             stream.write(buf, 0, l);
 173:             flushed -= l;
 174:           }
 175:         stream.flush();
 176:       }
 177:   }
 178: }