use of se.light.assembly64.d64.db.Disk in project assembly64fx by freabemania.
the class DiskImage method readImage.
/**
* Load image from disk
* @param filename file name of disk image
* @param expectedFileSize if uncompressed image is smaller, then throw CbmException. If larger, print warning message.
* @param type the type of image to load. Used for logging.
* @throws CbmException
*/
protected void readImage(String filename, int expectedFileSize, String type) throws CbmException {
feedbackMessage = new StringBuffer();
FileInputStream input;
int magic = 0;
feedbackMessage.append("Trying to load " + type + " image ").append(filename).append("\n");
this.cbmDisk = null;
try {
magic = getFileMagic(filename) & 0xffff;
} catch (Exception e) {
throw new CbmException("Failed to open header. " + e.getMessage());
}
if (magic == GZIPInputStream.GZIP_MAGIC) {
feedbackMessage.append("GZIP compressed file detected.\n");
readZippedFile(filename);
} else {
File file = new File(filename);
if (file.isFile() == false) {
throw new CbmException("File is not a regular file.");
} else if (file.length() <= 0) {
throw new CbmException("File is empty.");
} else if (file.length() > Integer.MAX_VALUE) {
throw new CbmException("File is too large.");
} else if (file.length() < expectedFileSize) {
throw new CbmException("File smaller than normal size. A " + type + " file should be " + expectedFileSize + " bytes.");
} else if (file.length() > expectedFileSize) {
feedbackMessage.append("Warning: File larger than normal size. A " + type + " file should be ").append(expectedFileSize).append(" bytes.\n");
}
try {
input = new FileInputStream(filename);
} catch (Exception e) {
throw new CbmException("Failed to open file. " + e.getMessage());
}
try {
this.cbmDisk = new byte[(int) file.length()];
for (int i = 0; i < cbmDisk.length; i++) {
cbmDisk[i] = 0;
}
input.read(cbmDisk);
} catch (Exception e) {
try {
input.close();
} catch (Exception e2) {
}
throw new CbmException("Failed to read file. " + e.getMessage());
}
try {
input.close();
} catch (Exception e) {
throw new CbmException("Failed to close file. " + e.getMessage());
}
}
feedbackMessage.append(type + " disk image was loaded.\n");
}
use of se.light.assembly64.d64.db.Disk in project assembly64fx by freabemania.
the class DiskImage method readPRG.
/**
* Reads a PRG file from hard disk<BR>
* globals written: feedbackMessage<BR>
* @param filename the filename
* @throws CbmException
*/
public byte[] readPRG(String filename) throws CbmException {
feedbackMessage.append("readPRG: Trying to load program ").append(filename).append("... \n");
File file = new File(filename);
if (file.length() > MAX_PRG) {
throw new CbmException(" File is too big for a PRG file (more than " + MAX_PRG + " bytes.");
}
if (!file.isFile()) {
throw new CbmException("File is not a regular file.");
}
byte[] saveData = new byte[(int) file.length()];
if (file.length() > 0) {
// Only attempt to read if file has contents. No need to read empty files.
FileInputStream input;
try {
input = new FileInputStream(filename);
} catch (Exception e) {
throw new CbmException("Failed to open file. " + e.getMessage());
}
try {
input.read(saveData);
} catch (Exception e) {
try {
input.close();
} catch (Exception e2) {
}
throw new CbmException("Could not read filedata.");
}
try {
input.close();
} catch (Exception e) {
throw new CbmException("Failed to close file. " + e.getMessage());
}
}
feedbackMessage.append("File is ").append(saveData.length).append(" bytes.");
return saveData;
}
use of se.light.assembly64.d64.db.Disk in project assembly64fx by freabemania.
the class DiskImage method getDisk.
/**
* Get <code>Disk</code> instance of current image. This is used when saving to database.
* @return Disk
*/
public Disk getDisk() {
Disk disk = new Disk();
disk.setLabel(getBam().getDiskName());
disk.setImageType(imageFormat);
disk.setErrors(errors);
disk.setWarnings(warnings);
for (int filenumber = 0; filenumber <= getFilenumberMax() - 1; filenumber++) {
boolean isLocked = getCbmFile(filenumber).isFileLocked();
boolean isClosed = getCbmFile(filenumber).isFileClosed();
DiskFile file = new DiskFile();
file.setName(getCbmFile(filenumber).getName());
file.setSize(getCbmFile(filenumber).getSizeInBlocks());
file.setFileType(getCbmFile(filenumber).getFileType());
file.setFileNum(filenumber);
file.setFlags((isLocked ? DiskFile.FLAG_LOCKED : 0) | (isClosed ? 0 : DiskFile.FLAG_NOT_CLOSED));
disk.getFileList().add(file);
}
return disk;
}
Aggregations