Module java.desktop

Class FileCacheImageOutputStream

所有已实现的接口:
Closeable, DataInput, DataOutput, AutoCloseable, ImageInputStream, ImageOutputStream

public class FileCacheImageOutputStream extends ImageOutputStreamImpl
实现了ImageOutputStream,将其输出写入常规OutputStream的类。使用文件缓存数据,直到刷新到输出流为止。
  • Constructor Details

    • FileCacheImageOutputStream

      public FileCacheImageOutputStream(OutputStream stream, File cacheDir) throws IOException
      构造一个将写入给定outputStreamFileCacheImageOutputStream

      使用临时文件作为缓存。如果cacheDirnull且为目录,则文件将在那里创建。如果为null,则将使用系统相关的默认临时文件目录(有关详细信息,请参阅File.createTempFile的文档)。

      参数:
      stream - 要写入的OutputStream
      cacheDir - 指示应在其中创建缓存文件的File,或null以使用系统目录。
      抛出:
      IllegalArgumentException - 如果streamnull
      IllegalArgumentException - 如果cacheDirnull但不是目录。
      IOException - 如果无法创建缓存文件。
  • Method Details

    • read

      public int read() throws IOException
      从类中复制的描述: ImageInputStreamImpl
      从流中读取一个字节并将其作为介于0和255之间的int返回。如果达到EOF,则返回-1

      子类必须为此方法提供实现。子类实现应在退出前更新流位置。

      读取发生前,流中的位偏移必须重置为零。

      指定者:
      read 在接口 ImageInputStream
      指定者:
      read 在类 ImageInputStreamImpl
      返回:
      流中下一个字节的值,如果达到EOF,则返回-1
      抛出:
      IOException - 如果流已关闭。
    • read

      public int read(byte[] b, int off, int len) throws IOException
      从类中复制的描述: ImageInputStreamImpl
      从流中读取最多len个字节,并将它们存储到从索引off开始的b中。如果无法读取任何字节,因为已到达流的末尾,则返回-1

      读取发生前,流中的位偏移必须重置为零。

      子类必须为此方法提供实现。子类实现应在退出前更新流位置。

      指定者:
      read 在接口 ImageInputStream
      指定者:
      read 在类 ImageInputStreamImpl
      参数:
      b - 要写入的字节数组。
      off - 要写入的数据中的起始位置。
      len - 要读取的最大字节数。
      返回:
      实际读取的字节数,或-1表示EOF。
      抛出:
      IOException - 如果发生I/O错误。
    • write

      public void write(int b) throws IOException
      从接口中复制的描述: ImageOutputStream
      将一个字节写入当前位置的流。忽略b的24个高阶位。

      如果流中的位偏移不为零,则首先填充当前字节的剩余部分并写出。写入后,位偏移将为0。实现者可以使用ImageOutputStreamImplflushBits方法来保证这一点。

      参数:
      b - 要写入其低8位的int
      抛出:
      IOException - 如果发生I/O错误。
    • write

      public void write(byte[] b, int off, int len) throws IOException
      从接口中复制的描述: ImageOutputStream
      将一系列字节写入当前位置的流。如果len为0,则不写入任何内容。首先写入字节b[off],然后是字节b[off + 1],依此类推。

      如果流中的位偏移不为零,则首先填充当前字节的剩余部分并写出。写入后,位偏移将为0。实现者可以使用ImageOutputStreamImplflushBits方法来保证这一点。

      参数:
      b - 要写入的byte数组。
      off - 数据中的起始偏移量。
      len - 要写入的byte数。
      抛出:
      IOException - 如果发生I/O错误。
    • length

      public long length()
      从类中复制的描述: ImageInputStreamImpl
      返回-1L,表示流的长度未知。子类必须重写此方法以提供实际长度信息。
      指定者:
      length 在接口 ImageInputStream
      覆盖:
      length 在类 ImageInputStreamImpl
      返回:
      -1L表示长度未知。
    • seek

      public void seek(long pos) throws IOException
      设置当前流位置并将位偏移重置为0。可以寻址超出文件末尾;只有在执行读取时才会抛出EOFException。直到执行写入操作,文件长度不会增加。
      参数:
      pos - 包含所需文件指针位置的long
      抛出:
      IndexOutOfBoundsException - 如果pos小于刷新位置。
      IOException - 如果发生其他I/O错误。
    • isCached

      public boolean isCached()
      返回true,因为此ImageOutputStream缓存数据以允许向后查找。
      指定由:
      isCached 在接口 ImageInputStream
      覆盖:
      isCached 在类 ImageInputStreamImpl
      返回:
      true.
      参见:
    • isCachedFile

      public boolean isCachedFile()
      返回true,因为这个ImageOutputStream维护一个文件缓存。
      指定由:
      isCachedFile 在接口 ImageInputStream
      覆盖:
      isCachedFile 在类 ImageInputStreamImpl
      返回:
      true.
      参见:
    • isCachedMemory

      public boolean isCachedMemory()
      返回false,因为这个ImageOutputStream不维护主内存缓存。
      指定由:
      isCachedMemory 在接口 ImageInputStream
      覆盖:
      isCachedMemory 在类 ImageInputStreamImpl
      返回:
      false.
      参见:
    • close

      public void close() throws IOException
      关闭这个FileCacheImageOutputStream。所有待处理数据都会刷新到输出,缓存文件会被关闭和删除。目标OutputStream不会被关闭。
      抛出:
      IOException - 如果发生错误。
    • flushBefore

      public void flushBefore(long pos) throws IOException
      从接口复制的描述: ImageOutputStream
      刷新给定位置之前的所有数据到底层目标,比如一个OutputStreamFile。尝试定位到已刷新部分的流将导致IndexOutOfBoundsException
      参数:
      pos - 包含要刷新到目标的流前缀的长度的long
      抛出:
      IOException - 如果发生I/O错误。