From 99b357956e4fd516ddd051d8a0f478c47faa40b1 Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Mon, 15 Jan 2018 06:21:23 +0100
Subject: MappedByteBufferInputStream: Add flushSlices() to release mapped
 slices to save mapped ByteBuffer memory

Also:
- fix dbgDump(..) FileChannel access, test if openend.
- add DEBUG dumps on CTOR, Close and notifyLengthChangeImpl
---
 .../common/nio/MappedByteBufferInputStream.java    | 39 +++++++++++++++++-----
 1 file changed, 31 insertions(+), 8 deletions(-)

(limited to 'src/java/com/jogamp/common/nio')

diff --git a/src/java/com/jogamp/common/nio/MappedByteBufferInputStream.java b/src/java/com/jogamp/common/nio/MappedByteBufferInputStream.java
index dbd3941..6a56d6e 100644
--- a/src/java/com/jogamp/common/nio/MappedByteBufferInputStream.java
+++ b/src/java/com/jogamp/common/nio/MappedByteBufferInputStream.java
@@ -184,10 +184,12 @@ public class MappedByteBufferInputStream extends InputStream {
             }
         }
         long fcSz = 0, pos = 0, rem = 0;
-        try {
-            fcSz = fc.size();
-        } catch (final IOException e) {
-            e.printStackTrace();
+        if( fc.isOpen() ) {
+            try {
+                fcSz = fc.size();
+            } catch (final IOException e) {
+                e.printStackTrace();
+            }
         }
         if( 0 < refCount ) {
             try {
@@ -228,6 +230,10 @@ public class MappedByteBufferInputStream extends InputStream {
         this.mark = -1;
 
         currentSlice().position(0);
+
+        if( MappedByteBufferInputStream.DEBUG ) {
+            this.dbgDump("CTOR", System.err);
+        }
     }
 
     /**
@@ -321,6 +327,9 @@ public class MappedByteBufferInputStream extends InputStream {
                 }
             }
         }
+        if( MappedByteBufferInputStream.DEBUG ) {
+            this.dbgDump("Close", System.err);
+        }
     }
 
     final FileChannel.MapMode getMapMode() { return mmode; }
@@ -432,10 +441,9 @@ public class MappedByteBufferInputStream extends InputStream {
             }
             position2( Math.min(prePosition, newTotalSize) ); // -> clipped position (set currSlice and re-map/-pos buffer)
         }
-        /* if( DEBUG ) {
-            System.err.println("notifyLengthChange.X: "+slices[currSlice]);
-            dbgDump("notifyLengthChange.X:", System.err);
-        } */
+        if( MappedByteBufferInputStream.DEBUG ) {
+            this.dbgDump("NotifyLengthChange", System.err);
+        }
     }
 
     /**
@@ -542,6 +550,21 @@ public class MappedByteBufferInputStream extends InputStream {
         }
     }
 
+    /**
+     * Releases the mapped {@link ByteBuffer} slices.
+     * @throws IOException if a buffer slice operation failed.
+     */
+    public final synchronized void flushSlices() throws IOException {
+        if( null != slices ) {
+            for(int i=0; i<sliceCount; i++) {
+                flushSlice(i, synchronous);
+            }
+        }
+        if( MappedByteBufferInputStream.DEBUG ) {
+            this.dbgDump("FlushSlices", System.err);
+        }
+    }
+
     synchronized void syncSlice(final ByteBuffer s) throws IOException {
         syncSlice(s, synchronous);
     }
-- 
cgit v1.2.3