Example usage for org.springframework.integration.file.remote InputStreamCallback InputStreamCallback

List of usage examples for org.springframework.integration.file.remote InputStreamCallback InputStreamCallback

Introduction

In this page you can find the example usage for org.springframework.integration.file.remote InputStreamCallback InputStreamCallback.

Prototype

InputStreamCallback

Source Link

Usage

From source file:org.springframework.batch.integration.x.RemoteFileToHadoopTasklet.java

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
    final String filePath = chunkContext.getStepContext().getStepExecution().getExecutionContext()
            .getString("filePath");
    Assert.notNull(filePath);/*from  ww w.jav a 2  s  .  c  om*/
    if (logger.isDebugEnabled()) {
        logger.debug("Transferring " + filePath + " to HDFS");
    }
    boolean result = this.template.get(filePath, new InputStreamCallback() {

        @Override
        public void doWithInputStream(InputStream stream) throws IOException {
            OutputStreamWriter writer = new OutputStreamWriter(configuration,
                    new Path(hdfsDirectory + filePath), null);
            byte[] buff = new byte[1024];
            int len;
            while ((len = stream.read(buff)) > 0) {
                if (len == buff.length) {
                    writer.write(buff);
                } else {
                    writer.write(Arrays.copyOf(buff, len));
                }
            }
            writer.close();
        }

    });
    if (!result) {
        throw new MessagingException("Error during file transfer");
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Transferred " + filePath + " to HDFS");
        }
        return RepeatStatus.FINISHED;
    }
}