/** * {@inheritDoc} */ public int get(long position) throws java.io.IOException { return source.get(position); }
/** * {@inheritDoc} */ public long length() { return source.length(); } }
/** * {@inheritDoc} * Ensure that the source is mapped. In the future, we may implement an MRU that allows multiple pages to be opened at a time */ @Override protected void sourceInUse(IRandomAccessSource source) throws java.io.IOException { ((MappedChannelRandomAccessSource)source).open(); }
/** * 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(); }
/** * {@inheritDoc} * Cleans the mapped byte buffers and closes the channel */ public void close() throws java.io.IOException { try { source.close(); } finally { try { channel.close(); } catch (Exception ex) { Logger logger = LoggerFactory.getLogger(FileChannelRandomAccessSource.class); logger.error(LogMessageConstant.FILE_CHANNEL_CLOSING_FAILED, ex); } } }
/** * 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(); }
/** * {@inheritDoc} * Cleans the mapped byte buffers and closes the channel */ public void close() throws java.io.IOException { try { source.close(); } finally { try { channel.close(); } catch (Exception ex) { Logger logger = LoggerFactory.getLogger(FileChannelRandomAccessSource.class); logger.error(LogMessageConstant.FILE_CHANNEL_CLOSING_FAILED, ex); } } }
/** * 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; }
/** * {@inheritDoc} */ public int get(long position, byte[] bytes, int off, int len) throws java.io.IOException { return source.get(position, bytes, off, len); }
/** * {@inheritDoc} */ public long length() { return source.length(); } }
/** * {@inheritDoc} * Ensure that the source is mapped. In the future, we may implement an MRU that allows multiple pages to be opened at a time */ @Override protected void sourceInUse(IRandomAccessSource source) throws java.io.IOException { ((MappedChannelRandomAccessSource)source).open(); }
/** * {@inheritDoc} */ public int get(long position, byte[] bytes, int off, int len) throws java.io.IOException { return source.get(position, bytes, off, len); }
/** * {@inheritDoc} */ public int get(long position) throws java.io.IOException { return source.get(position); }