From d61c21f60e4b2d0a1cd68979632a8578f4c56b17 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Sat, 24 Sep 2016 14:41:33 +1000 Subject: [PATCH] Fix error from flushing on different thread --- .../object/changeset/DiskStorageHistory.java | 64 +++++++------- .../changeset/MemoryOptimizedHistory.java | 84 ++++++++++--------- 2 files changed, 78 insertions(+), 70 deletions(-) diff --git a/core/src/main/java/com/boydti/fawe/object/changeset/DiskStorageHistory.java b/core/src/main/java/com/boydti/fawe/object/changeset/DiskStorageHistory.java index 32124eff..962f9c88 100644 --- a/core/src/main/java/com/boydti/fawe/object/changeset/DiskStorageHistory.java +++ b/core/src/main/java/com/boydti/fawe/object/changeset/DiskStorageHistory.java @@ -134,33 +134,35 @@ public class DiskStorageHistory extends FaweStreamChangeSet { @Override public boolean flush() { - super.flush(); - boolean flushed = osBD != null || osNBTF != null || osNBTT != null && osENTCF != null || osENTCT != null; - try { - if (osBD != null) { - osBD.close(); - osBD = null; + synchronized (this) { + super.flush(); + boolean flushed = osBD != null || osNBTF != null || osNBTT != null && osENTCF != null || osENTCT != null; + try { + if (osBD != null) { + osBD.close(); + osBD = null; + } + if (osNBTF != null) { + osNBTF.close(); + osNBTF = null; + } + if (osNBTT != null) { + osNBTT.close(); + osNBTT = null; + } + if (osENTCF != null) { + osENTCF.close(); + osENTCF = null; + } + if (osENTCT != null) { + osENTCT.close(); + osENTCT = null; + } + } catch (Exception e) { + MainUtil.handleError(e); } - if (osNBTF != null) { - osNBTF.close(); - osNBTF = null; - } - if (osNBTT != null) { - osNBTT.close(); - osNBTT = null; - } - if (osENTCF != null) { - osENTCF.close(); - osENTCF = null; - } - if (osENTCT != null) { - osENTCT.close(); - osENTCT = null; - } - } catch (Exception e) { - MainUtil.handleError(e); + return flushed; } - return flushed; } @Override @@ -199,11 +201,13 @@ public class DiskStorageHistory extends FaweStreamChangeSet { if (osBD != null) { return osBD; } - bdFile.getParentFile().mkdirs(); - bdFile.createNewFile(); - osBD = getCompressedOS(new FileOutputStream(bdFile)); - writeHeader(osBD, x, y, z); - return osBD; + synchronized (this) { + bdFile.getParentFile().mkdirs(); + bdFile.createNewFile(); + osBD = getCompressedOS(new FileOutputStream(bdFile)); + writeHeader(osBD, x, y, z); + return osBD; + } } @Override diff --git a/core/src/main/java/com/boydti/fawe/object/changeset/MemoryOptimizedHistory.java b/core/src/main/java/com/boydti/fawe/object/changeset/MemoryOptimizedHistory.java index db5c2500..abf443e1 100644 --- a/core/src/main/java/com/boydti/fawe/object/changeset/MemoryOptimizedHistory.java +++ b/core/src/main/java/com/boydti/fawe/object/changeset/MemoryOptimizedHistory.java @@ -48,42 +48,44 @@ public class MemoryOptimizedHistory extends FaweStreamChangeSet { @Override public boolean flush() { - super.flush(); - try { - if (idsStream != null) { - idsStreamZip.close(); - size = idsStream.getSize(); - ids = idsStream.toByteArrays(); - idsStream = null; - idsStreamZip = null; + synchronized (this) { + super.flush(); + try { + if (idsStream != null) { + idsStreamZip.close(); + size = idsStream.getSize(); + ids = idsStream.toByteArrays(); + idsStream = null; + idsStreamZip = null; + } + if (entCStream != null) { + entCStreamZip.close(); + entC = entCStream.toByteArrays(); + entCStream = null; + entCStreamZip = null; + } + if (entRStream != null) { + entRStreamZip.close(); + entR = entRStream.toByteArrays(); + entRStream = null; + entRStreamZip = null; + } + if (tileCStream != null) { + tileCStreamZip.close(); + tileC = tileCStream.toByteArrays(); + tileCStream = null; + tileCStreamZip = null; + } + if (tileRStream != null) { + tileRStreamZip.close(); + tileR = tileRStream.toByteArrays(); + tileRStream = null; + tileRStreamZip = null; + } + return true; + } catch (IOException e) { + MainUtil.handleError(e); } - if (entCStream != null) { - entCStreamZip.close(); - entC = entCStream.toByteArrays(); - entCStream = null; - entCStreamZip = null; - } - if (entRStream != null) { - entRStreamZip.close(); - entR = entRStream.toByteArrays(); - entRStream = null; - entRStreamZip = null; - } - if (tileCStream != null) { - tileCStreamZip.close(); - tileC = tileCStream.toByteArrays(); - tileCStream = null; - tileCStreamZip = null; - } - if (tileRStream != null) { - tileRStreamZip.close(); - tileR = tileRStream.toByteArrays(); - tileRStream = null; - tileRStreamZip = null; - } - return true; - } catch (IOException e) { - MainUtil.handleError(e); } return false; } @@ -110,11 +112,13 @@ public class MemoryOptimizedHistory extends FaweStreamChangeSet { if (idsStreamZip != null) { return idsStreamZip; } - setOrigin(x, z); - idsStream = new FastByteArrayOutputStream(Settings.HISTORY.BUFFER_SIZE); - idsStreamZip = getCompressedOS(idsStream); - writeHeader(idsStreamZip, x, y, z); - return idsStreamZip; + synchronized (this) { + setOrigin(x, z); + idsStream = new FastByteArrayOutputStream(Settings.HISTORY.BUFFER_SIZE); + idsStreamZip = getCompressedOS(idsStream); + writeHeader(idsStreamZip, x, y, z); + return idsStreamZip; + } } @Override