    001    package net.minecraft.src;
    002
    003    import java.io.DataInputStream;
    004    import java.io.DataOutputStream;
    005    import java.io.File;
    006    import java.io.IOException;
    007    import java.lang.ref.Reference;
    008    import java.lang.ref.SoftReference;
    009    import java.util.HashMap;
    010    import java.util.Iterator;
    011    import java.util.Map;
    012
    013    public class RegionFileCache
    014    {
    015        /** A map containing Files as keys and RegionFiles as values */
    016        private static final Map regionsByFilename = new HashMap();
    017
    018        public static synchronized RegionFile createOrLoadRegionFile(File par0File, int par1, int par2)
    019        {
    020            File var3 = new File(par0File, "region");
    021            File var4 = new File(var3, "r." + (par1 >> 5) + "." + (par2 >> 5) + ".mca");
    022            Reference var5 = (Reference)regionsByFilename.get(var4);
    023            RegionFile var6;
    024
    025            if (var5 != null)
    026            {
    027                var6 = (RegionFile)var5.get();
    028
    029                if (var6 != null)
    030                {
    031                    return var6;
    032                }
    033            }
    034
    035            if (!var3.exists())
    036            {
    037                var3.mkdirs();
    038            }
    039
    040            if (regionsByFilename.size() >= 256)
    041            {
    042                clearRegionFileReferences();
    043            }
    044
    045            var6 = new RegionFile(var4);
    046            regionsByFilename.put(var4, new SoftReference(var6));
    047            return var6;
    048        }
    049
    050        /**
    051         * Saves the current Chunk Map Cache
    052         */
    053        public static synchronized void clearRegionFileReferences()
    054        {
    055            Iterator var0 = regionsByFilename.values().iterator();
    056
    057            while (var0.hasNext())
    058            {
    059                Reference var1 = (Reference)var0.next();
    060
    061                try
    062                {
    063                    RegionFile var2 = (RegionFile)var1.get();
    064
    065                    if (var2 != null)
    066                    {
    067                        var2.close();
    068                    }
    069                }
    070                catch (IOException var3)
    071                {
    072                    var3.printStackTrace();
    073                }
    074            }
    075
    076            regionsByFilename.clear();
    077        }
    078
    079        /**
    080         * Returns an input stream for the specified chunk. Args: worldDir, chunkX, chunkZ
    081         */
    082        public static DataInputStream getChunkInputStream(File par0File, int par1, int par2)
    083        {
    084            RegionFile var3 = createOrLoadRegionFile(par0File, par1, par2);
    085            return var3.getChunkDataInputStream(par1 & 31, par2 & 31);
    086        }
    087
    088        /**
    089         * Returns an output stream for the specified chunk. Args: worldDir, chunkX, chunkZ
    090         */
    091        public static DataOutputStream getChunkOutputStream(File par0File, int par1, int par2)
    092        {
    093            RegionFile var3 = createOrLoadRegionFile(par0File, par1, par2);
    094            return var3.getChunkDataOutputStream(par1 & 31, par2 & 31);
    095        }
    096    }