    001    package net.minecraft.world.chunk.storage;
    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 var5 = (RegionFile)regionsByFilename.get(var4);
    021
    022            if (var5 != null)
    023            {
    024                return var5;
    025            }
    026            else
    027            {
    028                if (!var3.exists())
    029                {
    030                    var3.mkdirs();
    031                }
    032
    033                if (regionsByFilename.size() >= 256)
    034                {
    035                    clearRegionFileReferences();
    036                }
    037
    038                RegionFile var6 = new RegionFile(var4);
    039                regionsByFilename.put(var4, var6);
    040                return var6;
    041            }
    042        }
    043
    044        /**
    045         * Saves the current Chunk Map Cache
    046         */
    047        public static synchronized void clearRegionFileReferences()
    048        {
    049            Iterator var0 = regionsByFilename.values().iterator();
    050
    051            while (var0.hasNext())
    052            {
    053                RegionFile var1 = (RegionFile)var0.next();
    054
    055                try
    056                {
    057                    if (var1 != null)
    058                    {
    059                        var1.close();
    060                    }
    061                }
    062                catch (IOException var3)
    063                {
    064                    var3.printStackTrace();
    065                }
    066            }
    067
    068            regionsByFilename.clear();
    069        }
    070
    071        /**
    072         * Returns an input stream for the specified chunk. Args: worldDir, chunkX, chunkZ
    073         */
    074        public static DataInputStream getChunkInputStream(File par0File, int par1, int par2)
    075        {
    076            RegionFile var3 = createOrLoadRegionFile(par0File, par1, par2);
    077            return var3.getChunkDataInputStream(par1 & 31, par2 & 31);
    078        }
    079
    080        /**
    081         * Returns an output stream for the specified chunk. Args: worldDir, chunkX, chunkZ
    082         */
    083        public static DataOutputStream getChunkOutputStream(File par0File, int par1, int par2)
    084        {
    085            RegionFile var3 = createOrLoadRegionFile(par0File, par1, par2);
    086            return var3.getChunkDataOutputStream(par1 & 31, par2 & 31);
    087        }
    088    }