| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
package org.apache.commons.io.input; |
| 15 |
|
| 16 |
import static org.apache.commons.io.IOUtils.EOF; |
| 17 |
|
| 18 |
import java.io.BufferedInputStream; |
| 19 |
import java.io.File; |
| 20 |
import java.io.IOException; |
| 21 |
import java.io.InputStream; |
| 22 |
import java.nio.ByteBuffer; |
| 23 |
import java.nio.channels.FileChannel; |
| 24 |
import java.nio.file.Path; |
| 25 |
import java.nio.file.StandardOpenOption; |
| 26 |
|
| 27 |
import org.apache.commons.io.IOUtils; |
| 28 |
import org.apache.commons.io.build.AbstractStreamBuilder; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public final class BufferedFileChannelInputStream extends InputStream { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static class Builder extends AbstractStreamBuilder<BufferedFileChannelInputStream, Builder> { |
| 74 |
|
| 75 |
private FileChannel fileChannel; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public Builder() { |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
@Override |
| 107 |
public BufferedFileChannelInputStream get() throws IOException { |
| 108 |
return new BufferedFileChannelInputStream(this); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public Builder setFileChannel(final FileChannel fileChannel) { |
| 122 |
this.fileChannel = fileChannel; |
| 123 |
return this; |
| 124 |
} |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public static Builder builder() { |
| 135 |
return new Builder(); |
| 136 |
} |
| 137 |
|
| 138 |
private final ByteBuffer byteBuffer; |
| 139 |
|
| 140 |
private final FileChannel fileChannel; |
| 141 |
|
| 142 |
@SuppressWarnings("resource") |
| 143 |
private BufferedFileChannelInputStream(final Builder builder) throws IOException { |
| 144 |
this.fileChannel = builder.fileChannel != null ? builder.fileChannel : FileChannel.open(builder.getPath(), StandardOpenOption.READ); |
| 145 |
this.byteBuffer = ByteBuffer.allocateDirect(builder.getBufferSize()); |
| 146 |
this.byteBuffer.flip(); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
@Deprecated |
| 157 |
public BufferedFileChannelInputStream(final File file) throws IOException { |
| 158 |
this(file, IOUtils.DEFAULT_BUFFER_SIZE); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
@Deprecated |
| 170 |
public BufferedFileChannelInputStream(final File file, final int bufferSize) throws IOException { |
| 171 |
this(file.toPath(), bufferSize); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
@Deprecated |
| 182 |
public BufferedFileChannelInputStream(final Path path) throws IOException { |
| 183 |
this(path, IOUtils.DEFAULT_BUFFER_SIZE); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
@Deprecated |
| 195 |
public BufferedFileChannelInputStream(final Path path, final int bufferSize) throws IOException { |
| 196 |
this(builder().setPath(path).setBufferSize(bufferSize)); |
| 197 |
} |
| 198 |
|
| 199 |
@Override |
| 200 |
public synchronized int available() throws IOException { |
| 201 |
if (!fileChannel.isOpen()) { |
| 202 |
return 0; |
| 203 |
} |
| 204 |
if (!refill()) { |
| 205 |
return 0; |
| 206 |
} |
| 207 |
return byteBuffer.remaining(); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
private void clean(final ByteBuffer buffer) { |
| 219 |
if (buffer.isDirect()) { |
| 220 |
cleanDirectBuffer(buffer); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
private void cleanDirectBuffer(final ByteBuffer buffer) { |
| 233 |
if (ByteBufferCleaner.isSupported()) { |
| 234 |
ByteBufferCleaner.clean(buffer); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
@Override |
| 239 |
public synchronized void close() throws IOException { |
| 240 |
try { |
| 241 |
fileChannel.close(); |
| 242 |
} finally { |
| 243 |
clean(byteBuffer); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
@Override |
| 248 |
public synchronized int read() throws IOException { |
| 249 |
if (!refill()) { |
| 250 |
return EOF; |
| 251 |
} |
| 252 |
return byteBuffer.get() & 0xFF; |
| 253 |
} |
| 254 |
|
| 255 |
@Override |
| 256 |
public synchronized int read(final byte[] b, final int offset, int len) throws IOException { |
| 257 |
if (offset < 0 || len < 0 || offset + len < 0 || offset + len > b.length) { |
| 258 |
throw new IndexOutOfBoundsException(); |
| 259 |
} |
| 260 |
if (!refill()) { |
| 261 |
return EOF; |
| 262 |
} |
| 263 |
len = Math.min(len, byteBuffer.remaining()); |
| 264 |
byteBuffer.get(b, offset, len); |
| 265 |
return len; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
private boolean refill() throws IOException { |
| 275 |
Input.checkOpen(fileChannel.isOpen()); |
| 276 |
if (!byteBuffer.hasRemaining()) { |
| 277 |
byteBuffer.clear(); |
| 278 |
int nRead = 0; |
| 279 |
while (nRead == 0) { |
| 280 |
nRead = fileChannel.read(byteBuffer); |
| 281 |
} |
| 282 |
byteBuffer.flip(); |
| 283 |
return nRead >= 0; |
| 284 |
} |
| 285 |
return true; |
| 286 |
} |
| 287 |
|
| 288 |
@Override |
| 289 |
public synchronized long skip(final long n) throws IOException { |
| 290 |
if (n <= 0L) { |
| 291 |
return 0L; |
| 292 |
} |
| 293 |
if (byteBuffer.remaining() >= n) { |
| 294 |
|
| 295 |
byteBuffer.position(byteBuffer.position() + (int) n); |
| 296 |
return n; |
| 297 |
} |
| 298 |
final long skippedFromBuffer = byteBuffer.remaining(); |
| 299 |
final long toSkipFromFileChannel = n - skippedFromBuffer; |
| 300 |
|
| 301 |
byteBuffer.position(0); |
| 302 |
byteBuffer.flip(); |
| 303 |
return skippedFromBuffer + skipFromFileChannel(toSkipFromFileChannel); |
| 304 |
} |
| 305 |
|
| 306 |
private long skipFromFileChannel(final long n) throws IOException { |
| 307 |
final long currentFilePosition = fileChannel.position(); |
| 308 |
final long size = fileChannel.size(); |
| 309 |
if (n > size - currentFilePosition) { |
| 310 |
fileChannel.position(size); |
| 311 |
return size - currentFilePosition; |
| 312 |
} |
| 313 |
fileChannel.position(currentFilePosition + n); |
| 314 |
return n; |
| 315 |
} |
| 316 |
|
| 317 |
} |