public void downloadChunk()

in transport/local-transport/src/main/java/org/apache/airavata/mft/transport/local/LocalIncomingChunkedConnector.java [61:97]


    public void downloadChunk(int chunkId, long startByte, long endByte, String downloadFile) throws Exception {

        logger.info("Downloading chunk {} with start byte {} and end byte {} to file {} from resource path {}",
                chunkId, startByte, endByte, downloadFile, this.resourcePath);

        if (dmaFlag) {
            if (resourceSize <= endByte - startByte) {
                Files.copy(Path.of(this.resourcePath), Path.of(downloadFile));
            } else {
                try (FileInputStream from = new FileInputStream(this.resourcePath);
                     FileOutputStream to = new FileOutputStream(downloadFile)) {
                    from.getChannel().transferTo(startByte, endByte - startByte, to.getChannel());
                } catch (Exception e) {
                    logger.error("Unexpected error occurred while downloading chunk {} to file {} from resource path {}",
                            chunkId, downloadFile, this.resourcePath, e);
                    throw e;
                }
            }
        }   else {
            try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(this.resourcePath), buffLen);
                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(downloadFile))) {
                byte[] buffer = new byte[buffLen];
                int read = 0;
                long totalRead = bis.skip(startByte);
                while ((read = bis.read(buffer, 0, Math.min(buffLen, (int) (endByte - totalRead)))) > 0) {
                    bos.write(buffer, 0, read);
                    totalRead += read;
                }
                bis.close();
                bos.close();
            } catch (Exception e) {
                logger.error("Unexpected error occurred while downloading chunk {} to file {} from resource path {}",
                        chunkId, downloadFile, this.resourcePath, e);
                throw e;
            }
        }
    }