/** * Constructs a new {@link FileChannelRandomAccessSource} based on the specified FileChannel. The entire source channel will be mapped into memory. * @param channel the channel to use as the backing store * @throws java.io.IOException if the channel cannot be opened or mapped */ public FileChannelRandomAccessSource(FileChannel channel) throws java.io.IOException { this.channel = channel; if(channel.size() == 0) throw new java.io.IOException("File size is 0 bytes"); source = new MappedChannelRandomAccessSource(channel, 0, channel.size()); source.open(); }
/** * Constructs a set of {@link MappedChannelRandomAccessSource}s for each page (of size bufferSize) of the underlying channel * @param channel the underlying channel * @param bufferSize the size of each page (the last page may be shorter) * @return a list of sources that represent the pages of the channel * @throws java.io.IOException if IO fails for any reason */ private static IRandomAccessSource[] buildSources(FileChannel channel, int bufferSize) throws java.io.IOException{ long size = channel.size(); if (size <= 0) throw new java.io.IOException("File size must be greater than zero"); int bufferCount = (int)(size/bufferSize) + (size % bufferSize == 0 ? 0 : 1); MappedChannelRandomAccessSource[] sources = new MappedChannelRandomAccessSource[bufferCount]; for (int i = 0; i < bufferCount; i++){ long pageOffset = (long)i*bufferSize; long pageLength = Math.min(size - pageOffset, bufferSize); sources[i] = new MappedChannelRandomAccessSource(channel, pageOffset, pageLength); } return sources; }
/** * Constructs a set of {@link MappedChannelRandomAccessSource}s for each page (of size bufferSize) of the underlying channel * @param channel the underlying channel * @param bufferSize the size of each page (the last page may be shorter) * @return a list of sources that represent the pages of the channel * @throws java.io.IOException if IO fails for any reason */ private static IRandomAccessSource[] buildSources(FileChannel channel, int bufferSize) throws java.io.IOException{ long size = channel.size(); if (size <= 0) throw new java.io.IOException("File size must be greater than zero"); int bufferCount = (int)(size/bufferSize) + (size % bufferSize == 0 ? 0 : 1); MappedChannelRandomAccessSource[] sources = new MappedChannelRandomAccessSource[bufferCount]; for (int i = 0; i < bufferCount; i++){ long pageOffset = (long)i*bufferSize; long pageLength = Math.min(size - pageOffset, bufferSize); sources[i] = new MappedChannelRandomAccessSource(channel, pageOffset, pageLength); } return sources; }
/** * Constructs a new {@link FileChannelRandomAccessSource} based on the specified FileChannel. The entire source channel will be mapped into memory. * @param channel the channel to use as the backing store * @throws java.io.IOException if the channel cannot be opened or mapped */ public FileChannelRandomAccessSource(FileChannel channel) throws java.io.IOException { this.channel = channel; if(channel.size() == 0) throw new java.io.IOException("File size is 0 bytes"); source = new MappedChannelRandomAccessSource(channel, 0, channel.size()); source.open(); }