garydgregory commented on a change in pull request #215: URL: https://github.com/apache/commons-io/pull/215#discussion_r708257169
########## File path: src/main/java/org/apache/commons/io/input/ByteBufferCleaner.java ########## @@ -0,0 +1,144 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.input; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.nio.ByteBuffer; + +/** + * Utility to manually clean a direct {@link ByteBuffer}. Without manual + * intervention, direct ByteBuffers will be cleaned eventually upon garbage + * collection. However, this should be be relied upon since it may not occur in + * a timely fashion - especially since off heap ByeBuffers don't put pressure on + * the garbage collector. + * + * <p> + * <b>Warning:</b> Do not attempt to use a direct {@link ByteBuffer} that has + * been cleaned or bad things will happen. Don't use this class unless you can + * ensure that the cleaned buffer will not be accessed anymore. + * </p> + * <p> + * See <a href=https://bugs.openjdk.java.net/browse/JDK-4724038>JDK-4724038</a> + * </p> + * + * @since 2.10.0 Review comment: You do need a since tag since this is package private. Also new public and protected elements should be since 2.12.0 ;-) ########## File path: src/main/java/org/apache/commons/io/input/ByteBufferCleaner.java ########## @@ -0,0 +1,144 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.input; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.nio.ByteBuffer; + +/** + * Utility to manually clean a direct {@link ByteBuffer}. Without manual + * intervention, direct ByteBuffers will be cleaned eventually upon garbage + * collection. However, this should be be relied upon since it may not occur in Review comment: "this should be be relied" -> "this should NOT be relied" ########## File path: src/main/java/org/apache/commons/io/input/MemoryMappedFileInputStream.java ########## @@ -0,0 +1,169 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.input; + +import static org.apache.commons.io.IOUtils.EOF; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.channels.FileChannel.MapMode; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; + +/** + * <p> + * An {@link InputStream} that utilizes memory mapped files to improve + * performance. A sliding window of the file is mapped to memory to avoid + * mapping the entire file to memory at one time. The size of the sliding buffer + * is user configurable. + * </p> + * <p> + * For most operating systems, mapping a file into memory is more expensive than + * reading or writing a few tens of kilobytes of data. From the standpoint of + * performance it is generally only worth mapping relatively large files into + * memory. + * </p> + * Note: Use of this class does not necessarily obviate the need to use a + * {@link BufferedInputStream}. Depending on the use case, the use of buffering + * may still further improve performance. For example: + * + * <pre> + * new BufferedInputStream(new GzipInputStream(new MemoryMappedFileInputStream(path)))) + * </pre> + * + * will greatly outperform: + * + * <pre> + * new GzipInputStream(new MemoryMappedFileInputStream(path)) + * </pre> + * + * @since 2.10.0 Review comment: "2.10.0" -> "2.12.0" ########## File path: src/main/java/org/apache/commons/io/input/MemoryMappedFileInputStream.java ########## @@ -0,0 +1,169 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.input; + +import static org.apache.commons.io.IOUtils.EOF; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.channels.FileChannel.MapMode; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; + +/** + * <p> + * An {@link InputStream} that utilizes memory mapped files to improve + * performance. A sliding window of the file is mapped to memory to avoid + * mapping the entire file to memory at one time. The size of the sliding buffer + * is user configurable. + * </p> + * <p> + * For most operating systems, mapping a file into memory is more expensive than + * reading or writing a few tens of kilobytes of data. From the standpoint of + * performance it is generally only worth mapping relatively large files into + * memory. + * </p> + * Note: Use of this class does not necessarily obviate the need to use a + * {@link BufferedInputStream}. Depending on the use case, the use of buffering + * may still further improve performance. For example: + * + * <pre> + * new BufferedInputStream(new GzipInputStream(new MemoryMappedFileInputStream(path)))) + * </pre> + * + * will greatly outperform: + * + * <pre> + * new GzipInputStream(new MemoryMappedFileInputStream(path)) + * </pre> + * + * @since 2.10.0 + */ +public class MemoryMappedFileInputStream extends InputStream { + /** + * Default size of the sliding memory mapped buffer. We use 256K, equal to 65536 + * pages (given a 4K page size). Increasing the value beyond the default size + * will generally not provide any increase in throughput. + */ + private static final int DEFAULT_BUFFER_SIZE = 256 * 1024; + private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.wrap(new byte[0]).asReadOnlyBuffer(); + private static final boolean IS_CLEANING_SUPPORTED = ByteBufferCleaner.isSupported(); + private final int bufferSize; + private final FileChannel channel; + private ByteBuffer buffer = EMPTY_BUFFER; + private boolean closed = false; + /** + * The starting position (within the file) of the next sliding buffer. + */ + private long nextBufferPosition = 0; + + public MemoryMappedFileInputStream(final Path file) throws IOException { Review comment: Please add a Javadoc comment: "Constructs..." ########## File path: src/main/java/org/apache/commons/io/input/MemoryMappedFileInputStream.java ########## @@ -0,0 +1,169 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.io.input; + +import static org.apache.commons.io.IOUtils.EOF; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.channels.FileChannel.MapMode; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; + +/** + * <p> + * An {@link InputStream} that utilizes memory mapped files to improve + * performance. A sliding window of the file is mapped to memory to avoid + * mapping the entire file to memory at one time. The size of the sliding buffer + * is user configurable. + * </p> + * <p> + * For most operating systems, mapping a file into memory is more expensive than + * reading or writing a few tens of kilobytes of data. From the standpoint of + * performance it is generally only worth mapping relatively large files into + * memory. + * </p> + * Note: Use of this class does not necessarily obviate the need to use a + * {@link BufferedInputStream}. Depending on the use case, the use of buffering + * may still further improve performance. For example: + * + * <pre> + * new BufferedInputStream(new GzipInputStream(new MemoryMappedFileInputStream(path)))) + * </pre> + * + * will greatly outperform: + * + * <pre> + * new GzipInputStream(new MemoryMappedFileInputStream(path)) + * </pre> + * + * @since 2.10.0 + */ +public class MemoryMappedFileInputStream extends InputStream { + /** + * Default size of the sliding memory mapped buffer. We use 256K, equal to 65536 + * pages (given a 4K page size). Increasing the value beyond the default size + * will generally not provide any increase in throughput. + */ + private static final int DEFAULT_BUFFER_SIZE = 256 * 1024; + private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.wrap(new byte[0]).asReadOnlyBuffer(); + private static final boolean IS_CLEANING_SUPPORTED = ByteBufferCleaner.isSupported(); + private final int bufferSize; + private final FileChannel channel; + private ByteBuffer buffer = EMPTY_BUFFER; + private boolean closed = false; + /** + * The starting position (within the file) of the next sliding buffer. + */ + private long nextBufferPosition = 0; + + public MemoryMappedFileInputStream(final Path file) throws IOException { + this(file, DEFAULT_BUFFER_SIZE); + } + + public MemoryMappedFileInputStream(final Path file, final int bufferSize) throws IOException { Review comment: Please add a Javadoc comment: "Constructs..." -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
