SHARE
TWEET

Untitled

a guest Feb 16th, 2011 5,616 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  ** 2011 January 5
  3.  **
  4.  ** The author disclaims copyright to this source code.  In place of
  5.  ** a legal notice, here is a blessing:
  6.  **
  7.  **    May you do good and not evil.
  8.  **    May you find forgiveness for yourself and forgive others.
  9.  **    May you share freely, never taking more than you give.
  10.  */
  11.  
  12. /*
  13.  * 2011 February 16
  14.  *
  15.  * This source code is based on the work of Scaevolus (see notice above).
  16.  * It has been slightly modified by Mojang AB to limit the maximum cache
  17.  * size (relevant to extremely big worlds on Linux systems with limited
  18.  * number of file handles). The region files are postfixed with ".mcr"
  19.  * (Minecraft region file) instead of ".data" to differentiate from the
  20.  * original McRegion files.
  21.  *
  22.  */
  23.  
  24. // A simple cache and wrapper for efficiently multiple RegionFiles simultaneously.
  25.  
  26. import java.io.*;
  27. import java.lang.ref.*;
  28. import java.util.*;
  29.  
  30. public class RegionFileCache {
  31.  
  32.     private static final int MAX_CACHE_SIZE = 256;
  33.  
  34.  
  35.     private static final Map<File, Reference<RegionFile>> cache = new HashMap<File, Reference<RegionFile>>();
  36.  
  37.     private RegionFileCache() {
  38.     }
  39.  
  40.     public static synchronized RegionFile getRegionFile(File basePath, int chunkX, int chunkZ) {
  41.         File regionDir = new File(basePath, "region");
  42.         File file = new File(regionDir, "r." + (chunkX >> 5) + "." + (chunkZ >> 5) + ".mcr");
  43.  
  44.         Reference<RegionFile> ref = cache.get(file);
  45.  
  46.         if (ref != null && ref.get() != null) {
  47.             return ref.get();
  48.         }
  49.  
  50.         if (!regionDir.exists()) {
  51.             regionDir.mkdirs();
  52.         }
  53.  
  54.         if (cache.size() >= MAX_CACHE_SIZE) {
  55.             RegionFileCache.clear();
  56.         }
  57.  
  58.         RegionFile reg = new RegionFile(file);
  59.         cache.put(file, new SoftReference<RegionFile>(reg));
  60.         return reg;
  61.     }
  62.  
  63.     public static synchronized void clear() {
  64.         for (Reference<RegionFile> ref : cache.values()) {
  65.             try {
  66.                 if (ref.get() != null) {
  67.                     ref.get().close();
  68.                 }
  69.             } catch (IOException e) {
  70.                 e.printStackTrace();
  71.             }
  72.         }
  73.         cache.clear();
  74.     }
  75.  
  76.     public static int getSizeDelta(File basePath, int chunkX, int chunkZ) {
  77.         RegionFile r = getRegionFile(basePath, chunkX, chunkZ);
  78.         return r.getSizeDelta();
  79.     }
  80.  
  81.     public static DataInputStream getChunkDataInputStream(File basePath, int chunkX, int chunkZ) {
  82.         RegionFile r = getRegionFile(basePath, chunkX, chunkZ);
  83.         return r.getChunkDataInputStream(chunkX & 31, chunkZ & 31);
  84.     }
  85.  
  86.     public static DataOutputStream getChunkDataOutputStream(File basePath, int chunkX, int chunkZ) {
  87.         RegionFile r = getRegionFile(basePath, chunkX, chunkZ);
  88.         return r.getChunkDataOutputStream(chunkX & 31, chunkZ & 31);
  89.     }
  90. }
RAW Paste Data
Top