Example usage for org.apache.commons.compress.compressors CompressorStreamFactory createCompressorInputStream

List of usage examples for org.apache.commons.compress.compressors CompressorStreamFactory createCompressorInputStream

Introduction

In this page you can find the example usage for org.apache.commons.compress.compressors CompressorStreamFactory createCompressorInputStream.

Prototype

public CompressorInputStream createCompressorInputStream(final String name, final InputStream in)
        throws CompressorException 

Source Link

Document

Create a compressor input stream from a compressor name and an input stream.

Usage

From source file:com.milaboratory.util.CompressionType.java

private static InputStream createInputStream(CompressionType ct, InputStream is) throws IOException {
    switch (ct) {
    case None://from  w  w w.  j av a 2 s.c o m
        return is;
    case GZIP:
        return new GZIPInputStream(is, 2048);
    case BZIP2:
        CompressorStreamFactory factory = new CompressorStreamFactory();
        try {
            return factory.createCompressorInputStream(CompressorStreamFactory.BZIP2,
                    new BufferedInputStream(is));
        } catch (CompressorException e) {
            throw new IOException(e);
        }
    }
    throw new NullPointerException();
}

From source file:com.milaboratory.core.io.CompressionType.java

private static InputStream createInputStream(CompressionType ct, InputStream is, int buffer)
        throws IOException {
    switch (ct) {
    case None://from  w  ww  .  j  av  a  2  s  . c  o  m
        return is;
    case GZIP:
        return new GZIPInputStream(is, buffer);
    case BZIP2:
        CompressorStreamFactory factory = new CompressorStreamFactory();
        try {
            return factory.createCompressorInputStream(CompressorStreamFactory.BZIP2,
                    new BufferedInputStream(is));
        } catch (CompressorException e) {
            throw new IOException(e);
        }
    }
    throw new NullPointerException();
}

From source file:edu.utah.bmi.ibiomes.io.IBIOMESFileReader.java

/**
 * Get input stream reader// w w  w  .j a v  a2 s.co m
 * @param hex Hexadecimal representation of first bits 
 * @return Input stream
 * @throws CompressorException
 * @throws IOException
 */
private CompressorInputStream getInputStreamForCompressedFile(String hex)
        throws CompressorException, IOException {
    if (hex.startsWith(HEX_HEADER_BZIP))
        compressionScheme = CompressorStreamFactory.BZIP2;
    else if (hex.startsWith(HEX_HEADER_GZIP))
        compressionScheme = CompressorStreamFactory.GZIP;

    if (compressionScheme != null) {
        CompressorStreamFactory factory = new CompressorStreamFactory();
        CompressorInputStream inputStream = factory.createCompressorInputStream(compressionScheme,
                new FileInputStream(file));
        return inputStream;
    } else //unsupported
    {
        throw new IOException(
                "Cannot read file " + file.getAbsolutePath() + ". Unsupported compression scheme.");
    }
}

From source file:fr.gael.ccsds.sip.archive.TgzArchiveManager.java

@Override
public File copy(final File src, final File tar_file, final String dst) throws Exception {
    final ArchiveStreamFactory asf = new ArchiveStreamFactory();
    final CompressorStreamFactory csf = new CompressorStreamFactory();

    // Case of tar already exist: all the entries must be copied..
    if (tar_file.exists()) {
        final FileInputStream fis = new FileInputStream(tar_file);
        final CompressorInputStream cis = csf.createCompressorInputStream(CompressorStreamFactory.GZIP, fis);
        final ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, cis);

        final File tempFile = File.createTempFile("updateTar", "tar");
        final FileOutputStream fos = new FileOutputStream(tempFile);
        final CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, fos);
        final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos);

        // copy the existing entries
        ArchiveEntry nextEntry;/*from   w ww  .j  a v a  2  s.  c o  m*/
        while ((nextEntry = ais.getNextEntry()) != null) {
            aos.putArchiveEntry(nextEntry);
            IOUtils.copy(ais, aos);
            aos.closeArchiveEntry();
        }

        // create the new entry
        final TarArchiveEntry entry = new TarArchiveEntry(src, dst);
        entry.setSize(src.length());
        aos.putArchiveEntry(entry);
        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, aos);
        sfis.close();
        aos.closeArchiveEntry();

        aos.finish();
        ais.close();
        aos.close();
        fis.close();

        // copies the new file over the old
        tar_file.delete();
        tempFile.renameTo(tar_file);
        return tar_file;
    } else {
        final FileOutputStream fos = new FileOutputStream(tar_file);
        final CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, fos);
        final ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos);

        // create the new entry
        final TarArchiveEntry entry = new TarArchiveEntry(src, dst);
        entry.setSize(src.length());
        aos.putArchiveEntry(entry);
        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, aos);
        sfis.close();
        aos.closeArchiveEntry();

        aos.finish();
        aos.close();
        fos.close();
    }
    return tar_file;
}

