From e3ac682fc0c278907c08c0e8a4ed271ecd2c78e9 Mon Sep 17 00:00:00 2001 From: Denis Molony Date: Sun, 18 Apr 2021 07:32:03 +1000 Subject: [PATCH] tidying --- .../diskbrowser/disk/DiskFactory.java | 46 ++------- .../diskbrowser/prodos/write/ProdosDisk.java | 4 +- .../bytezone/diskbrowser/utilities/NuFX.java | 96 +++++++++++++------ .../diskbrowser/utilities/Record.java | 7 ++ .../diskbrowser/utilities/Utility.java | 17 +--- 5 files changed, 86 insertions(+), 84 deletions(-) diff --git a/src/com/bytezone/diskbrowser/disk/DiskFactory.java b/src/com/bytezone/diskbrowser/disk/DiskFactory.java index f045057..618fc89 100755 --- a/src/com/bytezone/diskbrowser/disk/DiskFactory.java +++ b/src/com/bytezone/diskbrowser/disk/DiskFactory.java @@ -146,19 +146,23 @@ public class DiskFactory } } - if ("sdk".equals (suffix)) // shrinkit disk archive + if ("sdk".equals (suffix) || "shk".equals (suffix)) // shrinkit disk/file archive { if (debug) - System.out.println (" ** sdk **"); + System.out.println (" ** sdk/shk **"); try { NuFX nuFX = new NuFX (file.toPath ()); - int totalDisks = nuFX.getTotalDisks (); - if (totalDisks == 0) + if (nuFX.getTotalDisks () == 0 && nuFX.getTotalFiles () == 0) return null; + + byte[] diskBuffer = nuFX.getDiskBuffer (); + if (diskBuffer == null) + return null; + File tmp = File.createTempFile (suffix, null); FileOutputStream fos = new FileOutputStream (tmp); - fos.write (nuFX.getDiskBuffer ()); + fos.write (diskBuffer); fos.close (); tmp.deleteOnExit (); file = tmp; @@ -175,38 +179,6 @@ public class DiskFactory return null; } } - else if ("shk".equals (suffix)) // shrinkit file archive - { - if (debug) - System.out.println (" ** shk **"); - - try - { - NuFX nuFX = new NuFX (file.toPath ()); - int totalFiles = nuFX.getTotalFiles (); - // System.out.printf ("Total files: %d%n", totalFiles); - if (totalFiles == 0) - return null; - - File tmp = File.createTempFile (suffix, null); - FileOutputStream fos = new FileOutputStream (tmp); - fos.write (nuFX.getDiskBuffer ()); - fos.close (); - tmp.deleteOnExit (); - file = tmp; - suffix = "po"; - compressed = true; - } - catch (IOException e) - { - e.printStackTrace (); - return null; - } - catch (FileFormatException e) - { - return null; - } - } FormattedDisk disk = null; FormattedDisk disk2 = null; diff --git a/src/com/bytezone/diskbrowser/prodos/write/ProdosDisk.java b/src/com/bytezone/diskbrowser/prodos/write/ProdosDisk.java index 7f9ca5e..1680e0d 100644 --- a/src/com/bytezone/diskbrowser/prodos/write/ProdosDisk.java +++ b/src/com/bytezone/diskbrowser/prodos/write/ProdosDisk.java @@ -290,9 +290,7 @@ public class ProdosDisk { int nextBlock = getFreeBlock (); if (nextBlock < 0) - { - throw new DiskFullException ("Full"); - } + throw new DiskFullException ("Disk Full"); volumeBitMap.set (nextBlock, false); // mark as unavailable diff --git a/src/com/bytezone/diskbrowser/utilities/NuFX.java b/src/com/bytezone/diskbrowser/utilities/NuFX.java index b7d7c67..542be1e 100644 --- a/src/com/bytezone/diskbrowser/utilities/NuFX.java +++ b/src/com/bytezone/diskbrowser/utilities/NuFX.java @@ -22,6 +22,7 @@ public class NuFX private final List records = new ArrayList<> (); private int totalFiles; private int totalDisks; + private int totalBlocks; // ---------------------------------------------------------------------------------// public NuFX (Path path) throws FileFormatException, IOException @@ -41,6 +42,11 @@ public class NuFX for (int rec = 0; rec < masterHeader.getTotalRecords (); rec++) { Record record = new Record (buffer, dataPtr); + if (record.getFileSystemID () != 1) + { + System.out.println ("Not Prodos"); + break; + } records.add (record); if (debug) @@ -61,7 +67,17 @@ public class NuFX } if (record.hasFile ()) + { ++totalFiles; + int blocks = (record.getFileSize () - 1) / 512 + 1; + if (blocks == 1) // seedling + totalBlocks += blocks; + else if (blocks <= 256) // sapling + totalBlocks += blocks + 1; + else // tree + totalBlocks += blocks + (blocks / 256) + 1; + } + if (record.hasDisk ()) ++totalDisks; } @@ -80,41 +96,53 @@ public class NuFX } else if (totalFiles > 0) { - try + int[] diskSizes = { 280, 800, 1600, 3200, 6400, 65536 }; + // System.out.printf ("Files require: %d blocks%n", totalBlocks); + + for (int diskSize : diskSizes) { - ProdosDisk disk = new ProdosDisk (3200, "DISKBROWSER"); - int count = 0; - for (Record record : records) + // System.out.printf ("Checking %d %d%n", diskSize, totalBlocks); + if (diskSize < (totalBlocks + 10)) + continue; + + try { - if (record.hasFile ()) + ProdosDisk disk = new ProdosDisk (diskSize, "DISK.BROWSER"); + int count = 0; + for (Record record : records) { - String fileName = record.getFileName (); - int fileSize = record.getFileSize (); - byte fileType = (byte) record.getFileType (); - int eof = record.getUncompressedSize (); - int auxType = record.getAuxType (); - LocalDateTime created = record.getCreated (); - LocalDateTime modified = record.getModified (); - byte[] buffer = record.getData (); + if (record.hasFile ()) + { + String fileName = record.getFileName (); + // int fileSize = record.getFileSize (); + byte fileType = (byte) record.getFileType (); + int eof = record.getUncompressedSize (); + int auxType = record.getAuxType (); + LocalDateTime created = record.getCreated (); + LocalDateTime modified = record.getModified (); + byte[] buffer = record.getData (); - System.out.printf ("%3d %-35s %,7d %02X %,7d %,7d %,7d %s %s%n", - ++count, fileName, fileSize, fileType, eof, auxType, buffer.length, - created, modified); - disk.addFile (fileName, fileType, auxType, created, modified, buffer); + if (false) + System.out.printf ("%3d %-35s %02X %,7d %,7d %,7d %s %s%n", ++count, + fileName, fileType, auxType, eof, buffer.length, created, modified); + + disk.addFile (fileName, fileType, auxType, created, modified, buffer); + } } + + disk.close (); + + return disk.getBuffer (); + } + catch (IOException e) + { + e.printStackTrace (); + return null; + } + catch (DiskFullException e) + { + System.out.println ("disk full: " + diskSize); // go round again } - - disk.close (); - - return disk.getBuffer (); - } - catch (IOException e) - { - e.printStackTrace (); - } - catch (DiskFullException e) - { - e.printStackTrace (); } } @@ -148,6 +176,13 @@ public class NuFX return totalDisks; } + // ---------------------------------------------------------------------------------// + public int getTotalBlocks () + // ---------------------------------------------------------------------------------// + { + return totalBlocks; + } + // ---------------------------------------------------------------------------------// private void listFiles () // ---------------------------------------------------------------------------------// @@ -179,7 +214,8 @@ public class NuFX public static void main (String[] args) throws FileFormatException, IOException // ---------------------------------------------------------------------------------// { - File file = new File ("/Users/denismolony/Dropbox/Examples/SHK/DiversiCopy.3.1.shk"); + File file = new File ( + "/Users/denismolony/Dropbox/Examples/SHK/Disk Disintegrator Deluxe 5.0_D1.SHK"); NuFX nufx = new NuFX (file.toPath ()); System.out.println (nufx); diff --git a/src/com/bytezone/diskbrowser/utilities/Record.java b/src/com/bytezone/diskbrowser/utilities/Record.java index d64cac4..815bd04 100644 --- a/src/com/bytezone/diskbrowser/utilities/Record.java +++ b/src/com/bytezone/diskbrowser/utilities/Record.java @@ -202,6 +202,13 @@ class Record return modified.getLocalDateTime (); } + // ---------------------------------------------------------------------------------// + int getFileSystemID () + // ---------------------------------------------------------------------------------// + { + return fileSystemID; + } + // ---------------------------------------------------------------------------------// int getFileSize () // ---------------------------------------------------------------------------------// diff --git a/src/com/bytezone/diskbrowser/utilities/Utility.java b/src/com/bytezone/diskbrowser/utilities/Utility.java index 3df3623..6f50b4b 100644 --- a/src/com/bytezone/diskbrowser/utilities/Utility.java +++ b/src/com/bytezone/diskbrowser/utilities/Utility.java @@ -195,18 +195,6 @@ public class Utility int minute = buffer[offset + 2] & 0x3F; int hour = buffer[offset + 3] & 0x1F; - if (month < 1 || month > 12) - { - System.out.printf ("Invalid month: %d%n", month); - return null; - } - - if (hour > 23) - { - System.out.printf ("Invalid hour: %d%n", hour); - return null; - } - if (year < 70) year += 2000; else @@ -214,11 +202,12 @@ public class Utility try { - return LocalDateTime.of (year, month - 1, day, hour, minute); + return LocalDateTime.of (year, month, day, hour, minute); } catch (DateTimeException e) { - System.out.println ("Bad date/time"); + System.out.printf ("Bad date/time: %d %d %d %d %d %n", year, month, day, hour, + minute); } }