Java – using LinkedHashMap’s multidimensional byte array… Is there a better way?
I'm new to Java programming, so please forgive me for my new question:)
I'm using LinkedHashMap as a file cache for the application I'm modifying to help develop I do this to reduce I / O overhead and improve performance The problem is to introduce overhead in many other ways
The relevant sources look like this
// 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 method of loading data is just the 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);
This is a cached declaration
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; } }
Because I am very unfamiliar with Java flow etiquette, the above code may look hasty I am also convinced that there are more effective ways to do this, such as the location of the buffer
Anyway, my main problem is that whenever I need to do anything with a single block, I have to convert all grid data into objects, send it to the cache, and then write a file This is a very inefficient way of doing things I wonder if there is a better way to do this, so I didn't get (); When I only need to access that block, the entire byte [8] [8] [] array I like to do chunk = cache Get [CX] CZ, but I'm sure it's not that simple
Anyway, as I said earlier, please forgive the question. If the answer is obvious, I'm just an insignificant new thing: D. I'm very grateful for any input:)
thank you.
Solution
If your goal is to reduce I / O overhead, how do you put byte [] [] objects in a wrapper object, which adds the concept of dirty flags?
In this way, you can reduce the number of file writes during modification, write dirty objects to disk only after using the cache, or delete the oldest objects when inserting the full cache