Adding Utilities and shifting to use it.

This commit is contained in:
Rob Greene 2018-06-03 12:31:07 -05:00
parent cd5d7ac142
commit b72c9f9b74
2 changed files with 34 additions and 2 deletions

View File

@ -0,0 +1,31 @@
package io.github.applecommander.applesingle;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class Utilities {
private Utilities() { /* Prevent construction */ }
/** Utility method to read all bytes from an InputStream. */
public static byte[] toByteArray(InputStream inputStream) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
while (true) {
byte[] buf = new byte[1024];
int len = inputStream.read(buf);
if (len == -1) break;
outputStream.write(buf, 0, len);
}
outputStream.flush();
return outputStream.toByteArray();
}
/** Convert bytes in an Entry to a 7-bit ASCII string. Emphasis on 7-bit in case Apple II high bit is along for the ride. */
public static String entryToAsciiString(Entry entry) {
byte[] data = entry.getData();
for (int i=0; i<data.length; i++) {
data[i] = (byte)(data[i] & 0x7f);
}
return new String(data);
}
}

View File

@ -9,6 +9,7 @@ import java.util.Optional;
import java.util.concurrent.Callable;
import io.github.applecommander.applesingle.AppleSingle;
import io.github.applecommander.applesingle.Utilities;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
@ -99,7 +100,7 @@ public class CreateCommand implements Callable<Void> {
public byte[] prepDataFork() throws IOException {
byte[] dataFork = null;
if (stdinForkType == ForkType.data) {
dataFork = AppleSingle.toByteArray(System.in);
dataFork = Utilities.toByteArray(System.in);
} else if (dataForkFile != null) {
dataFork = Files.readAllBytes(dataForkFile);
}
@ -116,7 +117,7 @@ public class CreateCommand implements Callable<Void> {
public byte[] prepResourceFork() throws IOException {
byte[] resourceFork = null;
if (stdinForkType == ForkType.resource) {
resourceFork = AppleSingle.toByteArray(System.in);
resourceFork = Utilities.toByteArray(System.in);
} else if (resourceForkFile != null) {
resourceFork = Files.readAllBytes(resourceForkFile);
}