    001package net.minecraft.world.chunk.storage;
    002
    003import java.io.DataInputStream;
    004import java.io.DataOutputStream;
    005import java.io.File;
    006import java.io.IOException;
    007import java.util.HashMap;
    008import java.util.Iterator;
    009import java.util.Map;
    010
    011public 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 file2 = new File(par0File, "region");
    019        File file3 = new File(file2, "r." + (par1 >> 5) + "." + (par2 >> 5) + ".mca");
    020        RegionFile regionfile = (RegionFile)regionsByFilename.get(file3);
    021
    022        if (regionfile != null)
    023        {
    024            return regionfile;
    025        }
    026        else
    027        {
    028            if (!file2.exists())
    029            {
    030                file2.mkdirs();
    031            }
    032
    033            if (regionsByFilename.size() >= 256)
    034            {
    035                clearRegionFileReferences();
    036            }
    037
    038            RegionFile regionfile1 = new RegionFile(file3);
    039            regionsByFilename.put(file3, regionfile1);
    040            return regionfile1;
    041        }
    042    }
    043
    044    /**
    045     * Saves the current Chunk Map Cache
    046     */
    047    public static synchronized void clearRegionFileReferences()
    048    {
    049        Iterator iterator = regionsByFilename.values().iterator();
    050
    051        while (iterator.hasNext())
    052        {
    053            RegionFile regionfile = (RegionFile)iterator.next();
    054
    055            try
    056            {
    057                if (regionfile != null)
    058                {
    059                    regionfile.close();
    060                }
    061            }
    062            catch (IOException ioexception)
    063            {
    064                ioexception.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 regionfile = createOrLoadRegionFile(par0File, par1, par2);
    077        return regionfile.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 regionfile = createOrLoadRegionFile(par0File, par1, par2);
    086        return regionfile.getChunkDataOutputStream(par1 & 31, par2 & 31);
    087    }
    088}