Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
3DSystemNavigationBackend/src/main/java/com/lmco/spectrum/systemnavigation3d/service/SmgService.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
85 lines (69 sloc)
2.67 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lmco.spectrum.systemnavigation3d.service; | |
import com.amazonaws.util.IOUtils; | |
import com.lmco.spectrum.systemnavigation3d.util.io.SfxZipInputStream; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import javax.annotation.Nullable; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipInputStream; | |
import java.util.zip.ZipOutputStream; | |
@Service | |
public class SmgService { | |
private static final Logger log = LoggerFactory.getLogger(SmgService.class); | |
private static final String PRODUCT_XML = "product.smgXml"; | |
@Autowired | |
TransformationService transformationService; | |
// Max capacity is 2^31-1 bytes or ~2GB. | |
@Nullable | |
public byte[] processSmg(byte[] data){ | |
Map<String, byte[]> entryMap = getSmgXmlEntry(data); | |
byte[] smgXmlData = entryMap.get(PRODUCT_XML); | |
if(smgXmlData != null) { | |
String smgXml = new String(smgXmlData); | |
smgXml = transformationService.transform(smgXml); | |
entryMap.put(PRODUCT_XML, smgXml.getBytes()); // Update XML entry. | |
return repackageSmg(entryMap); | |
} else { | |
return null; | |
} | |
} | |
public Map<String, byte[]> getSmgXmlEntry(byte[] data) { | |
Map<String, byte[]> entryMap = new HashMap<>(); | |
try(ZipInputStream zis = new ZipInputStream(new SfxZipInputStream(new ByteArrayInputStream(data)))) { | |
ZipEntry entry; | |
while((entry = zis.getNextEntry()) != null) { | |
entryMap.put(entry.getName(), IOUtils.toByteArray(zis)); | |
zis.closeEntry(); | |
} | |
return entryMap; | |
} catch (IOException e) { | |
log.error("Failed to read .smgXml entry!"); | |
e.printStackTrace(); | |
} | |
return entryMap; | |
} | |
@Nullable | |
public byte[] repackageSmg(Map<String, byte[]> entryMap) { | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
try (ZipOutputStream zos = new ZipOutputStream(bos)) { | |
for(Map.Entry<String, byte[]> entry : entryMap.entrySet()) { | |
ZipEntry zipEntry = new ZipEntry(entry.getKey()); | |
zos.putNextEntry(zipEntry); | |
zos.write(entry.getValue()); | |
zos.closeEntry(); | |
} | |
} catch (IOException e) { | |
log.error("Failed to repackage smg file"); | |
e.printStackTrace(); | |
return null; | |
} | |
return bos.toByteArray(); | |
} | |
} |