Java tutorial
/* Educational Online Test Delivery System Copyright (c) 2013 American Institutes for Research Distributed under the AIR Open Source License, Version 1.0 See accompanying file AIR-License-1_0.txt or at http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf */ package org.opentestsystem.delivery.testreg.transformer; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.opentestsystem.delivery.testreg.transformer.domain.DwConfigs.DwConfigType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.messaging.Message; import org.springframework.integration.annotation.Header; import org.springframework.integration.annotation.Transformer; import org.springframework.integration.support.MessageBuilder; import org.springframework.stereotype.Component; @Component("tarBundler") public class TarBundler { private static final Logger LOGGER = LoggerFactory.getLogger(TarBundler.class); private static final int BUFFER_SIZE = 4096; /** * Bundles the inputs into a tar which is returned as a byte stream. The input is an array of byte arrays in which * the first element is a filename and the second element is the actual file. Subsequent files follow this same * pattern. If there is an uneven number of elements then the last file will be ignored. * * @param inputs * @return */ @Transformer public Message<File> bundleToTar(final String[] inputs, final @Header("dwBatchUuid") String dwBatchUuid, final @Header("fileSuffix") String fileSuffix, final @Header("recordsSent") int recordsSent, final @Header("tempPaths") List<Path> tempPaths, final @Header("dwConfigType") DwConfigType dwConfigType) { String debugPrefix = dwConfigType + " DW Config: "; long curTime = System.currentTimeMillis(); File tmpTarFile; FileOutputStream tmpTarOutStream; FileInputStream tmpCsvInStream; FileInputStream tmpJsonInStream; try { Path tmpTarPath = Files.createTempFile(DwBatchHandler.DW_TAR_TMP_PREFIX, (dwConfigType == DwConfigType.SBAC ? DwBatchHandler.SBAC_DW_NAME : DwBatchHandler.LOCAL_DW_NAME) + fileSuffix); tempPaths.add(tmpTarPath); tmpTarFile = tmpTarPath.toFile(); LOGGER.debug(debugPrefix + "Created temp TAR file " + tmpTarFile.getAbsolutePath()); tmpTarOutStream = new FileOutputStream(tmpTarFile); tmpCsvInStream = new FileInputStream(inputs[1]); tmpJsonInStream = new FileInputStream(inputs[4]); TarArchiveOutputStream tarOutput = new TarArchiveOutputStream(tmpTarOutStream); String csvFilename = inputs[0]; String jsonFilename = inputs[3]; // tar archive entry for the csv file TarArchiveEntry entry = new TarArchiveEntry(csvFilename); entry.setSize(Integer.valueOf(inputs[2])); tarOutput.putArchiveEntry(entry); byte[] buf = new byte[BUFFER_SIZE]; int len; while ((len = tmpCsvInStream.read(buf)) > 0) { tarOutput.write(buf, 0, len); } tarOutput.closeArchiveEntry(); // tar archive entry for the json file entry = new TarArchiveEntry(jsonFilename); entry.setSize(Integer.valueOf(inputs[5])); tarOutput.putArchiveEntry(entry); buf = new byte[BUFFER_SIZE]; while ((len = tmpJsonInStream.read(buf)) > 0) { tarOutput.write(buf, 0, len); } tarOutput.closeArchiveEntry(); tarOutput.close(); tmpCsvInStream.close(); tmpJsonInStream.close(); } catch (IOException e) { throw new TarBundlerException(debugPrefix + "failure to tar output streams", e); } LOGGER.debug(debugPrefix + "Created TAR output in " + (System.currentTimeMillis() - curTime)); return MessageBuilder.withPayload(tmpTarFile).setHeader("dwBatchUuid", dwBatchUuid) .setHeader("fileSuffix", fileSuffix).setHeader("recordsSent", recordsSent) .setHeader("tempPaths", tempPaths).setHeader("dwConfigType", dwConfigType).build(); } }