dmolony-DiskBrowser/src/com/bytezone/diskbrowser/utilities/NuFX.java

187 lines
5.8 KiB
Java
Raw Normal View History

2016-02-24 21:11:14 +00:00
package com.bytezone.diskbrowser.utilities;
2015-06-01 09:35:51 +00:00
2021-04-15 07:27:20 +00:00
import java.io.File;
2015-06-01 09:35:51 +00:00
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
2021-04-17 02:14:06 +00:00
import java.time.LocalDateTime;
2015-06-01 09:35:51 +00:00
import java.util.ArrayList;
import java.util.List;
2021-04-17 02:14:06 +00:00
import com.bytezone.diskbrowser.prodos.write.DiskFullException;
2021-04-16 11:08:59 +00:00
import com.bytezone.diskbrowser.prodos.write.ProdosDisk;
2020-02-07 23:26:38 +00:00
// -----------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
public class NuFX
2020-02-07 23:26:38 +00:00
// -----------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
2021-04-15 07:27:20 +00:00
private MasterHeader masterHeader;
2015-06-01 09:35:51 +00:00
private final byte[] buffer;
private final boolean debug = false;
2020-02-02 10:17:49 +00:00
private final List<Record> records = new ArrayList<> ();
2021-04-15 07:27:20 +00:00
private int totalFiles;
private int totalDisks;
2015-06-01 09:35:51 +00:00
2020-02-07 23:26:38 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
public NuFX (Path path) throws FileFormatException, IOException
2020-02-07 23:26:38 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
buffer = Files.readAllBytes (path);
2016-12-15 00:01:42 +00:00
2021-04-15 07:27:20 +00:00
masterHeader = new MasterHeader (buffer);
2015-06-01 09:35:51 +00:00
int dataPtr = 48;
2021-04-15 07:27:20 +00:00
if (masterHeader.bin2)
2015-06-01 09:35:51 +00:00
dataPtr += 128;
if (debug)
2021-04-15 07:27:20 +00:00
System.out.printf ("%s%n%n", masterHeader);
2015-06-01 09:35:51 +00:00
2021-04-15 07:27:20 +00:00
for (int rec = 0; rec < masterHeader.getTotalRecords (); rec++)
2015-06-01 09:35:51 +00:00
{
2021-03-29 01:04:01 +00:00
Record record = new Record (buffer, dataPtr);
2015-06-01 09:35:51 +00:00
records.add (record);
if (debug)
System.out.printf ("Record: %d%n%n%s%n%n", rec, record);
2021-03-29 01:04:01 +00:00
dataPtr += record.getAttributes () + record.getFileNameLength ();
2015-06-01 09:35:51 +00:00
int threadsPtr = dataPtr;
2021-03-29 01:04:01 +00:00
dataPtr += record.getTotalThreads () * 16;
2015-06-01 09:35:51 +00:00
2021-03-29 01:04:01 +00:00
for (int i = 0; i < record.getTotalThreads (); i++)
2015-06-01 09:35:51 +00:00
{
Thread thread = new Thread (buffer, threadsPtr + i * 16, dataPtr);
2021-04-15 07:27:20 +00:00
record.threads.add (thread);
2015-06-01 09:35:51 +00:00
dataPtr += thread.getCompressedEOF ();
if (debug)
System.out.printf ("Thread: %d%n%n%s%n%n", i, thread);
}
2021-04-15 07:27:20 +00:00
if (record.hasFile ())
++totalFiles;
if (record.hasDisk ())
++totalDisks;
2015-06-01 09:35:51 +00:00
}
}
2020-02-07 23:26:38 +00:00
// ---------------------------------------------------------------------------------//
2021-04-15 07:27:20 +00:00
public byte[] getDiskBuffer ()
2020-02-07 23:26:38 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
2021-04-15 07:27:20 +00:00
if (totalDisks > 0)
{
for (Record record : records)
for (Thread thread : record.threads)
if (thread.hasDisk ())
return thread.getData ();
}
else if (totalFiles > 0)
{
2021-04-16 11:08:59 +00:00
try
{
2021-04-17 02:14:06 +00:00
ProdosDisk disk = new ProdosDisk (3200, "DISKBROWSER");
2021-04-16 11:08:59 +00:00
int count = 0;
for (Record record : records)
{
if (record.hasFile ())
{
String fileName = record.getFileName ();
int fileSize = record.getFileSize ();
2021-04-17 02:14:06 +00:00
byte fileType = (byte) record.getFileType ();
2021-04-16 11:08:59 +00:00
int eof = record.getUncompressedSize ();
2021-04-17 02:14:06 +00:00
int auxType = record.getAuxType ();
LocalDateTime created = record.getCreated ();
LocalDateTime modified = record.getModified ();
2021-04-16 11:08:59 +00:00
byte[] buffer = record.getData ();
2021-04-17 02:14:06 +00:00
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);
2021-04-16 11:08:59 +00:00
}
}
disk.close ();
return disk.getBuffer ();
}
catch (IOException e)
{
e.printStackTrace ();
}
2021-04-17 02:14:06 +00:00
catch (DiskFullException e)
{
e.printStackTrace ();
}
2021-04-15 07:27:20 +00:00
}
2015-06-01 09:35:51 +00:00
return null;
}
2021-04-15 07:27:20 +00:00
// ---------------------------------------------------------------------------------//
public byte[] getFileBuffer (String fileName)
// ---------------------------------------------------------------------------------//
{
for (Record record : records)
if (record.hasFile (fileName))
for (Thread thread : record.threads)
if (thread.hasFile ())
return thread.getData ();
return null;
}
// ---------------------------------------------------------------------------------//
public int getTotalFiles ()
// ---------------------------------------------------------------------------------//
{
return totalFiles;
}
// ---------------------------------------------------------------------------------//
public int getTotalDisks ()
// ---------------------------------------------------------------------------------//
{
return totalDisks;
}
// ---------------------------------------------------------------------------------//
private void listFiles ()
// ---------------------------------------------------------------------------------//
{
int count = 0;
for (Record record : records)
{
if (record.hasFile ())
System.out.printf ("%3d %-35s %,7d %d %,7d%n", count, record.getFileName (),
record.getFileSize (), record.getFileType (), record.getUncompressedSize ());
count++;
}
}
2020-02-07 23:26:38 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
@Override
public String toString ()
2020-02-07 23:26:38 +00:00
// ---------------------------------------------------------------------------------//
2015-06-01 09:35:51 +00:00
{
2021-04-15 07:27:20 +00:00
for (Record record : records)
for (Thread thread : record.threads)
if (thread.hasDisk ())
return thread.toString ();
2015-06-01 09:35:51 +00:00
return "no disk";
}
2021-04-15 07:27:20 +00:00
// ---------------------------------------------------------------------------------//
public static void main (String[] args) throws FileFormatException, IOException
// ---------------------------------------------------------------------------------//
{
2021-04-17 02:14:06 +00:00
File file = new File ("/Users/denismolony/Dropbox/Examples/SHK/DiversiCopy.3.1.shk");
2021-04-15 07:27:20 +00:00
NuFX nufx = new NuFX (file.toPath ());
System.out.println (nufx);
}
2015-06-01 09:35:51 +00:00
}