File producer should support a flush attribute
----------------------------------------------

                 Key: CAMEL-5136
                 URL: https://issues.apache.org/jira/browse/CAMEL-5136
             Project: Camel
          Issue Type: Improvement
          Components: camel-core
    Affects Versions: 2.9.1, 2.8.4
            Reporter: Jean-Baptiste Onofré
            Assignee: Jean-Baptiste Onofré
             Fix For: 2.8.5, 2.10.0, 2.9.2


Currently, the file operation doesn't flush the file change while writing to 
the filesystem:

    private void writeFileByFile(File source, File target) throws IOException {
        FileChannel in = new FileInputStream(source).getChannel();
        FileChannel out = null;
        try {
            out = prepareOutputFileChannel(target, out);
            LOG.trace("Using FileChannel to transfer from: {} to: {}", in, out);
            long size = in.size();
            long position = 0;
            while (position < size) {
                position += in.transferTo(position, endpoint.getBufferSize(), 
out);
            }
        } finally {
            IOHelper.close(in, source.getName(), LOG);
            IOHelper.close(out, target.getName(), LOG);
        }
    }

    private void writeFileByStream(InputStream in, File target) throws 
IOException {
        FileChannel out = null;
        try {
            out = prepareOutputFileChannel(target, out);
            LOG.trace("Using InputStream to transfer from: {} to: {}", in, out);
            int size = endpoint.getBufferSize();
            byte[] buffer = new byte[size];
            ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
            int bytesRead;
            while ((bytesRead = in.read(buffer)) != -1) {
                if (bytesRead < size) {
                    byteBuffer.limit(bytesRead);
                }
                out.write(byteBuffer);
                byteBuffer.clear();
            }
        } finally {
            IOHelper.close(in, target.getName(), LOG);
            IOHelper.close(out, target.getName(), LOG);
        }
    }

It means that the file can be visible on the filesystem quite a long time after 
starting to send the exchanges to the file endpoint.

It could be interesting to add a flush attribute doing:

    FileOutputStream os = ...
    FileDescriptor fd = os.getFD();
    ...
    os.write(data);
    os.flush();
    fd.sync();



--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: 
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira


Reply via email to