Skip to content
Permalink
ff84ec79dd
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
85 lines (69 sloc) 2.67 KB
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();
}
}