From source file:org.apache.marmotta.loader.core.MarmottaLoader.java

/**
 * Load data from the reader given as first argument into the handler given as second argument.
 *
 * @param file     file to read the data from; in case a compression format is not explicitly given, the method will
 *                 try to decide from the file name if the file is in a compressed format
 * @param handler  handler to add the data to
 * @param format   format to use for creating the parser or null for auto-detection
 * @param compression compression format to use, or null for auto-detection (see formats in org.apache.commons.compress.compressors.CompressorStreamFactory)
 * @throws RDFParseException//ww w .j  a va2  s .c o m
 * @throws IOException
 */
public void loadFile(File file, LoaderHandler handler, RDFFormat format, String compression)
        throws RDFParseException, IOException {
    log.info("loading file {} ...", file);

    CompressorStreamFactory cf = new CompressorStreamFactory();
    cf.setDecompressConcatenated(true);

    // detect the file compression
    String detectedCompression = detectCompression(file);
    if (compression == null) {
        if (detectedCompression != null) {
            log.info("using auto-detected compression ({})", detectedCompression);
            compression = detectedCompression;
        }
    } else {
        if (detectedCompression != null && !compression.equals(detectedCompression)) {
            log.warn("user-specified compression ({}) overrides auto-detected compression ({})", compression,
                    detectedCompression);
        } else {
            log.info("using user-specified compression ({})", compression);
        }
    }

    // detect the file format
    RDFFormat detectedFormat = Rio.getParserFormatForFileName(uncompressedName(file));
    if (format == null) {
        if (detectedFormat != null) {
            log.info("using auto-detected format ({})", detectedFormat.getName());
            format = detectedFormat;
        } else {
            throw new RDFParseException("could not detect input format of file " + file);
        }
    } else {
        if (detectedFormat != null && !format.equals(detectedFormat)) {
            log.warn("user-specified format ({}) overrides auto-detected format ({})", format.getName(),
                    detectedFormat.getName());
        }
    }

    // create input stream from file and wrap in compressor stream
    InputStream in;
    InputStream fin = new BufferedInputStream(new FileInputStream(file));
    try {
        if (compression != null) {
            if (CompressorStreamFactory.GZIP.equalsIgnoreCase(compression)) {
                in = new GzipCompressorInputStream(fin, true);
            } else if (CompressorStreamFactory.BZIP2.equalsIgnoreCase(compression)) {
                in = new BZip2CompressorInputStream(fin, true);
            } else if (CompressorStreamFactory.XZ.equalsIgnoreCase(compression)) {
                in = new XZCompressorInputStream(fin, true);
            } else {
                // does not honour decompressConcatenated
                in = cf.createCompressorInputStream(compression, fin);
            }
        } else {
            in = cf.createCompressorInputStream(fin);
        }
    } catch (CompressorException ex) {
        log.info("no compression detected, using plain input stream");
        in = fin;
    }

    // load using the input stream
    load(in, handler, format);
}

From source file:org.github.evenjn.gzip.GZipDecoderBlueprint.java

