Skip to content

Commit

Permalink
snaploader-examples: code optimizations
Browse files Browse the repository at this point in the history
*	modified:   snaploader-examples/src/main/java/com/avrsandbox/snaploader/examples/TestBasicFeatures.java
*	modified:   snaploader-examples/src/main/java/com/avrsandbox/snaploader/examples/TestMultipleLoads.java
*	modified:   snaploader-examples/src/main/java/com/avrsandbox/snaploader/examples/TestZipExtractor.java
  • Loading branch information
pavly-gerges committed Jul 19, 2023
1 parent 97ceb1d commit 8028909
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.avrsandbox.snaploader.LibraryInfo;
import com.avrsandbox.snaploader.NativeBinaryLoader;
import com.avrsandbox.snaploader.platform.NativeVariant;
import com.avrsandbox.snaploader.platform.PropertiesProvider;
import com.avrsandbox.snaploader.LoadingCriterion;

/**
Expand All @@ -43,17 +44,11 @@
* @author pavl_g
*/
public final class TestBasicFeatures {

protected static final String userdir = System.getProperty("user.dir");
protected static final String fileSeparator = System.getProperty("file.separator");
protected static final String jar = "jme3-alloc-desktop-1.0.0-pre-gamma-2.jar";
protected static final String libraryBasename = "jmealloc";

protected static final String libraryAbsolutePath = userdir + fileSeparator + "libs";
protected static final String jarFile = libraryAbsolutePath + fileSeparator + jar;
protected static final LibraryInfo libraryInfo = new LibraryInfo(jarFile, null, libraryBasename, libraryAbsolutePath);

protected static final String finalAbsolutePath = libraryAbsolutePath + fileSeparator + "libjmealloc.so";

protected static final LibraryInfo libraryInfo = new LibraryInfo(getJarFile(),
null,
getLibraryBaseName(),
getLibrariesAbsolutePath());

protected static NativeBinaryLoader loader;

Expand Down Expand Up @@ -81,4 +76,28 @@ protected static void printDetails(NativeBinaryLoader loader) {
System.out.println("Is Extracted: " + loader.getNativeDynamicLibrary().isExtracted());
System.out.println("--------------------------------------------------------------");
}

protected static String getLibrariesAbsolutePath() {
return PropertiesProvider.USER_DIR.getSystemProperty() +
PropertiesProvider.FILE_SEPARATOR.getSystemProperty() + "libs";
}

protected static String getJarFilePath() {
return getLibrariesAbsolutePath() +
PropertiesProvider.FILE_SEPARATOR.getSystemProperty() + getJarFile();
}

protected static String getNativeDynamicLibraryPath() {
return getLibrariesAbsolutePath() +
PropertiesProvider.FILE_SEPARATOR.getSystemProperty() +
"lib" + getLibraryBaseName() + ".so";
}

protected static String getJarFile() {
return "jme3-alloc-desktop-1.0.0-pre-gamma-2.jar";
}

protected static String getLibraryBaseName() {
return "jmealloc";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public final class TestMultipleLoads {

public static void main(String[] args) throws InterruptedException, IOException {
TestBasicFeatures.main(args);
new File(TestBasicFeatures.finalAbsolutePath).delete();
new File(TestBasicFeatures.getNativeDynamicLibraryPath()).delete();
Thread.sleep(5000);
TestBasicFeatures.main(args);
new File(TestBasicFeatures.finalAbsolutePath).delete();
new File(TestBasicFeatures.getNativeDynamicLibraryPath()).delete();
Thread.sleep(5000);
TestBasicFeatures.main(args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,45 @@
import com.avrsandbox.snaploader.file.FileExtractor;
import com.avrsandbox.snaploader.file.FileLocator;
import com.avrsandbox.snaploader.file.ZipCompressionType;
import com.avrsandbox.snaploader.platform.PropertiesProvider;

/**
* Tests extracting an image compression from a Zip compression type file using {@link FileExtractor} API.
*
* @author pavl_g
*/
public class TestZipExtractor {

protected static final String directory = TestBasicFeatures.libraryAbsolutePath + TestBasicFeatures.fileSeparator + "jmelogo700.zip";
protected static final String destination = TestBasicFeatures.libraryAbsolutePath + TestBasicFeatures.fileSeparator + "jmelogo700.png";


public static void main(String[] args) throws IOException {
/* Locates the image inside the Zip Compression */
final FileLocator fileLocator = new FileLocator(directory, "jmelogo700.png", ZipCompressionType.ZIP);
final FileLocator fileLocator = new FileLocator(getZipAbsolutePath(), getFilePath(), ZipCompressionType.ZIP);
/* Extracts the image file from the Zip Compression */
final FileExtractor fileExtractor = new FileExtractor(fileLocator, destination);
final FileExtractor fileExtractor = new FileExtractor(fileLocator, getExtractionPath());
/* CLOSE/CLEAR I/O Resources */
fileExtractor.setExtractionListener(() -> clearResources(fileExtractor));
fileExtractor.extract();
}

protected static String getZipAbsolutePath() {
return TestBasicFeatures.getLibrariesAbsolutePath() +
PropertiesProvider.FILE_SEPARATOR.getSystemProperty() + "jmelogo700.zip";
}

protected static String getExtractionPath() {
return TestBasicFeatures.getLibrariesAbsolutePath() +
PropertiesProvider.FILE_SEPARATOR.getSystemProperty() + getFilePath();
}

protected static String getFilePath() {
return "jmelogo700.png";
}

private static void clearResources(FileExtractor fileExtractor) {
try {
fileExtractor.getFileLocator().close();
fileExtractor.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

0 comments on commit 8028909

Please sign in to comment.