@Test(expectedExceptions = IllegalArgumentException.class) public void testReadUnavailablePiece() throws IOException { pieceStorage.readPiecePart(45, 0, pieceSize); }
@Override public byte[] readPiecePart(int pieceIndex, int offset, int length) throws IOException { checkPieceIndex(pieceIndex); try { readWriteLock.readLock().lock(); if (closedFully) throw new IOException("Storage is closed"); BitSet availablePieces = this.availablePieces; if (availablePieces != null && !availablePieces.get(pieceIndex)) { throw new IllegalArgumentException("trying reading part of not available piece"); } synchronized (openStorageLock) { openStorageIsNecessary(availablePieces == null); } ByteBuffer buffer = ByteBuffer.allocate(length); long pos = pieceIndex; pos = pos * pieceSize + offset; fileCollectionStorage.read(buffer, pos); return buffer.array(); } finally { readWriteLock.readLock().unlock(); } }
PieceStorageImpl implements PieceStorage { @Override public byte[] readPiecePart(int pieceIndex, int offset, int length) throws IOException { checkPieceIndex(pieceIndex); try { readWriteLock.readLock().lock(); if (closedFully) throw new IOException("Storage is closed"); BitSet availablePieces = this.availablePieces; if (availablePieces != null && !availablePieces.get(pieceIndex)) { throw new IllegalArgumentException("trying reading part of not available piece"); } synchronized (openStorageLock) { openStorageIsNecessary(availablePieces == null); } ByteBuffer buffer = ByteBuffer.allocate(length); long pos = pieceIndex; pos = pos * pieceSize + offset; fileCollectionStorage.read(buffer, pos); return buffer.array(); } finally { readWriteLock.readLock().unlock(); } } }
PieceStorageImpl implements PieceStorage { @Override public byte[] readPiecePart(int pieceIndex, int offset, int length) throws IOException { checkPieceIndex(pieceIndex); try { readWriteLock.readLock().lock(); if (closedFully) throw new IOException("Storage is closed"); BitSet availablePieces = this.availablePieces; if (availablePieces != null && !availablePieces.get(pieceIndex)) { throw new IllegalArgumentException("trying reading part of not available piece"); } synchronized (openStorageLock) { openStorageIsNecessary(availablePieces == null); } ByteBuffer buffer = ByteBuffer.allocate(length); long pos = pieceIndex; pos = pos * pieceSize + offset; fileCollectionStorage.read(buffer, pos); return buffer.array(); } finally { readWriteLock.readLock().unlock(); } } PieceStorageImpl(TorrentByteStorage fileCollectionStorage,
                          BitSet availablePieces,
                          int piecesCount,
                          int pieceSize); }
PieceStorageImpl implements PieceStorage { @Override public byte[] readPiecePart(int pieceIndex, int offset, int length) throws IOException { checkPieceIndex(pieceIndex); try { readWriteLock.readLock().lock(); if (closedFully) throw new IOException("Storage is closed"); BitSet availablePieces = this.availablePieces; if (availablePieces != null && !availablePieces.get(pieceIndex)) { throw new IllegalArgumentException("trying reading part of not available piece"); } synchronized (openStorageLock) { openStorageIsNecessary(availablePieces == null); } ByteBuffer buffer = ByteBuffer.allocate(length); long pos = pieceIndex; pos = pos * pieceSize + offset; fileCollectionStorage.read(buffer, pos); return buffer.array(); } finally { readWriteLock.readLock().unlock(); } } PieceStorageImpl(TorrentByteStorage fileCollectionStorage,
                          BitSet availablePieces,
                          int piecesCount,
                          int pieceSize); @Override void savePiece(int pieceIndex, byte[] pieceData); @Override byte[] readPiecePart(int pieceIndex, int offset, int length); @Override boolean isFinished(); @Override void closeFully(); @Override BitSet getAvailablePieces(); @Override void close(); }
PieceStorageImpl implements PieceStorage { @Override public byte[] readPiecePart(int pieceIndex, int offset, int length) throws IOException { checkPieceIndex(pieceIndex); try { readWriteLock.readLock().lock(); if (closedFully) throw new IOException("Storage is closed"); BitSet availablePieces = this.availablePieces; if (availablePieces != null && !availablePieces.get(pieceIndex)) { throw new IllegalArgumentException("trying reading part of not available piece"); } synchronized (openStorageLock) { openStorageIsNecessary(availablePieces == null); } ByteBuffer buffer = ByteBuffer.allocate(length); long pos = pieceIndex; pos = pos * pieceSize + offset; fileCollectionStorage.read(buffer, pos); return buffer.array(); } finally { readWriteLock.readLock().unlock(); } } PieceStorageImpl(TorrentByteStorage fileCollectionStorage,
                          BitSet availablePieces,
                          int piecesCount,
                          int pieceSize); @Override void savePiece(int pieceIndex, byte[] pieceData); @Override byte[] readPiecePart(int pieceIndex, int offset, int length); @Override boolean isFinished(); @Override void closeFully(); @Override BitSet getAvailablePieces(); @Override void close();  }