private static InputStream decode(Hook hook, InputStream is) {
    try {/*from ww w .jav a  2 s.  c  om*/
        /**
         * This never throws, it's not closeable.
         */
        CompressorStreamFactory compressorStreamFactory = new CompressorStreamFactory();

        /**
         * This may throw. The result is autocloseable.
         */
        CompressorInputStream createCompressorInputStream = compressorStreamFactory
                .createCompressorInputStream(CompressorStreamFactory.GZIP, is);

        return hook.hook(createCompressorInputStream);
    } catch (CompressorException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.kaaproject.kaa.server.control.service.sdk.CppSdkGenerator.java

@Override
public FileData generateSdk(String buildVersion, List<BootstrapNodeInfo> bootstrapNodes,
        SdkProfileDto sdkProfile, String profileSchemaBody, String notificationSchemaBody,
        String configurationProtocolSchemaBody, String configurationBaseSchema, byte[] defaultConfigurationData,
        List<EventFamilyMetadata> eventFamilies, String logSchemaBody) throws Exception {

    String sdkToken = sdkProfile.getToken();
    Integer configurationSchemaVersion = sdkProfile.getConfigurationSchemaVersion();
    final Integer profileSchemaVersion = sdkProfile.getProfileSchemaVersion();
    Integer notificationSchemaVersion = sdkProfile.getNotificationSchemaVersion();
    Integer logSchemaVersion = sdkProfile.getLogSchemaVersion();
    String defaultVerifierToken = sdkProfile.getDefaultVerifierToken();

    String sdkTemplateLocation = Environment.getServerHomeDir() + "/" + CPP_SDK_DIR + "/" + CPP_SDK_PREFIX
            + buildVersion + ".tar.gz";

    LOG.debug("Lookup Java SDK template: {}", sdkTemplateLocation);

    CompressorStreamFactory csf = new CompressorStreamFactory();
    ArchiveStreamFactory asf = new ArchiveStreamFactory();

    CompressorInputStream cis = csf.createCompressorInputStream(CompressorStreamFactory.GZIP,
            new FileInputStream(sdkTemplateLocation));

    final ArchiveInputStream templateArchive = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, cis);

    ByteArrayOutputStream sdkOutput = new ByteArrayOutputStream();
    CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, sdkOutput);

    ArchiveOutputStream sdkFile = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos);

    Map<String, TarEntryData> replacementData = new HashMap<>();

    List<TarEntryData> cppSources = new ArrayList<>();

    // TODO: remove all version fields and add single sdkToken field
    // create entry for default properties
    TarArchiveEntry entry = new TarArchiveEntry(SDK_DEFAULTS_PATH);
    byte[] data = generateKaaDefaults(bootstrapNodes, sdkToken, defaultConfigurationData, defaultVerifierToken);

    entry.setSize(data.length);/*from w ww .ja v a  2s . c o  m*/
    TarEntryData tarEntry = new TarEntryData(entry, data);
    cppSources.add(tarEntry);

    Map<String, String> profileVars = new HashMap<>();
    profileVars.put(SDK_PROFILE_VERSION_VAR, profileSchemaVersion.toString());
    cppSources.addAll(processFeatureSchema(profileSchemaBody, PROFILE_SCHEMA_AVRO_SRC,
            PROFILE_DEFINITIONS_TEMPLATE, PROFILE_DEFINITIONS_PATH, profileVars));

    cppSources.addAll(processFeatureSchema(notificationSchemaBody, NOTIFICATION_SCHEMA_AVRO_SRC,
            NOTIFICATION_DEFINITIONS_TEMPLATE, NOTIFICATION_DEFINITIONS_PATH, null));
    cppSources.addAll(processFeatureSchema(logSchemaBody, LOG_SCHEMA_AVRO_SRC, LOG_DEFINITIONS_TEMPLATE,
            LOG_DEFINITIONS_PATH, null));
    cppSources.addAll(processFeatureSchema(configurationBaseSchema, CONFIGURATION_SCHEMA_AVRO_SRC,
            CONFIGURATION_DEFINITIONS_TEMPLATE, CONFIGURATION_DEFINITIONS_PATH, null));

    if (eventFamilies != null && !eventFamilies.isEmpty()) {
        cppSources.addAll(CppEventSourcesGenerator.generateEventSources(eventFamilies));
    }

    for (TarEntryData entryData : cppSources) {
        replacementData.put(entryData.getEntry().getName(), entryData);
    }

    ArchiveEntry archiveEntry;
    while ((archiveEntry = templateArchive.getNextEntry()) != null) {
        if (!archiveEntry.isDirectory()) {
            if (replacementData.containsKey(archiveEntry.getName())) {
                TarEntryData entryData = replacementData.remove(archiveEntry.getName());
                sdkFile.putArchiveEntry(entryData.getEntry());
                sdkFile.write(entryData.getData());
            } else {
                sdkFile.putArchiveEntry(archiveEntry);
                IOUtils.copy(templateArchive, sdkFile);
            }
        } else {
            sdkFile.putArchiveEntry(archiveEntry);
        }
        sdkFile.closeArchiveEntry();
    }

    templateArchive.close();

    for (String entryName : replacementData.keySet()) {
        TarEntryData entryData = replacementData.get(entryName);
        sdkFile.putArchiveEntry(entryData.getEntry());
        sdkFile.write(entryData.getData());
        sdkFile.closeArchiveEntry();
    }

    sdkFile.finish();
    sdkFile.close();

    String sdkFileName = CPP_SDK_PREFIX + sdkProfile.getToken() + ".tar.gz";

    byte[] sdkData = sdkOutput.toByteArray();

    FileData sdk = new FileData();
    sdk.setFileName(sdkFileName);
    sdk.setFileData(sdkData);
    return sdk;
}

