ImageIO读取中的控制缓存是什么意思 [英] What does control caching means in ImageIO read

查看:53
本文介绍了ImageIO读取中的控制缓存是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我唯一担心使用 BufferedImage 对象的是,对于 60000x32000 的非常大的图像,它会导致 JVM 在有限的 JVM 堆空间上以 OOM 关闭.然而,ImageIO.read 方法的 JavaDocs 说明了一些关于控制缓存"的内容.

My only fear of using BufferedImage object is that for a very large image of say 60000x32000 it will result in JVM shutting down with OOM on a limited JVM heap space. However, JavaDocs of ImageIO.read method says something about "control caching".

在这种情况下什么是控制缓存?

What is control caching in this context?

这是否意味着 ImageIO.read 对大图像使用磁盘上的图像缓存?

Does that mean ImageIO.read uses caching of image on disk for large image?

参考下面的JavaDocs和ImageIO.read方法:

Refer to JavaDocs and ImageIO.read method below:

       /**
         * Returns a <code>BufferedImage</code> as the result of decoding
         * a supplied <code>File</code> with an <code>ImageReader</code>
         * chosen automatically from among those currently registered.
         * The <code>File</code> is wrapped in an
         * <code>ImageInputStream</code>.  If no registered
         * <code>ImageReader</code> claims to be able to read the
         * resulting stream, <code>null</code> is returned.
         *
         * <p> The current cache settings from <code>getUseCache</code>and
         * <code>getCacheDirectory</code> will be used to control caching in the
         * <code>ImageInputStream</code> that is created.
         *
         * <p> Note that there is no <code>read</code> method that takes a
         * filename as a <code>String</code>; use this method instead after
         * creating a <code>File</code> from the filename.
         *
         * <p> This method does not attempt to locate
         * <code>ImageReader</code>s that can read directly from a
         * <code>File</code>; that may be accomplished using
         * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
         *
         * @param input a <code>File</code> to read from.
         *
         * @return a <code>BufferedImage</code> containing the decoded
         * contents of the input, or <code>null</code>.
         *
         * @exception IllegalArgumentException if <code>input</code> is
         * <code>null</code>.
         * @exception IOException if an error occurs during reading.
         */
        public static BufferedImage read(File input) throws IOException {
            if (input == null) {
                throw new IllegalArgumentException("input == null!");
            }
            if (!input.canRead()) {
                throw new IIOException("Can't read input file!");
            }

            ImageInputStream stream = createImageInputStream(input);
            if (stream == null) {
                throw new IIOException("Can't create an ImageInputStream!");
            }
            BufferedImage bi = read(stream);
            if (bi == null) {
                stream.close();
            }
            return bi;
        }

推荐答案

在这种情况下,它只是意味着 read 方法将使用 getUseCache 的设置>getCacheDirectory 控制是否允许缓存(getUseCache),如果允许,它可以存储临时文件的位置(getCacheDirectory).

In this context, it just means the read method will use the settings from getUseCache and getCacheDirectory to control if caching will be allowed (getUseCache) and if so, where it can store temporary files (getCacheDirectory).

ImageIO 中的缓存没有什么了不起的,可能只是用于处理不可查找的流.例如,当 ImageIO 需要确定图像的大小时,它可能需要读取流的重要部分.然后它可能需要再次重新读取流的那部分以进行实际解码.

The caching in ImageIO is nothing spectacular and probably only there for dealing with non-seekable streams. For example, when ImageIO needs to determine the size of the image, it may need to read a significant portion of the stream. It then may need to re-read that part of the stream again to do the actual decoding.

对于支持查找的文件和流,这不是问题,因为您可以在开始解码时重新阅读前面的部分.例如 HTTP 流,没有这样的选项,在这些情况下,流的一部分可能需要存储在某处以便稍后对其进行解码.这可以在内存 (MemoryCacheImageInputStream) 或临时文件 (FileCacheImageInputStream) 中.

For files and streams that support seeking, this is no issue as you can just re-read an earlier part when starting the decode. For say a HTTP stream, there is no such option and in those cases part of the stream may need to be stored somewhere in order to decode it later. This can be in memory (MemoryCacheImageInputStream) or in a temporary file (FileCacheImageInputStream).

使用哪种类型的流由 ImageIO 类决定,该类根据缓存设置和底层媒体动态决定.

Which type of stream is used is left up to the ImageIO class which dynamically decides this based on the cache settings and the underlying media.

所以,我认为这在处理非常大的图像时对您没有帮助.您仍然需要确保 VM 有足够的空间来解码它们.

So, I donot think this will help you when dealing with very large images. You will still need to make sure the VM has sufficient space for decoding them.

这篇关于ImageIO读取中的控制缓存是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