Skip to content

Commit 28700f4

Browse files
committed
Add NIOService for working with NIO classes
Migrated from the NIOService of SCIFIO, which was previously adapted from the NIOByteBufferProvider class of Bio-Formats.
1 parent 596813c commit 28700f4

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.io.nio;
33+
34+
import java.io.IOException;
35+
import java.nio.ByteBuffer;
36+
import java.nio.channels.FileChannel;
37+
import java.nio.channels.FileChannel.MapMode;
38+
39+
import org.scijava.log.LogService;
40+
import org.scijava.plugin.Parameter;
41+
import org.scijava.plugin.Plugin;
42+
import org.scijava.service.AbstractService;
43+
import org.scijava.service.Service;
44+
45+
/**
46+
* Default service for working with the {@link java.nio} package, particularly
47+
* NIO {@link ByteBuffer} objects.
48+
*
49+
* @author Chris Allan
50+
* @author Curtis Rueden
51+
*/
52+
@Plugin(type = Service.class)
53+
public class DefaultNIOService extends AbstractService implements NIOService {
54+
55+
// -- Fields --
56+
57+
@Parameter
58+
private LogService log;
59+
60+
/** Whether or not we are to use memory mapped I/O. */
61+
private final boolean useMappedByteBuffer = Boolean.parseBoolean(System
62+
.getProperty("mappedBuffers"));
63+
64+
// -- NIOService API methods --
65+
66+
@Override
67+
public ByteBuffer allocate(final FileChannel channel, final MapMode mapMode,
68+
final long bufferStartPosition, final int newSize) throws IOException
69+
{
70+
log.debug("NIO: allocate: mapped=" + useMappedByteBuffer + ", start=" +
71+
bufferStartPosition + ", size=" + newSize);
72+
if (useMappedByteBuffer) {
73+
return allocateMappedByteBuffer(channel, mapMode, bufferStartPosition,
74+
newSize);
75+
}
76+
return allocateDirect(channel, bufferStartPosition, newSize);
77+
}
78+
79+
// -- Helper methods --
80+
81+
/**
82+
* Allocates memory and copies the desired file data into it.
83+
*
84+
* @param channel File channel to allocate or map byte buffers from.
85+
* @param bufferStartPosition The absolute position of the start of the
86+
* buffer.
87+
* @param newSize The buffer size.
88+
* @return A newly allocated NIO byte buffer.
89+
* @throws IOException If there is an issue aligning or allocating the buffer.
90+
*/
91+
private ByteBuffer allocateDirect(final FileChannel channel,
92+
final long bufferStartPosition, final int newSize) throws IOException
93+
{
94+
final ByteBuffer buffer = ByteBuffer.allocate(newSize);
95+
channel.read(buffer, bufferStartPosition);
96+
return buffer;
97+
}
98+
99+
/**
100+
* Memory maps the desired file data into memory.
101+
*
102+
* @param channel File channel to allocate or map byte buffers from.
103+
* @param mapMode The map mode. Required but only used if memory mapped I/O is
104+
* to occur.
105+
* @param bufferStartPosition The absolute position of the start of the
106+
* buffer.
107+
* @param newSize The buffer size.
108+
* @return A newly mapped NIO byte buffer.
109+
* @throws IOException If there is an issue mapping, aligning or allocating
110+
* the buffer.
111+
*/
112+
private ByteBuffer allocateMappedByteBuffer(final FileChannel channel,
113+
final MapMode mapMode, final long bufferStartPosition, final int newSize)
114+
throws IOException
115+
{
116+
return channel.map(mapMode, bufferStartPosition, newSize);
117+
}
118+
119+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.io.nio;
33+
34+
import java.io.IOException;
35+
import java.nio.ByteBuffer;
36+
import java.nio.channels.FileChannel;
37+
import java.nio.channels.FileChannel.MapMode;
38+
39+
import org.scijava.service.SciJavaService;
40+
41+
/**
42+
* Interface for services that work with the {@link java.nio} package,
43+
* particularly NIO {@link ByteBuffer} objects.
44+
*
45+
* @author Chris Allan
46+
* @author Curtis Rueden
47+
*/
48+
public interface NIOService extends SciJavaService {
49+
50+
/**
51+
* Allocates or maps the desired file data into memory.
52+
* <p>
53+
* This method provides a facade to byte buffer allocation that enables
54+
* <code>FileChannel.map()</code> usage on platforms where it's unlikely to
55+
* give us problems and heap allocation where it is.
56+
* </p>
57+
*
58+
* @param channel File channel to allocate or map byte buffers from.
59+
* @param mapMode The map mode. Required but only used if memory mapped I/O is
60+
* to occur.
61+
* @param bufferStartPosition The absolute position of the start of the
62+
* buffer.
63+
* @param newSize The buffer size.
64+
* @return A newly allocated or mapped NIO byte buffer.
65+
* @see "http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5092131"
66+
* @see "http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6417205"
67+
* @throws IOException If there is an issue mapping, aligning or allocating
68+
* the buffer.
69+
*/
70+
ByteBuffer allocate(FileChannel channel, MapMode mapMode,
71+
long bufferStartPosition, int newSize) throws IOException;
72+
73+
}

src/test/java/org/scijava/ContextCreationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public void testFull() {
9898
org.scijava.io.DefaultDataHandleService.class,
9999
org.scijava.io.DefaultIOService.class,
100100
org.scijava.io.DefaultRecentFileService.class,
101+
org.scijava.io.nio.DefaultNIOService.class,
101102
org.scijava.main.DefaultMainService.class,
102103
org.scijava.menu.DefaultMenuService.class,
103104
org.scijava.module.DefaultModuleService.class,

0 commit comments

Comments
 (0)