From source file:org.kaaproject.kaa.server.control.service.sdk.CSdkGenerator.java

@Override
public FileData generateSdk(String buildVersion, List<BootstrapNodeInfo> bootstrapNodes,
        SdkProfileDto sdkProfile, String profileSchemaBody, String notificationSchemaBody,
        String configurationProtocolSchemaBody, String configurationBaseSchemaBody,
        byte[] defaultConfigurationData, List<EventFamilyMetadata> eventFamilies, String logSchemaBody)
        throws Exception {

    String sdkToken = sdkProfile.getToken();
    Integer profileSchemaVersion = sdkProfile.getProfileSchemaVersion();
    String defaultVerifierToken = sdkProfile.getDefaultVerifierToken();

    String sdkTemplateLocation = Environment.getServerHomeDir() + "/" + C_SDK_DIR + "/" + C_SDK_PREFIX
            + buildVersion + ".tar.gz";

    LOG.debug("Lookup C SDK template: {}", sdkTemplateLocation);

    CompressorStreamFactory csf = new CompressorStreamFactory();
    ArchiveStreamFactory asf = new ArchiveStreamFactory();

    CompressorInputStream cis = csf.createCompressorInputStream(CompressorStreamFactory.GZIP,
            new FileInputStream(sdkTemplateLocation));

    final ArchiveInputStream templateArchive = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, cis);

    ByteArrayOutputStream sdkOutput = new ByteArrayOutputStream();
    CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, sdkOutput);

    ArchiveOutputStream sdkFile = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos);

    Map<String, TarEntryData> replacementData = new HashMap<>();

    List<TarEntryData> sources = new ArrayList<>();

    if (!StringUtils.isBlank(profileSchemaBody)) {
        sources.addAll(generateProfileSources(profileSchemaBody));
    }//ww  w  .  ja  v  a2 s . c o m

    if (!StringUtils.isBlank(logSchemaBody)) {
        sources.addAll(generateLogSources(logSchemaBody));
    }

    if (!StringUtils.isBlank(configurationBaseSchemaBody)) {
        sources.addAll(generateConfigurationSources(configurationBaseSchemaBody));
    }

    if (!StringUtils.isBlank(notificationSchemaBody)) {
        sources.addAll(generateNotificationSources(notificationSchemaBody));
    }

    if (eventFamilies != null && !eventFamilies.isEmpty()) {
        sources.addAll(CEventSourcesGenerator.generateEventSources(eventFamilies));
    }

    for (TarEntryData entryData : sources) {
        replacementData.put(entryData.getEntry().getName(), entryData);
    }

    ArchiveEntry archiveEntry;
    while ((archiveEntry = templateArchive.getNextEntry()) != null) {
        if (!archiveEntry.isDirectory()) {
            if (archiveEntry.getName().equals(KAA_DEFAULTS_HEADER)) {
                // TODO: eliminate schema versions and substitute them for a single sdkToken
                byte[] kaaDefaultsData = generateKaaDefaults(bootstrapNodes, sdkToken, profileSchemaVersion,
                        configurationProtocolSchemaBody, defaultConfigurationData, defaultVerifierToken);

                TarArchiveEntry kaaDefaultsEntry = new TarArchiveEntry(KAA_DEFAULTS_HEADER);
                kaaDefaultsEntry.setSize(kaaDefaultsData.length);
                sdkFile.putArchiveEntry(kaaDefaultsEntry);
                sdkFile.write(kaaDefaultsData);
            } else if (archiveEntry.getName().equals(KAA_CMAKEGEN)) {
                // Ignore duplicate source names
                List<String> sourceNames = new LinkedList<>();
                for (TarEntryData sourceEntry : sources) {
                    String fileName = sourceEntry.getEntry().getName();
                    if (fileName.endsWith(C_SOURCE_SUFFIX) && !sourceNames.contains(fileName)) {
                        sourceNames.add(fileName);
                    }
                }

                VelocityContext context = new VelocityContext();
                context.put("sourceNames", sourceNames);
                String sourceData = generateSourceFromTemplate(TEMPLATE_DIR + File.separator + "CMakeGen.vm",
                        context);

                TarArchiveEntry kaaCMakeEntry = new TarArchiveEntry(KAA_CMAKEGEN);
                kaaCMakeEntry.setSize(sourceData.length());
                sdkFile.putArchiveEntry(kaaCMakeEntry);
                sdkFile.write(sourceData.getBytes());
            } else if (replacementData.containsKey(archiveEntry.getName())) {
                TarEntryData entryData = replacementData.remove(archiveEntry.getName());
                sdkFile.putArchiveEntry(entryData.getEntry());
                sdkFile.write(entryData.getData());
            } else {
                sdkFile.putArchiveEntry(archiveEntry);
                IOUtils.copy(templateArchive, sdkFile);
            }
        } else {
            sdkFile.putArchiveEntry(archiveEntry);
        }

        sdkFile.closeArchiveEntry();
    }

    templateArchive.close();

    for (String entryName : replacementData.keySet()) {
        TarEntryData entryData = replacementData.get(entryName);
        sdkFile.putArchiveEntry(entryData.getEntry());
        sdkFile.write(entryData.getData());
        sdkFile.closeArchiveEntry();
    }

    sdkFile.finish();
    sdkFile.close();

    String sdkFileName = C_SDK_PREFIX + sdkProfile.getToken() + ".tar.gz";

    FileData sdk = new FileData();
    sdk.setFileName(sdkFileName);
    sdk.setFileData(sdkOutput.toByteArray());

    return sdk;
}

