Code explanation

Could someone please enlighten me on what the purpose of the following code from the TextureLoader class is:


    private synchronized BufferedImage loadImageFast(String name, boolean expectAlpha) {
        try {

            String cacheName = null;
            if (cachePath != null) {
                File f = new File(name);
                cacheName = cachePath+f.getName() + ".img";

                f = new File(cacheName);
                if (f.exists()) {
                    FileInputStream fs = new FileInputStream(f);
                    FileChannel fc = fs.getChannel();
                    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY,0,f.length());
                    boolean hasAlpha = (bb.getInt()==1) ? true : false;
                    int width = bb.getInt();
                    int height = bb.getInt();
                    DirectBufferedImage im = null;
                    if (hasAlpha) im = (DirectBufferedImage)DirectBufferedImage.getDirectImageRGBA(width,height);
                    else im = (DirectBufferedImage)DirectBufferedImage.getDirectImageRGB(width,height);
                    bb.get(im.getBackingStore());
                    return im;
                }
            }


I don’t understand the purpose of the MappedByteBuffer and FileChannel objects. Why not simply read the width and height from the FileInputStream. and what is the point of the following line:


                    bb.get(im.getBackingStore());

Perhaps is it (again) a problem of low/high bit weight ? :stuck_out_tongue:

That feature is designed to sacrifice disk space for faster texture loading. If it turned on then the first time the texture is read it is stored in uncompressed form on the disk. The second time you run it maps the file into memory using NIO and just copies the data directly into the bytebuffer of the image, which is in turn passed directly to OpenGL