Debian Codegraph @

src/org/apache/commons/io/input/MemoryMappedFileInputStream.java

raw symbols SCIP
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.io.input;
18
19 import static org.apache.commons.io.IOUtils.EOF;
20
21 import java.io.BufferedInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.nio.ByteBuffer;
25 import java.nio.channels.FileChannel;
26 import java.nio.channels.FileChannel.MapMode;
27 import java.nio.file.Path;
28 import java.nio.file.StandardOpenOption;
29
30 import org.apache.commons.io.build.AbstractStreamBuilder;
31
32 /**
33 * An {@link InputStream} that utilizes memory mapped files to improve performance. A sliding window of the file is
34 * mapped to memory to avoid mapping the entire file to memory at one time. The size of the sliding buffer is
35 * configurable.
36 * <p>
37 * For most operating systems, mapping a file into memory is more expensive than reading or writing a few tens of
38 * kilobytes of data. From the standpoint of performance. it is generally only worth mapping relatively large files into
39 * memory.
40 * </p>
41 * <p>
42 * Note: Use of this class does not necessarily obviate the need to use a {@link BufferedInputStream}. Depending on the
43 * use case, the use of buffering may still further improve performance. For example:
44 * </p>
45 * <p>
46 * To build an instance, use {@link Builder}.
47 * </p>
48 * <pre>{@code
49 * BufferedInputStream s = new BufferedInputStream(new GzipInputStream(
50 * MemoryMappedFileInputStream.builder()
51 * .setPath(path)
52 * .setBufferSize(256 * 1024)
53 * .get()));}
54 * </pre>
55 * <p>
56 * should outperform:
57 * </p>
58 * <pre>
59 * new GzipInputStream(new MemoryMappedFileInputStream(path))
60 * </pre>
61 * <pre>{@code
62 * GzipInputStream s = new GzipInputStream(
63 * MemoryMappedFileInputStream.builder()
64 * .setPath(path)
65 * .setBufferSize(256 * 1024)
66 * .get());}
67 * </pre>
68 *
69 * @see Builder
70 * @since 2.12.0
71 */
72 public final class MemoryMappedFileInputStream extends AbstractInputStream {
73
74 // @formatter:off
75 /**
76 * Builds a new {@link MemoryMappedFileInputStream}.
77 *
78 * <p>
79 * For example:
80 * </p>
81 * <pre>{@code
82 * MemoryMappedFileInputStream s = MemoryMappedFileInputStream.builder()
83 * .setPath(path)
84 * .setBufferSize(256 * 1024)
85 * .get();}
86 * </pre>
87 *
88 * @see #get()
89 * @since 2.12.0
90 */
91 // @formatter:on
92 public static class Builder extends AbstractStreamBuilder<MemoryMappedFileInputStream, Builder> {
93
94 /**
95 * Constructs a new builder of {@link MemoryMappedFileInputStream}.
96 */
97 public Builder() {
98 setBufferSizeDefault(DEFAULT_BUFFER_SIZE);
99 setBufferSize(DEFAULT_BUFFER_SIZE);
100 }
101
102 /**
103 * Builds a new {@link MemoryMappedFileInputStream}.
104 * <p>
105 * You must set an aspect that supports {@link #getPath()}, otherwise, this method throws an exception.
106 * </p>
107 * <p>
108 * This builder uses the following aspects:
109 * </p>
110 * <ul>
111 * <li>{@link #getPath()} gets the target aspect.</li>
112 * <li>{@link #getBufferSize()}</li>
113 * </ul>
114 *
115 * @return a new instance.
116 * @throws IllegalStateException if the {@code origin} is {@code null}.
117 * @throws UnsupportedOperationException if the origin cannot be converted to a {@link Path}.
118 * @throws IOException if an I/O error occurs converting to an {@link Path} using {@link #getPath()}.
119 * @see #getPath()
120 * @see #getBufferSize()
121 * @see #getUnchecked()
122 */
123 @Override
124 public MemoryMappedFileInputStream get() throws IOException {
125 return new MemoryMappedFileInputStream(this);
126 }
127 }
128
129 /**
130 * Default size of the sliding memory mapped buffer. We use 256K, equal to 65536 pages (given a 4K page size).
131 * Increasing the value beyond the default size will generally not provide any increase in throughput.
132 */
133 private static final int DEFAULT_BUFFER_SIZE = 256 * 1024;
134
135 private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.wrap(new byte[0]).asReadOnlyBuffer();
136
137 /**
138 * Constructs a new {@link Builder}.
139 *
140 * @return a new {@link Builder}.
141 * @since 2.12.0
142 */
143 public static Builder builder() {
144 return new Builder();
145 }
146
147 private final int bufferSize;
148 private final FileChannel channel;
149 private ByteBuffer buffer = EMPTY_BUFFER;
150
151 /**
152 * The starting position (within the file) of the next sliding buffer.
153 */
154 private long nextBufferPosition;
155
156 /**
157 * Constructs a new instance.
158 *
159 * @param builder The builder.
160 * @throws IOException If an I/O error occurs.
161 */
162 private MemoryMappedFileInputStream(final Builder builder) throws IOException {
163 this.bufferSize = builder.getBufferSize();
164 this.channel = FileChannel.open(builder.getPath(), StandardOpenOption.READ);
165 }
166
167 @Override
168 public int available() throws IOException {
169 //return buffer != null ? buffer.remaining(): 0;
170 return buffer.remaining();
171 }
172
173 private void cleanBuffer() {
174 if (ByteBufferCleaner.isSupported() && buffer.isDirect()) {
175 ByteBufferCleaner.clean(buffer);
176 }
177 }
178
179 @Override
180 public void close() throws IOException {
181 if (!isClosed()) {
182 cleanBuffer();
183 buffer = EMPTY_BUFFER;
184 channel.close();
185 super.close();
186 }
187 }
188
189 int getBufferSize() {
190 return bufferSize;
191 }
192
193 private void nextBuffer() throws IOException {
194 final long remainingInFile = channel.size() - nextBufferPosition;
195 if (remainingInFile > 0) {
196 final long amountToMap = Math.min(remainingInFile, bufferSize);
197 cleanBuffer();
198 buffer = channel.map(MapMode.READ_ONLY, nextBufferPosition, amountToMap);
199 nextBufferPosition += amountToMap;
200 } else {
201 buffer = EMPTY_BUFFER;
202 }
203 }
204
205 @Override
206 public int read() throws IOException {
207 checkOpen();
208 if (!buffer.hasRemaining()) {
209 nextBuffer();
210 if (!buffer.hasRemaining()) {
211 return EOF;
212 }
213 }
214 return Short.toUnsignedInt(buffer.get());
215 }
216
217 @Override
218 public int read(final byte[] b, final int off, final int len) throws IOException {
219 checkOpen();
220 if (!buffer.hasRemaining()) {
221 nextBuffer();
222 if (!buffer.hasRemaining()) {
223 return EOF;
224 }
225 }
226 final int numBytes = Math.min(buffer.remaining(), len);
227 buffer.get(b, off, numBytes);
228 return numBytes;
229 }
230
231 @Override
232 public long skip(final long n) throws IOException {
233 checkOpen();
234 if (n <= 0) {
235 return 0;
236 }
237 if (n <= buffer.remaining()) {
238 buffer.position((int) (buffer.position() + n));
239 return n;
240 }
241 final long remainingInFile = channel.size() - nextBufferPosition;
242 final long skipped = buffer.remaining() + Math.min(remainingInFile, n - buffer.remaining());
243 nextBufferPosition += skipped - buffer.remaining();
244 nextBuffer();
245 return skipped;
246 }
247
248 }