@Test public void testSelect() throws Exception { final File file1 = tempFiles.createTempFile(); final File file2 = tempFiles.createTempFile(); final List<FileStorage> files = new ArrayList<FileStorage>(); files.add(new FileStorage(file1, 0, 2)); files.add(new FileStorage(file2, 2, 2)); final FileCollectionStorage storage = new FileCollectionStorage(files, 4); storage.open(false); try { assertTrue(storage.isFinished()); write(new byte[]{1, 2}, 0, storage); check(new byte[]{1, 2}, file1); write(new byte[]{5, 6}, 2, storage); check(new byte[]{5, 6}, file2); write(new byte[]{8, 9, 10, 11}, 0, storage); check(new byte[]{8, 9}, file1); check(new byte[]{10, 11}, file2); write(new byte[]{100, 101, 102}, 0, storage); check(new byte[]{102, 11}, file2); } finally { storage.close(); } }
private List<FileOffset> select(long offset, long length) { if (offset + length > this.size) { throw new IllegalArgumentException("Buffer overrun (" + offset + " + " + length + " > " + this.size + ") !"); } List<FileOffset> selected = new LinkedList<FileOffset>(); long bytes = 0; for (FileStorage file : this.files) { if (file.offset() >= offset + length) { break; } if (file.offset() + file.size() < offset) { continue; } long position = offset - file.offset(); position = position > 0 ? position : 0; long size = Math.min( file.size() - position, length - bytes); selected.add(new FileOffset(file, position, size)); bytes += size; } if (selected.size() == 0 || bytes < length) { throw new IllegalStateException("Buffer underrun (only got " + bytes + " out of " + length + " byte(s) requested)!"); } return selected; }
FileCollectionStorage implements TorrentByteStorage { private List<FileOffset> select(long offset, long length) { if (offset + length > this.size) { throw new IllegalArgumentException("Buffer overrun (" + offset + " + " + length + " > " + this.size + ") !"); } List<FileOffset> selected = new LinkedList<FileOffset>(); long bytes = 0; for (FileStorage file : this.files) { if (file.offset() >= offset + length) { break; } if (file.offset() + file.size() < offset) { continue; } long position = offset - file.offset(); position = position > 0 ? position : 0; long size = Math.min( file.size() - position, length - bytes); selected.add(new FileOffset(file, position, size)); bytes += size; } if (selected.size() == 0 || bytes < length) { throw new IllegalStateException("Buffer underrun (only got " + bytes + " out of " + length + " byte(s) requested)!"); } return selected; } }
FileCollectionStorage implements TorrentByteStorage { private List<FileOffset> select(long offset, long length) { if (offset + length > this.size) { throw new IllegalArgumentException("Buffer overrun (" + offset + " + " + length + " > " + this.size + ") !"); } List<FileOffset> selected = new LinkedList<FileOffset>(); long bytes = 0; for (FileStorage file : this.files) { if (file.offset() >= offset + length) { break; } if (file.offset() + file.size() < offset) { continue; } long position = offset - file.offset(); position = position > 0 ? position : 0; long size = Math.min( file.size() - position, length - bytes); selected.add(new FileOffset(file, position, size)); bytes += size; } if (selected.size() == 0 || bytes < length) { throw new IllegalStateException("Buffer underrun (only got " + bytes + " out of " + length + " byte(s) requested)!"); } return selected; } FileCollectionStorage(List<FileStorage> files,
                               long size); }
FileCollectionStorage implements TorrentByteStorage { private List<FileOffset> select(long offset, long length) { if (offset + length > this.size) { throw new IllegalArgumentException("Buffer overrun (" + offset + " + " + length + " > " + this.size + ") !"); } List<FileOffset> selected = new LinkedList<FileOffset>(); long bytes = 0; for (FileStorage file : this.files) { if (file.offset() >= offset + length) { break; } if (file.offset() + file.size() < offset) { continue; } long position = offset - file.offset(); position = position > 0 ? position : 0; long size = Math.min( file.size() - position, length - bytes); selected.add(new FileOffset(file, position, size)); bytes += size; } if (selected.size() == 0 || bytes < length) { throw new IllegalStateException("Buffer underrun (only got " + bytes + " out of " + length + " byte(s) requested)!"); } return selected; } FileCollectionStorage(List<FileStorage> files,
                               long size); static FileCollectionStorage create(TorrentMetadata metadata, File parent); synchronized void open(final boolean seeder); @Override int read(ByteBuffer buffer, long position); @Override int write(ByteBuffer buffer, long position); @Override synchronized void close(); @Override synchronized void finish(); @Override boolean isFinished(); @Override void delete(); }
FileCollectionStorage implements TorrentByteStorage { private List<FileOffset> select(long offset, long length) { if (offset + length > this.size) { throw new IllegalArgumentException("Buffer overrun (" + offset + " + " + length + " > " + this.size + ") !"); } List<FileOffset> selected = new LinkedList<FileOffset>(); long bytes = 0; for (FileStorage file : this.files) { if (file.offset() >= offset + length) { break; } if (file.offset() + file.size() < offset) { continue; } long position = offset - file.offset(); position = position > 0 ? position : 0; long size = Math.min( file.size() - position, length - bytes); selected.add(new FileOffset(file, position, size)); bytes += size; } if (selected.size() == 0 || bytes < length) { throw new IllegalStateException("Buffer underrun (only got " + bytes + " out of " + length + " byte(s) requested)!"); } return selected; } FileCollectionStorage(List<FileStorage> files,
                               long size); static FileCollectionStorage create(TorrentMetadata metadata, File parent); synchronized void open(final boolean seeder); @Override int read(ByteBuffer buffer, long position); @Override int write(ByteBuffer buffer, long position); @Override synchronized void close(); @Override synchronized void finish(); @Override boolean isFinished(); @Override void delete();  }