Bug 14: Refactor, improve and finish the Storage api

This commit is contained in:
Robert Vokac 2024-09-14 08:22:33 +02:00
parent 6105b8588d
commit b9599a7a5a
No known key found for this signature in database
GPG Key ID: C459E1E4B4A986BB
2 changed files with 68 additions and 7 deletions

View File

@ -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; }

View File

@ -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<String> 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<String, String> 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();
}
}