mirror of
https://github.com/AppleCommander/AppleCommander.git
synced 2026-10-24 23:02:27 +00:00
Switching FileStore interfaces to use DataBuffer instead of byte[].
This commit is contained in:
1 parent
016549d07e
commit
4b3b86e1d8
7 files changed
+27
-24
No files matched your search
+3
-3
@@ -204,11 +204,11 @@ public class DiskFileStoreAdapter implements FileStore {
|
||||
return Container.get(iface, subdirectory);
|
||||
}
|
||||
@Override
|
||||
public byte[] getDataFork() {
|
||||
return fileEntry.getFileData();
|
||||
public DataBuffer getDataFork() {
|
||||
return DataBuffer.wrap(fileEntry.getFileData());
|
||||
}
|
||||
@Override
|
||||
public byte[] getResourceFork() {
|
||||
public Optional<DataBuffer> getResourceFork() {
|
||||
throw new RuntimeException("Not supported by the legacy AppleCommander.");
|
||||
}
|
||||
@Override
|
||||
|
||||
+10
-6
@@ -24,6 +24,7 @@ import com.webcodepro.shrinkit.ThreadRecord;
|
||||
import org.applecommander.filestore.FileEntry;
|
||||
import org.applecommander.filestore.FileStore;
|
||||
import org.applecommander.util.Container;
|
||||
import org.applecommander.util.DataBuffer;
|
||||
import org.applecommander.util.FileMagic;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -171,25 +172,28 @@ public class ShrinkitFileEntry implements FileEntry {
|
||||
return headerBlock.getModWhen();
|
||||
}
|
||||
|
||||
private byte[] decompress(ThreadRecord record) {
|
||||
private DataBuffer decompress(ThreadRecord record) {
|
||||
if (record == null) {
|
||||
return new byte[0];
|
||||
return DataBuffer.create(0);
|
||||
}
|
||||
try (InputStream is = record.getInputStream()) {
|
||||
return is.readAllBytes();
|
||||
return DataBuffer.wrap(is.readAllBytes());
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getDataFork() {
|
||||
public DataBuffer getDataFork() {
|
||||
return decompress(headerBlock.getDataForkThreadRecord());
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getResourceFork() {
|
||||
return decompress(headerBlock.getResourceForkThreadRecord());
|
||||
public Optional<DataBuffer> getResourceFork() {
|
||||
if (headerBlock.getResourceForkThreadRecord() == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(decompress(headerBlock.getResourceForkThreadRecord()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.applecommander.filestore.Directory;
|
||||
import org.applecommander.filestore.FileEntry;
|
||||
import org.applecommander.filestore.FileStore;
|
||||
import org.applecommander.util.Container;
|
||||
import org.applecommander.util.DataBuffer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
@@ -103,16 +104,16 @@ public class ZipFileEntry implements FileEntry {
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getDataFork() {
|
||||
public DataBuffer getDataFork() {
|
||||
try (ZipInputStream inputStream = fileStore.getZipInputStream()) {
|
||||
int n = 0;
|
||||
while (true) {
|
||||
ZipEntry temp = inputStream.getNextEntry();
|
||||
if (temp == null) break;
|
||||
// Since the ZipEntry doesn't have an equals method and we can't trust name (can have multiple
|
||||
// Since the ZipEntry doesn't have an equals method, and we can't trust name (can have multiple
|
||||
// entries with the same name), we track the entry number that we read from originally.
|
||||
if (n == this.entryNumber) {
|
||||
return inputStream.readAllBytes();
|
||||
return DataBuffer.wrap(inputStream.readAllBytes());
|
||||
}
|
||||
n++;
|
||||
}
|
||||
@@ -123,11 +124,6 @@ public class ZipFileEntry implements FileEntry {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getResourceFork() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Optional<T> get(Class<T> iface) {
|
||||
return Container.get(iface, fileStore, entry);
|
||||
|
||||
@@ -21,6 +21,9 @@ package org.applecommander.filestore;
|
||||
|
||||
import org.applecommander.capability.Capability;
|
||||
import org.applecommander.util.Container;
|
||||
import org.applecommander.util.DataBuffer;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* A FileEntry represents a single file on disk.
|
||||
@@ -59,13 +62,13 @@ public interface FileEntry extends Container {
|
||||
* Return the file's data.
|
||||
* This does not include any metadata that may be embedded with the file.
|
||||
*/
|
||||
byte[] getDataFork();
|
||||
DataBuffer getDataFork();
|
||||
/**
|
||||
* Return the resource fork data.
|
||||
* This may not be supported and may throw an exception.
|
||||
* @see Capability
|
||||
*/
|
||||
// TODO should resource form reading and writing be deferred to a container operation?
|
||||
// ... Then we don't need capability (which is our test) and we don't have to implement no-op functions.
|
||||
byte[] getResourceFork();
|
||||
default Optional<DataBuffer> getResourceFork() {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -289,7 +289,7 @@ public class DiskFileStoreTest {
|
||||
}
|
||||
else {
|
||||
try {
|
||||
byte[] data = fileEntry.getDataFork();
|
||||
byte[] data = fileEntry.getDataFork().asBytes();
|
||||
assertNotNull(data);
|
||||
} catch (Exception e) {
|
||||
throw new AssertionError(String.format("Unable to read file '%s'", fileEntry.getName()), e);
|
||||
|
||||
@@ -45,7 +45,7 @@ public class ShrinkitFileStoreTest {
|
||||
assertEquals(1, files.size());
|
||||
FileEntry fileEntry = files.getFirst();
|
||||
assertEquals("TIP.DISK", fileEntry.getName());
|
||||
byte[] data = fileEntry.getDataFork();
|
||||
byte[] data = fileEntry.getDataFork().asBytes();
|
||||
assertNotNull(data);
|
||||
assertEquals(143360, data.length);
|
||||
Source disk = DataBufferSource.create(data, fileEntry.getName()).get();
|
||||
|
||||
@@ -45,7 +45,7 @@ public class ZipFileStoreTest {
|
||||
assertEquals(1, files.size());
|
||||
FileEntry fileEntry = files.getFirst();
|
||||
assertEquals("EMPTY PRODOS.DSK", fileEntry.getName());
|
||||
byte[] data = fileEntry.getDataFork();
|
||||
byte[] data = fileEntry.getDataFork().asBytes();
|
||||
assertNotNull(data);
|
||||
assertEquals(143360, data.length);
|
||||
Source disk = DataBufferSource.create(data, fileEntry.getName()).get();
|
||||
|
||||
Reference in new issue
Block a user