我对 Java 编程非常陌生,所以请原谅我的新问题:)。
为了协助开发,我正在使用 LinkedHashMap 作为我正在修改的应用程序的文件缓存。我这样做是为了减少 I/O 开销,从而提高性能。这样做的问题是开销以许多其他方式引入。
相关来源看起来像这样。
// Retrieve Data From LinkedHashMap
byte grid[][][] = null;
if(file.exists())
{
if (!cache.containsKey(file))
{
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis, 16384);
ObjectInputStream ois = new ObjectInputStream(bis);
cache.put(file, ois.readObject());
ois.close();
}
grid = (byte[][][]) cache.get(file);
} else {
grid = new byte[8][8][];
}
以下是我用来保存数据的。加载数据的方法正好相反。
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gos = new GZIPOutputStream(baos){{ def.setLevel(2);}};
BufferedOutputStream bos = new BufferedOutputStream(gos, 16384);
DataOutputStream dos = new DataOutputStream(bos);
// Some code writes to dos
dos.close();
byte[cx][cz] = baos.toByteArray();
baos.close();
cache.put(file, grid);
这是缓存的声明。
private static LinkedHashMap<File, Object> cache = new LinkedHashMap<File, Object>(64, 1.1f, true)
{protected boolean removeEldestEntry(Map.Entry<File, Object> eldest)
{
return size() > 64;
}
}
由于本人对Java流的礼仪很不熟悉,很可能上面的代码看起来很草率。我也确信有更有效的方法来完成上述操作,例如将缓冲区放在哪里。
无论如何,我的主要问题是:每当我需要对单个块做任何事情时,我必须将所有网格数据转换为一个对象,将其发送到缓存,然后写入文件。这是一种非常低效的做事方式。我想知道是否有更好的方法可以做到这一点,这样我就不必 get(); 当我只需要访问那个块时,整个 byte[8][8][] 数组。我很想做类似 chunk = cache.get[cx]cz 之类的事情,但我敢肯定它不是那么简单。
无论如何,正如我之前所说,如果答案很明显,请原谅这个问题,我只是一个卑微的新手:D。我非常感谢任何输入:)。
谢谢。