validation of track/sector/block parameters

This commit is contained in:
ThomasFok 2023-01-24 19:51:57 +00:00
parent 8ca3e27fc4
commit 27496433eb
1 changed files with 40 additions and 7 deletions

View File

@ -20,6 +20,7 @@
package io.github.applecommander.acx.arggroup;
import com.webcodepro.applecommander.storage.Disk;
import com.webcodepro.applecommander.storage.physical.ImageOrder;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Option;
@ -69,10 +70,29 @@ public class CoordinateSelection {
public boolean isBootSector() {
return track == 0 && sector == 0;
}
public void validateTrackAndSector(Disk disk) throws IllegalArgumentException {
final int tracksPerDisk = disk.getImageOrder().getTracksPerDisk();
final int sectorsPerTrack = disk.getImageOrder().getSectorsPerTrack();
if (track < 0 || track >= tracksPerDisk) {
String errormsg = String.format("The track number(%d) is out of range(0-%d) on this image.", track, tracksPerDisk-1);
throw new IllegalArgumentException(errormsg);
}
if (sector < 0 || sector >= sectorsPerTrack) {
String errormsg = String.format("The sector number(%d) is out of range(0-%d) on this image.", sector, sectorsPerTrack-1);
throw new IllegalArgumentException(errormsg);
}
}
public byte[] read(Disk disk) {
validateTrackAndSector(disk);
return disk.readSector(track, sector);
}
public void write(Disk disk, byte[] data) {
validateTrackAndSector(disk);
disk.writeSector(track, sector, data);
}
}
@ -83,10 +103,23 @@ public class CoordinateSelection {
public boolean isBootBlock() {
return block == 0;
}
public void validateBlockNum(Disk disk) throws IllegalArgumentException {
final int blocksOnDevice = disk.getImageOrder().getBlocksOnDevice();
if (block < 0 || block >= blocksOnDevice) {
String errormsg = String.format("The block number(%d) is out of range(0-%d) on this image.", block, blocksOnDevice-1);
throw new IllegalArgumentException(errormsg);
}
}
public byte[] read(Disk disk) {
validateBlockNum(disk);
return disk.readBlock(block);
}
public void write(Disk disk, byte[] data) {
validateBlockNum(disk);
disk.writeBlock(block, data);
}
}