I am VERY new to Java programming, so please forgive my newbies :).
I use LinkedHashMap as a file cache for an application that I modify to facilitate development. I do this to reduce I / O overhead and therefore improve performance. The problem is that overhead is introduced in many other ways.
The corresponding source is as follows.
// 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][]; }
The following is what I use to save data. The data loading method is the exact opposite.
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);
And here is the ad for the cache.
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; } }
Since I am very unfamiliar with the Java thread label, it is very likely that the code above looks sloppy. I am also sure that there are more efficient ways to do higher, for example, where to put the buffer.
In any case, my main problem is this: whenever I need to do something with one port, I need to convert all the grid data into an object, send it to the cache and write the file. This is a very inefficient way of doing things. I would like to know if there is a better way to do this, so I don't need to get (); the entire array of bytes [8] [8] [], when I need only access to this fragment. I would like to do something like chunk = cache.get [cx] cz, but I'm sure it is not so simple.
In any case, as I said earlier, please excuse the question, if the answer is obvious, I'm just a humble beginner: D. I am very grateful for any input :).
Thanks.