/** * Constructs a new {@link GroupedRandomAccessSource} based on the specified set of sources * @param sources the sources used to build this group */ public GroupedRandomAccessSource(IRandomAccessSource[] sources) throws java.io.IOException { this.sources = new SourceEntry[sources.length]; long totalSize = 0; for(int i = 0; i < sources.length; i++){ this.sources[i] = new SourceEntry(i, sources[i], totalSize); totalSize += sources[i].length(); } size = totalSize; currentSourceEntry = this.sources[sources.length-1]; sourceInUse(currentSourceEntry.source); }
/** * {@inheritDoc} */ public int get(long position, byte[] bytes, int off, int len) throws java.io.IOException { SourceEntry entry = getSourceEntryForOffset(position); if (entry == null) // we have run out of data to read from return -1; long offN = entry.offsetN(position); int remaining = len; while(remaining > 0){ if (entry == null) // we have run out of data to read from break; if (offN > entry.source.length()) break; int count = entry.source.get(offN, bytes, off, remaining); if (count == -1) break; off += count; position += count; remaining -= count; offN = 0; entry = getSourceEntryForOffset(position); } return remaining == len ? -1 : len - remaining; }
/** * {@inheritDoc} */ public int get(long position, byte[] bytes, int off, int len) throws java.io.IOException { SourceEntry entry = getSourceEntryForOffset(position); if (entry == null) // we have run out of data to read from return -1; long offN = entry.offsetN(position); int remaining = len; while(remaining > 0){ if (entry == null) // we have run out of data to read from break; if (offN > entry.source.length()) break; int count = entry.source.get(offN, bytes, off, remaining); if (count == -1) break; off += count; position += count; remaining -= count; offN = 0; entry = getSourceEntryForOffset(position); } return remaining == len ? -1 : len - remaining; }
/** * Constructs a new {@link GroupedRandomAccessSource} based on the specified set of sources * @param sources the sources used to build this group */ public GroupedRandomAccessSource(IRandomAccessSource[] sources) throws java.io.IOException { this.sources = new SourceEntry[sources.length]; long totalSize = 0; for(int i = 0; i < sources.length; i++){ this.sources[i] = new SourceEntry(i, sources[i], totalSize); totalSize += sources[i].length(); } size = totalSize; currentSourceEntry = this.sources[sources.length-1]; sourceInUse(currentSourceEntry.source); }
/** * {@inheritDoc} * The source that contains the byte at position is retrieved, the correct offset into that source computed, then the value * from that offset in the underlying source is returned. */ public int get(long position) throws java.io.IOException { SourceEntry entry = getSourceEntryForOffset(position); if (entry == null) // we have run out of data to read from return -1; return entry.source.get(entry.offsetN(position)); }
/** * {@inheritDoc} * The source that contains the byte at position is retrieved, the correct offset into that source computed, then the value * from that offset in the underlying source is returned. */ public int get(long position) throws java.io.IOException { SourceEntry entry = getSourceEntryForOffset(position); if (entry == null) // we have run out of data to read from return -1; return entry.source.get(entry.offsetN(position)); }