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