|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 by Naohide Sano, All rights reserved. |
| 3 | + * |
| 4 | + * Programmed by Naohide Sano |
| 5 | + */ |
| 6 | + |
| 7 | +package discUtils.core.pc98; |
| 8 | + |
| 9 | +import java.io.ByteArrayInputStream; |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.charset.Charset; |
| 12 | +import java.util.logging.Level; |
| 13 | + |
| 14 | +import discUtils.core.DiscFileSystem; |
| 15 | +import discUtils.core.FileSystemInfo; |
| 16 | +import discUtils.core.FileSystemParameters; |
| 17 | +import discUtils.core.VolumeInfo; |
| 18 | +import discUtils.core.coreCompat.EncodingHelper; |
| 19 | +import discUtils.core.vfs.VfsFileSystemFactory; |
| 20 | +import discUtils.core.vfs.VfsFileSystemInfo; |
| 21 | +import discUtils.fat.BootSector; |
| 22 | +import discUtils.fat.FatFileSystem; |
| 23 | +import discUtils.streams.util.Ownership; |
| 24 | +import discUtils.streams.util.StreamUtilities; |
| 25 | +import dotnet4j.io.Stream; |
| 26 | +import vavi.util.Debug; |
| 27 | +import vavi.util.serdes.Serdes; |
| 28 | +import vavix.io.fat.PC98BiosParameterBlock; |
| 29 | + |
| 30 | + |
| 31 | +/** |
| 32 | + * Pc98FileSystemFactory. |
| 33 | + * |
| 34 | + * @author <a href="mailto:umjammer@gmail.com">Naohide Sano</a> (nsano) |
| 35 | + * @version 0.00 2022-07-24 nsano initial version <br> |
| 36 | + */ |
| 37 | +public class Pc98FileSystemFactory implements VfsFileSystemFactory { |
| 38 | + |
| 39 | + public FileSystemInfo[] detect(Stream stream, VolumeInfo volume) { |
| 40 | + if (detectFAT(stream)) { |
| 41 | + return new FileSystemInfo[] { |
| 42 | + new VfsFileSystemInfo("PC98_FAT", "NEC FAT", this::open) |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + return new FileSystemInfo[0]; |
| 47 | + } |
| 48 | + |
| 49 | + private DiscFileSystem open(Stream stream, VolumeInfo volumeInfo, FileSystemParameters parameters) { |
| 50 | + BootSector bs = new Pc98BootSector(stream); |
| 51 | + parameters.setFileNameEncoding(EncodingHelper.forCodePage(932)); // TODO system property |
| 52 | + return new FatFileSystem(stream, bs, Ownership.None, parameters); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Detects if a stream contains a FAT file system. |
| 57 | + * |
| 58 | + * @param stream The stream to inspect. |
| 59 | + * @return {@code true} if the stream appears to be a FAT file system, else |
| 60 | + * {@code false}. |
| 61 | + */ |
| 62 | + private static boolean detectFAT(Stream stream) { |
| 63 | + if (stream.getLength() < 512) { |
| 64 | +Debug.println(Level.FINE, "stream length < 512"); |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + stream.setPosition(0); |
| 69 | + byte[] sector = StreamUtilities.readExact(stream, 512); |
| 70 | + ByteArrayInputStream bais = new ByteArrayInputStream(sector); |
| 71 | + |
| 72 | + PC98BiosParameterBlock bpb; |
| 73 | + try { |
| 74 | + bpb = new PC98BiosParameterBlock(); |
| 75 | + Serdes.Util.deserialize(bais, bpb); |
| 76 | + } catch (IOException e) { |
| 77 | +Debug.println(Level.FINE, e); |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + return bpb.validate(); |
| 82 | + } |
| 83 | +} |
0 commit comments