private void deployResourcesInternal()

in java/main/src/main/java/com/epam/deltix/utilities/ResourceLoader.java [665:736]


    private void deployResourcesInternal(final Path deploymentPath) throws IOException {
        final byte[] inputData = this.getInputBuffer();
        byte[] decompressedData = _outputBuffer;

        // Sort for deployment
        Collections.sort(_resources, new Comparator<Resource>() {
            @Override
            public int compare(final Resource o1, final Resource o2) {
                /* non-null(already existing files) before null */
                return null != o1._fileLock && null == o2._fileLock ? -1 :
                        null == o1._fileLock && null != o2._fileLock ? 1 :
                                /* otherwise, descending order by size */
                                Integer.compare(o2.length, o1.length);
            }
        });

        for (final Resource resource : _resources) {
            // If partial reuse is allowed and we already locked some files for read, do not deploy them
            if (null != resource.getFile())
                continue;

            byte[] outputData = inputData;

            final String resourcePath = _resourcesRoot + resource.source;
            log("Reading %s", resourcePath);
            final int resourceLength = resource.length;
            int outputLength = resourceLength;

            try (final InputStream in = _class.getClassLoader().getResourceAsStream(resourcePath)) {
                if (in == null)
                    throw new IOException("Can't open resource '" + resource.source + "'.");
                // Note: the buffer may be bigger than the current file we are processing
                readResourceFile(in, inputData, resourceLength);
            }

            if (resource.isZstd) {
                final long len = ZstdDecompressor.getDecompressedSize(inputData, 0, resourceLength);
                if (len > Integer.MAX_VALUE)
                    throw new RuntimeException(fmt("Decompressed file size is too big: %s for %s", outputLength, resourcePath));

                outputLength = (int) len;
                if (null == decompressedData || outputLength > decompressedData.length)
                    decompressedData = _outputBuffer = new byte[outputLength];

                outputData = decompressedData;
                final ZstdDecompressor dec = new ZstdDecompressor();
                // TODO: May use ByteBuffer interface later
                dec.decompress(inputData, 0, resourceLength, decompressedData, 0, outputLength);
            }

            lockFileWatchdogUpdate();

            final Path filePath = resource.getFullPath(deploymentPath);
            if (resource.destinationPathNullable != null)
                Files.createDirectories(deploymentPath.resolve(resource.destinationPathNullable));

            log("Writing %s", filePath);
            FileChannel out = null;
            try (final FileLock lock = openLockedFileChannel(filePath, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
                out = lock.channel();
                writeResourceFile(out, outputData, outputLength);
                out.truncate(outputLength);
                log("Done writing %s, closing", filePath);
            }

            if (null != out)
                out.close();

            log("After writing %s, taking read lock", filePath);
            resource.setReadLock(filePath);
        }
    }