Skip to content

Commit

Permalink
Merge pull request #20 from cml37/robustify
Browse files Browse the repository at this point in the history
Robustify
  • Loading branch information
cml37 authored Mar 5, 2024
2 parents 9739cc6 + 24917c3 commit 7f780bd
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 40 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/lenderman/nabu/adapter/loader/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ public interface Loader
* desire to preserve data)
* @return Optional<byte[]>
*/
public Optional<byte[]> tryGetData(String path, String preserveDataPath)
throws Exception;
public Optional<byte[]> tryGetData(String path, String preserveDataPath);

/**
* Try to get the containing directory of the specified file
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/com/lenderman/nabu/adapter/loader/LocalLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,19 @@ public class LocalLoader implements Loader
*/
@Override
public Optional<byte[]> tryGetData(String path, String preserveDataPath)
throws Exception
{
if (path.equalsIgnoreCase(Settings.HeadlessBootLoader))
{
return Optional.of(IOUtils.toByteArray(getClass().getClassLoader()
.getResourceAsStream(Settings.HeadlessBootResource)));
try
{
return Optional.of(IOUtils.toByteArray(
getClass().getClassLoader().getResourceAsStream(
Settings.HeadlessBootResource)));
}
catch (Exception ex)
{
return Optional.empty();
}
}

try
Expand All @@ -65,7 +72,14 @@ public Optional<String> tryGetDirectory(String path) throws Exception
{
try
{
return Optional.of(Paths.get(path).getParent().toString());
if (Files.isDirectory(Paths.get(path)))
{
return Optional.of(path);
}
else
{
return Optional.of(Paths.get(path).getParent().toString());
}
}
catch (Exception ex)
{
Expand Down
40 changes: 23 additions & 17 deletions src/main/java/com/lenderman/nabu/adapter/loader/WebLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,34 @@ public class WebLoader implements Loader
*/
@Override
public Optional<byte[]> tryGetData(String path, String preserveDataPath)
throws Exception
{
URLConnection connection = WebUtils.openWebClient(path);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] bytes = new byte[8192];
int len;
while ((len = connection.getInputStream().read(bytes)) > 0)
try
{
buffer.write(bytes, 0, len);
}
URLConnection connection = WebUtils.openWebClient(path);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] bytes = new byte[8192];
int len;
while ((len = connection.getInputStream().read(bytes)) > 0)
{
buffer.write(bytes, 0, len);
}

if (preserveDataPath != null)
if (preserveDataPath != null)
{
logger.debug("Preserving {}", path);
Path outputFile = Paths.get(preserveDataPath,
getPathSeparator(), new URI(path).getPath());
Files.createDirectories(outputFile.getParent());
Files.write(outputFile, buffer.toByteArray(),
StandardOpenOption.CREATE);
}
buffer.close();
return (Optional.of(buffer.toByteArray()));
}
catch (Exception ex)
{
logger.debug("Preserving {}", path);
Path outputFile = Paths.get(preserveDataPath, getPathSeparator(),
new URI(path).getPath());
Files.createDirectories(outputFile.getParent());
Files.write(outputFile, buffer.toByteArray(),
StandardOpenOption.CREATE);
return Optional.empty();
}
buffer.close();
return (Optional.of(buffer.toByteArray()));
}

/**
Expand Down
33 changes: 28 additions & 5 deletions src/main/java/com/lenderman/nabu/adapter/server/NabuServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ else if (this.settings.getPath().toLowerCase().endsWith(".pak")
+ ".nabu";
data = loader.tryGetData(segmentFullPath,
this.settings.getPreservedPath());
if (!data.isPresent())
{
segmentFullPath = directory.get()
+ loader.getPathSeparator() + segmentName
+ ".NABU";
data = loader.tryGetData(segmentFullPath,
this.settings.getPreservedPath());
}
if (data.isPresent())
{
logger.debug("Creating NABU segment {} from {}",
Expand All @@ -339,11 +347,26 @@ else if (this.settings.getPath().toLowerCase().endsWith(".pak")
String pakFullPath = directory.get()
+ loader.getPathSeparator() + segmentName
+ ".pak";
logger.debug("Loading NABU segment {} from {}",
String.format("%06x", segmentNumber),
pakFullPath);
segment = Optional.of(SegmentManager
.loadPackets(segmentNumber, data.get()));
data = loader.tryGetData(pakFullPath,
this.settings.getPreservedPath());
if (!data.isPresent())
{
pakFullPath = directory.get()
+ loader.getPathSeparator()
+ segmentName + ".PAK";
data = loader.tryGetData(pakFullPath,
this.settings.getPreservedPath());
}

if (data.isPresent())
{
logger.debug("Loading NABU segment {} from {}",
String.format("%06x", segmentNumber),
pakFullPath);
segment = Optional
.of(SegmentManager.loadPackets(
segmentNumber, data.get()));
}
}
}
}
Expand Down
29 changes: 17 additions & 12 deletions src/main/resources/NabuNetwork.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@
<Name>NABU 1984 Cycle (need to verify)</Name>
<Url>https://adaptor.thenabunetwork.com/cycle</Url>
</Target>
<!--Target>
<Target>
<TargetType>NabuNetwork</TargetType>
<Name>NABU Network 1984 Cycle v1 (Dec 2022)</Name>
<Url>https://adaptor.thenabunetwork.com/cycle1984</Url>
</Target-->
<!--Target>
<Name>NABU Network 1984 Cycle v1</Name>
<Url>https://adaptor.thenabunetwork.com/cycle1</Url>
</Target>
<Target>
<TargetType>NabuNetwork</TargetType>
<Name>NABU Network 1984 Cycle v2 (Mar 2023)</Name>
<Url>https://adaptor.thenabunetwork.com/cycle2022</Url>
</Target-->
<!--Target>
<Name>NABU Network 1984 Cycle v2</Name>
<Url>https://adaptor.thenabunetwork.com/cycle2</Url>
</Target>
<Target>
<TargetType>NabuNetwork</TargetType>
<Name>NABU Net 1986 Leos Cycle (Feb 2024)</Name>
<Url>https://adaptor.thenabunetwork.com/cycle1986</Url>
</Target-->
<Name>NABU Network 1996 Cycle v3</Name>
<Url>https://adaptor.thenabunetwork.com/cycle3</Url>
</Target>
<Target>
<TargetType>NabuNetwork</TargetType>
<Name>NABU Network DJ Cycle</Name>
<Url>https://adaptor.thenabunetwork.com/cycledj</Url>
</Target>
<Target>
<TargetType>NabuNetwork</TargetType>
<Name>CP/M 2.2 (ISHKUR)</Name>
Expand Down

0 comments on commit 7f780bd

Please sign in to comment.