From source file:org.kaaproject.kaa.server.control.service.sdk.ObjCSdkGenerator.java

@Override
public FileData generateSdk(String buildVersion, List<BootstrapNodeInfo> bootstrapNodes,
        SdkProfileDto sdkProperties, String profileSchemaBody, String notificationSchemaBody,
        String configurationProtocolSchemaBody, String configurationBaseSchemaBody,
        byte[] defaultConfigurationData, List<EventFamilyMetadata> eventFamilies, String logSchemaBody)
        throws Exception {

    final String sdkToken = sdkProperties.getToken();
    final String defaultVerifierToken = sdkProperties.getDefaultVerifierToken();

    String sdkTemplateLocation = System.getProperty("server_home_dir") + "/" + SDK_TEMPLATE_DIR + SDK_PREFIX
            + buildVersion + ".tar.gz";

    LOG.debug("Lookup Objective C SDK template: {}", sdkTemplateLocation);

    CompressorStreamFactory csf = new CompressorStreamFactory();
    ArchiveStreamFactory asf = new ArchiveStreamFactory();

    CompressorInputStream cis = csf.createCompressorInputStream(CompressorStreamFactory.GZIP,
            new FileInputStream(sdkTemplateLocation));

    final ArchiveInputStream templateArchive = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, cis);

    ByteArrayOutputStream sdkOutput = new ByteArrayOutputStream();
    CompressorOutputStream cos = csf.createCompressorOutputStream(CompressorStreamFactory.GZIP, sdkOutput);

    ArchiveOutputStream sdkFile = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, cos);

    Map<String, TarEntryData> replacementData = new HashMap<>();

    List<TarEntryData> objcSources = new ArrayList<>();

    if (StringUtils.isNotBlank(profileSchemaBody)) {
        LOG.debug("Generating profile schema");
        Schema profileSchema = new Schema.Parser().parse(profileSchemaBody);
        String profileClassName = PROFILE_NAMESPACE + profileSchema.getName();

        String profileCommonHeader = readResource(PROFILE_TEMPLATE_DIR + PROFILE_COMMON + _H + TEMPLATE_SUFFIX)
                .replaceAll(PROFILE_CLASS_VAR, profileClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(profileCommonHeader,
                KAA_ROOT + PROFILE_DIR + PROFILE_COMMON + _H));

        String profileCommonSource = readResource(PROFILE_TEMPLATE_DIR + PROFILE_COMMON + _M + TEMPLATE_SUFFIX)
                .replaceAll(PROFILE_CLASS_VAR, profileClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(profileCommonSource,
                KAA_ROOT + PROFILE_DIR + PROFILE_COMMON + _M));

        objcSources.addAll(generateSourcesFromSchema(profileSchema, PROFILE_GEN, PROFILE_NAMESPACE));
    }/*from   w w  w.ja  v a2 s.  c  o  m*/

    String logClassName = "";
    if (StringUtils.isNotBlank(logSchemaBody)) {
        LOG.debug("Generating log schema");
        Schema logSchema = new Schema.Parser().parse(logSchemaBody);
        logClassName = LOGGING_NAMESPACE + logSchema.getName();

        String logRecordHeader = readResource(LOG_TEMPLATE_DIR + LOG_RECORD + _H + TEMPLATE_SUFFIX)
                .replaceAll(LOG_RECORD_CLASS_VAR, logClassName);

        objcSources
                .add(CommonSdkUtil.tarEntryForSources(logRecordHeader, KAA_ROOT + LOG_DIR + LOG_RECORD + _H));

        String logRecordSource = readResource(LOG_TEMPLATE_DIR + LOG_RECORD + _M + TEMPLATE_SUFFIX)
                .replaceAll(LOG_RECORD_CLASS_VAR, logClassName);

        objcSources
                .add(CommonSdkUtil.tarEntryForSources(logRecordSource, KAA_ROOT + LOG_DIR + LOG_RECORD + _M));

        String logCollector = readResource(LOG_TEMPLATE_DIR + LOG_COLLECTOR_INTERFACE + TEMPLATE_SUFFIX)
                .replaceAll(LOG_RECORD_CLASS_VAR, logClassName);

        objcSources.add(
                CommonSdkUtil.tarEntryForSources(logCollector, KAA_ROOT + LOG_DIR + LOG_COLLECTOR_INTERFACE));

        String logCollectorImplHeader = readResource(
                LOG_TEMPLATE_DIR + LOG_COLLECTOR_SOURCE + _H + TEMPLATE_SUFFIX).replaceAll(LOG_RECORD_CLASS_VAR,
                        logClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(logCollectorImplHeader,
                KAA_ROOT + LOG_DIR + LOG_COLLECTOR_SOURCE + _H));

        String logCollectorImplSource = readResource(
                LOG_TEMPLATE_DIR + LOG_COLLECTOR_SOURCE + _M + TEMPLATE_SUFFIX).replaceAll(LOG_RECORD_CLASS_VAR,
                        logClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(logCollectorImplSource,
                KAA_ROOT + LOG_DIR + LOG_COLLECTOR_SOURCE + _M));

        objcSources.addAll(generateSourcesFromSchema(logSchema, LOG_GEN, LOGGING_NAMESPACE));
    }

    String configurationClassName = "";
    if (StringUtils.isNotBlank(configurationBaseSchemaBody)) {
        LOG.debug("Generating configuration schema");
        Schema configurationSchema = new Schema.Parser().parse(configurationBaseSchemaBody);
        configurationClassName = CONFIGURATION_NAMESPACE + configurationSchema.getName();

        String configurationCommon = readResource(
                CONFIGURATION_TEMPLATE_DIR + CONFIGURATION_COMMON + TEMPLATE_SUFFIX)
                        .replaceAll(CONFIGURATION_CLASS_VAR, configurationClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(configurationCommon,
                KAA_ROOT + CONFIGURATION_DIR + CONFIGURATION_COMMON));

        String cfManagerImplHeader = readResource(
                CONFIGURATION_TEMPLATE_DIR + CONFIGURATION_MANAGER_IMPL + _H + TEMPLATE_SUFFIX)
                        .replaceAll(CONFIGURATION_CLASS_VAR, configurationClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(cfManagerImplHeader,
                KAA_ROOT + CONFIGURATION_DIR + CONFIGURATION_MANAGER_IMPL + _H));

        String cfManagerImplSource = readResource(
                CONFIGURATION_TEMPLATE_DIR + CONFIGURATION_MANAGER_IMPL + _M + TEMPLATE_SUFFIX)
                        .replaceAll(CONFIGURATION_CLASS_VAR, configurationClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(cfManagerImplSource,
                KAA_ROOT + CONFIGURATION_DIR + CONFIGURATION_MANAGER_IMPL + _M));

        String cfDeserializerHeader = readResource(
                CONFIGURATION_TEMPLATE_DIR + CONFIGURATION_DESERIALIZER + _H + TEMPLATE_SUFFIX)
                        .replaceAll(CONFIGURATION_CLASS_VAR, configurationClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(cfDeserializerHeader,
                KAA_ROOT + CONFIGURATION_DIR + CONFIGURATION_DESERIALIZER + _H));

        String cfDeserializerSource = readResource(
                CONFIGURATION_TEMPLATE_DIR + CONFIGURATION_DESERIALIZER + _M + TEMPLATE_SUFFIX)
                        .replaceAll(CONFIGURATION_CLASS_VAR, configurationClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(cfDeserializerSource,
                KAA_ROOT + CONFIGURATION_DIR + CONFIGURATION_DESERIALIZER + _M));

        objcSources.addAll(
                generateSourcesFromSchema(configurationSchema, CONFIGURATION_GEN, CONFIGURATION_NAMESPACE));
    }

    if (StringUtils.isNotBlank(notificationSchemaBody)) {
        LOG.debug("Generating notification schema");
        Schema notificationSchema = new Schema.Parser().parse(notificationSchemaBody);
        String notificationClassName = NOTIFICATION_NAMESPACE + notificationSchema.getName();

        String nfCommonHeader = readResource(
                NOTIFICATION_TEMPLATE_DIR + NOTIFICATION_COMMON + _H + TEMPLATE_SUFFIX)
                        .replaceAll(NOTIFICATION_CLASS_VAR, notificationClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(nfCommonHeader,
                KAA_ROOT + NOTIFICATION_DIR + NOTIFICATION_COMMON + _H));

        String nfCommonSource = readResource(
                NOTIFICATION_TEMPLATE_DIR + NOTIFICATION_COMMON + _M + TEMPLATE_SUFFIX)
                        .replaceAll(NOTIFICATION_CLASS_VAR, notificationClassName);

        objcSources.add(CommonSdkUtil.tarEntryForSources(nfCommonSource,
                KAA_ROOT + NOTIFICATION_DIR + NOTIFICATION_COMMON + _M));

        objcSources.addAll(
                generateSourcesFromSchema(notificationSchema, NOTIFICATION_GEN, NOTIFICATION_NAMESPACE));
    }

    if (eventFamilies != null && !eventFamilies.isEmpty()) {
        objcSources.addAll(ObjCEventClassesGenerator.generateEventSources(eventFamilies));
    }

    String kaaClient = readResource(SDK_TEMPLATE_DIR + KAA_CLIENT + TEMPLATE_SUFFIX)
            .replaceAll(LOG_RECORD_CLASS_VAR, logClassName)
            .replaceAll(CONFIGURATION_CLASS_VAR, configurationClassName);

    objcSources.add(CommonSdkUtil.tarEntryForSources(kaaClient, KAA_ROOT + KAA_CLIENT));

    String baseKaaClientHeader = readResource(SDK_TEMPLATE_DIR + BASE_KAA_CLIENT + _H + TEMPLATE_SUFFIX)
            .replaceAll(LOG_RECORD_CLASS_VAR, logClassName)
            .replaceAll(CONFIGURATION_CLASS_VAR, configurationClassName);

    objcSources.add(CommonSdkUtil.tarEntryForSources(baseKaaClientHeader, KAA_ROOT + BASE_KAA_CLIENT + _H));
    String baseKaaClientSource = readResource(SDK_TEMPLATE_DIR + BASE_KAA_CLIENT + _M + TEMPLATE_SUFFIX)
            .replaceAll(LOG_RECORD_CLASS_VAR, logClassName)
            .replaceAll(CONFIGURATION_CLASS_VAR, configurationClassName);

    objcSources.add(CommonSdkUtil.tarEntryForSources(baseKaaClientSource, KAA_ROOT + BASE_KAA_CLIENT + _M));

    String tokenVerifier = defaultVerifierToken == null ? "nil" : "@\"" + defaultVerifierToken + "\"";

    String tokenVerifierSource = readResource(EVENT_TEMPLATE_DIR + USER_VERIFIER_CONSTANTS + TEMPLATE_SUFFIX)
            .replaceAll(DEFAULT_USER_VERIFIER_TOKEN_VAR, tokenVerifier);

    objcSources.add(CommonSdkUtil.tarEntryForSources(tokenVerifierSource,
            KAA_ROOT + EVENT_DIR + USER_VERIFIER_CONSTANTS));

    String kaaDefaultsTemplate = readResource(SDK_TEMPLATE_DIR + KAA_DEFAULTS + TEMPLATE_SUFFIX);
    objcSources.add(generateKaaDefaults(kaaDefaultsTemplate, bootstrapNodes, sdkToken,
            configurationProtocolSchemaBody, defaultConfigurationData, sdkProperties));

    for (TarEntryData entryData : objcSources) {
        replacementData.put(entryData.getEntry().getName(), entryData);
    }

    ArchiveEntry archiveEntry;
    while ((archiveEntry = templateArchive.getNextEntry()) != null) {
        if (!archiveEntry.isDirectory()) {
            if (replacementData.containsKey(archiveEntry.getName())) {
                TarEntryData entryData = replacementData.remove(archiveEntry.getName());
                sdkFile.putArchiveEntry(entryData.getEntry());
                sdkFile.write(entryData.getData());
            } else {
                sdkFile.putArchiveEntry(archiveEntry);
                IOUtils.copy(templateArchive, sdkFile);
            }
        } else {
            sdkFile.putArchiveEntry(archiveEntry);
        }
        sdkFile.closeArchiveEntry();
    }

    templateArchive.close();

    for (String entryName : replacementData.keySet()) {
        TarEntryData entryData = replacementData.get(entryName);
        sdkFile.putArchiveEntry(entryData.getEntry());
        sdkFile.write(entryData.getData());
        sdkFile.closeArchiveEntry();
    }

    sdkFile.finish();
    sdkFile.close();

    String sdkFileName = SDK_PREFIX + sdkProperties.getToken() + ".tar.gz";

    byte[] sdkData = sdkOutput.toByteArray();

    FileData sdk = new FileData();
    sdk.setFileName(sdkFileName);
    sdk.setFileData(sdkData);
    return sdk;
}

From source file:org.lenskit.util.io.CompressedByteSource.java

@Override
public InputStream openStream() throws IOException {
    CompressorStreamFactory factory = new CompressorStreamFactory();
    InputStream base = delegate.openStream();
    try {/*from  w  ww.j ava2 s  . c o m*/
        return factory.createCompressorInputStream(compName, base);
    } catch (CompressorException e) {
        throw new IOException("Could not create compressor", e);
    }
}