|
| 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 | +} |
0 commit comments