Raid0Device implements Device { public void write(final long address, final byte[] data) throws Exception { if (blockCount == -1) { throw new IllegalStateException("You first have to open the Device!"); } int fragments = data.length / extendSize; int blocks = data.length / blockSize; int blockFactor = extendSize / blockSize; if (address < 0 || address + blocks > blockCount) { long adr = address < 0 ? address : address + blocks - 1; throw new IllegalArgumentException("Address " + adr + " out of range."); } if (data.length % extendSize != 0) { throw new IllegalArgumentException("Number of bytes is not a multiple of the extend size (" + extendSize + ")!"); } int parts = (fragments >= devices.length) ? devices.length : fragments; List<byte[]> deviceData = new Vector<byte[]>(); long actualAddress = ((address / blockFactor) / devices.length) * blockFactor; int actualDevice = (int)((address / blockFactor) % devices.length); int deviceBlockCount; barrier = new CyclicBarrier(parts + 1); for (int i = 0; i < parts; i++) { deviceBlockCount = fragments / devices.length * blockFactor; if (i < (fragments % devices.length)) { deviceBlockCount += blockFactor; } deviceData.add(new byte[deviceBlockCount * blockSize]); } for (int i = 0; i < fragments; i++) { System.arraycopy(data, i * extendSize, deviceData.get(i % devices.length), i / devices.length * extendSize, extendSize); } for (int i = 0; i < parts; i++) { executor.execute(new WriteThread(devices[actualDevice], actualAddress, deviceData.get(i))); if (actualDevice == devices.length - 1) { actualDevice = 0; actualAddress += blockFactor; } else { actualDevice++; } } barrier.await(); } Raid0Device(final Device[] initDevices);  Raid0Device(final Device[] initDevices, final int initExtendSize); void close(); int getBlockSize(); String getName(); long getBlockCount(); void open(); void read(final long address, final byte[] data); void write(final long address, final byte[] data);  }
@Test(expected = IllegalArgumentException.class) public void testWriteAddressToLow() throws Exception { address = -1; device.write(address, testData); }
@Test(expected = IllegalArgumentException.class) public void testWriteIllegalSize() throws Exception { address = 0; byte[] data = new byte[1]; randomGenerator.nextBytes(data); device.write(address, data); }
@Test(expected = IllegalStateException.class) public void testWriteNotOpened() throws Exception { Device newDevice = new Raid0Device(jSCSIDevices); address = 0; newDevice.write(address, testData); }