From b9599a7a5a11894101660673474928167187decd Mon Sep 17 00:00:00 2001 From: Robert Vokac Date: Sat, 14 Sep 2024 08:22:33 +0200 Subject: [PATCH] Bug 14: Refactor, improve and finish the Storage api --- .../backend/libgdx/PixelBackendLibGDX.java | 7 +- .../backend/libgdx/UtilsLibGDXImpl.java | 68 ++++++++++++++++++- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/pixelgamelibrary/backend/libgdx/PixelBackendLibGDX.java b/src/main/java/com/pixelgamelibrary/backend/libgdx/PixelBackendLibGDX.java index e4ca27b..4a3df3a 100644 --- a/src/main/java/com/pixelgamelibrary/backend/libgdx/PixelBackendLibGDX.java +++ b/src/main/java/com/pixelgamelibrary/backend/libgdx/PixelBackendLibGDX.java @@ -19,11 +19,6 @@ /////////////////////////////////////////////////////////////////////////////////////////////// package com.pixelgamelibrary.backend.libgdx; -import com.badlogic.gdx.Application; -import static com.badlogic.gdx.Application.ApplicationType.Desktop; -import com.badlogic.gdx.Gdx; -import com.pixelgamelibrary.api.PixelException; -import com.pixelgamelibrary.api.Platform; import com.pixelgamelibrary.api.interfaces.AppI; import com.pixelgamelibrary.api.interfaces.AssetI; import com.pixelgamelibrary.api.interfaces.AudioI; @@ -107,7 +102,7 @@ public class PixelBackendLibGDX implements PixelBackend { public UtilsI utils() { if (pixelUtilsLibGdxImpl == null) { - pixelUtilsLibGdxImpl = new UtilsLibGDXImpl(); + pixelUtilsLibGdxImpl = new UtilsLibGDXImpl(app()); } return pixelUtilsLibGdxImpl; } diff --git a/src/main/java/com/pixelgamelibrary/backend/libgdx/UtilsLibGDXImpl.java b/src/main/java/com/pixelgamelibrary/backend/libgdx/UtilsLibGDXImpl.java index 3bbc9fe..2b24108 100644 --- a/src/main/java/com/pixelgamelibrary/backend/libgdx/UtilsLibGDXImpl.java +++ b/src/main/java/com/pixelgamelibrary/backend/libgdx/UtilsLibGDXImpl.java @@ -23,6 +23,14 @@ import com.badlogic.gdx.utils.Base64Coder; import com.badlogic.gdx.utils.XmlReader; import com.pixelgamelibrary.api.interfaces.XmlElement; import com.pixelgamelibrary.api.interfaces.UtilsI; +import java.util.List; +import java.util.Map; +import com.badlogic.gdx.utils.compression.Lzma; +import com.pixelgamelibrary.api.PixelException; +import com.pixelgamelibrary.api.interfaces.AppI; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; /** * @@ -30,6 +38,12 @@ import com.pixelgamelibrary.api.interfaces.UtilsI; */ public class UtilsLibGDXImpl implements UtilsI { + private final AppI appI; + + public UtilsLibGDXImpl(AppI appI) { + this.appI = appI; + } + @Override public XmlElement parseXml(String xmlString) { XmlReader.Element root = new XmlReader().parse(xmlString); @@ -44,6 +58,58 @@ public class UtilsLibGDXImpl implements UtilsI { public String encodeToBase64(byte[] data) { return String.valueOf(Base64Coder.encode(data)); } - + + @Override + public List listSupportedCompressions() { + throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + } + + @Override + public byte[] compress(byte[] data, String compression, Map arguments) { + switch (compression) { + case "LZMA": { + try { + return compressLzma(data); + } catch (IOException ex) { + appI.error(ex.getMessage()); + throw new PixelException(ex.getMessage()); + } + } + + default: + throw new UnsupportedOperationException("This type of compression is not supported: " + compression); + } + + } + + @Override + public byte[] decompress(byte[] data, String compression) { + switch (compression) { + case "LZMA": { + try { + return decompressLzma(data); + } catch (IOException ex) { + appI.error(ex.getMessage()); + throw new PixelException(ex.getMessage()); + } + } + + default: + throw new UnsupportedOperationException("This type of compression is not supported: " + compression); + } + } + + public byte[] compressLzma(byte[] data) throws IOException { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + Lzma.compress(new ByteArrayInputStream(data), outputStream); + return outputStream.toByteArray(); + } + + public byte[] decompressLzma(byte[] compressedData) throws IOException { + ByteArrayInputStream inputStream = new ByteArrayInputStream(compressedData); + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + Lzma.decompress(inputStream, outputStream); + return outputStream.toByteArray(); + } }