From 92a6d2c1476fd562721f231f89afba9342ed8a20 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 26 Sep 2014 12:29:04 +0200 Subject: Bug 1080 - Add write support for memory mapped big file I/O via specialized OutputStream impl. Added MappedByteBufferOutputStream as a child instance of MappedByteBufferInputStream, since the latter already manages the file's mapped buffer slices. Current design is: - MappedByteBufferInputStream (parent) - MappedByteBufferOutputStream this is due to InputStream and OutputStream not being interfaces, but most functionality is provided in one class. We could redesign both as follows: - MappedByteBufferIOStream (parent) - MappedByteBufferInputStream - MappedByteBufferOutputStream This might visualize things better .. dunno whether its worth the extra redirection. +++ MappedByteBufferInputStream: - Adding [file] resize support via custom FileResizeOp - All construction happens via ctors - Handle refCount, incr. by ctor and getOutputStream(..), decr by close - Check whether stream is closed already -> IOException - Simplify / Reuse code MappedByteBufferOutputStream: - Adding simple write operations --- .../common/nio/MappedByteBufferInputStream.java | 395 ++++++++++++++++----- .../common/nio/MappedByteBufferOutputStream.java | 156 ++++++++ 2 files changed, 462 insertions(+), 89 deletions(-) create mode 100644 src/java/com/jogamp/common/nio/MappedByteBufferOutputStream.java (limited to 'src/java') diff --git a/src/java/com/jogamp/common/nio/MappedByteBufferInputStream.java b/src/java/com/jogamp/common/nio/MappedByteBufferInputStream.java index 5ac1ffb..67cbbbe 100644 --- a/src/java/com/jogamp/common/nio/MappedByteBufferInputStream.java +++ b/src/java/com/jogamp/common/nio/MappedByteBufferInputStream.java @@ -29,11 +29,14 @@ package com.jogamp.common.nio; import java.io.IOException; import java.io.InputStream; +import java.io.PrintStream; +import java.io.RandomAccessFile; import java.lang.ref.WeakReference; import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; +import java.nio.channels.FileChannel.MapMode; import java.security.AccessController; import java.security.PrivilegedAction; @@ -92,6 +95,24 @@ public class MappedByteBufferInputStream extends InputStream { FLUSH_PRE_HARD }; + /** + * File resize interface allowing a file to change its size, + * e.g. via {@link RandomAccessFile#setLength(long)}. + */ + public static interface FileResizeOp { + /** + * @param newSize the new file size + * @throws IOException if file size change is not supported or any other I/O error occurs + */ + void setLength(final long newSize) throws IOException; + } + private static final FileResizeOp NoFileResize = new FileResizeOp() { + @Override + public void setLength(final long newSize) throws IOException { + throw new IOException("file size change not supported"); + } + }; + /** * Default slice shift, i.e. 1L << shift, denoting slice size in MiB: *