List of usage examples for java.nio.channels FileChannel force
public abstract void force(boolean metaData) throws IOException;
From source file:org.paxle.crawler.fs.impl.FsCrawler.java
private File copyChanneled(final File file, final ICrawlerDocument cdoc, final boolean useFsync) { logger.info(String.format("Copying '%s' using the copy mechanism of the OS%s", file, (useFsync) ? " with fsync" : "")); final ITempFileManager tfm = this.contextLocal.getCurrentContext().getTempFileManager(); if (tfm == null) { cdoc.setStatus(ICrawlerDocument.Status.UNKNOWN_FAILURE, "Cannot access ITempFileMananger from " + Thread.currentThread().getName()); return null; }/*w ww.j a v a 2s . c o m*/ FileInputStream fis = null; FileOutputStream fos = null; File out = null; try { out = tfm.createTempFile(); fis = new FileInputStream(file); fos = new FileOutputStream(out); final FileChannel in_fc = fis.getChannel(); final FileChannel out_fc = fos.getChannel(); long txed = 0L; while (txed < in_fc.size()) txed += in_fc.transferTo(txed, in_fc.size() - txed, out_fc); if (useFsync) out_fc.force(false); out_fc.close(); try { detectFormats(cdoc, fis); } catch (IOException ee) { logger.warn( String.format("Error detecting format of '%s': %s", cdoc.getLocation(), ee.getMessage())); } } catch (IOException e) { logger.error(String.format("Error copying '%s' to '%s': %s", cdoc.getLocation(), out, e.getMessage()), e); cdoc.setStatus(ICrawlerDocument.Status.UNKNOWN_FAILURE, e.getMessage()); } finally { if (fis != null) try { fis.close(); } catch (IOException e) { /* ignore */} if (fos != null) try { fos.close(); } catch (IOException e) { /* ignore */} } return out; }
From source file:org.solmix.commons.util.Files.java
/** * ?//w w w.j a v a2s .co m * * @param f1 * ? * @param f2 * * @throws Exception */ public static void copyFile(File f1, File f2) throws Exception { int length = 2097152; FileInputStream in = new FileInputStream(f1); FileOutputStream out = new FileOutputStream(f2); FileChannel inC = in.getChannel(); FileChannel outC = out.getChannel(); ByteBuffer b = null; while (true) { if (inC.position() == inC.size()) { inC.close(); outC.close(); } if ((inC.size() - inC.position()) < length) { length = (int) (inC.size() - inC.position()); } else length = 2097152; b = ByteBuffer.allocateDirect(length); inC.read(b); b.flip(); outC.write(b); outC.force(false); } }
From source file:org.springframework.batch.item.xml.StaxEventItemWriter.java
/** * Helper method for opening output source at given file position *///from w ww.j a v a 2 s. c om private void open(long position) { File file; FileOutputStream os = null; FileChannel fileChannel = null; try { file = resource.getFile(); FileUtils.setUpOutputFile(file, restarted, false, overwriteOutput); Assert.state(resource.exists(), "Output resource must exist"); os = new FileOutputStream(file, true); fileChannel = os.getChannel(); channel = os.getChannel(); setPosition(position); } catch (IOException ioe) { throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource + "]", ioe); } XMLOutputFactory outputFactory = createXmlOutputFactory(); if (outputFactory.isPropertySupported("com.ctc.wstx.automaticEndElements")) { // If the current XMLOutputFactory implementation is supplied by // Woodstox >= 3.2.9 we want to disable its // automatic end element feature (see: // http://jira.codehaus.org/browse/WSTX-165) per // http://jira.spring.io/browse/BATCH-761). outputFactory.setProperty("com.ctc.wstx.automaticEndElements", Boolean.FALSE); } if (outputFactory.isPropertySupported("com.ctc.wstx.outputValidateStructure")) { // On restart we don't write the root element so we have to disable // structural validation (see: // http://jira.spring.io/browse/BATCH-1681). outputFactory.setProperty("com.ctc.wstx.outputValidateStructure", Boolean.FALSE); } try { final FileChannel channel = fileChannel; if (transactional) { TransactionAwareBufferedWriter writer = new TransactionAwareBufferedWriter(channel, new Runnable() { @Override public void run() { closeStream(); } }); writer.setEncoding(encoding); writer.setForceSync(forceSync); bufferedWriter = writer; } else { bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, encoding)); } delegateEventWriter = createXmlEventWriter(outputFactory, bufferedWriter); eventWriter = new NoStartEndDocumentStreamWriter(delegateEventWriter); initNamespaceContext(delegateEventWriter); if (!restarted) { startDocument(delegateEventWriter); if (forceSync) { channel.force(false); } } } catch (XMLStreamException xse) { throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource + "]", xse); } catch (UnsupportedEncodingException e) { throw new DataAccessResourceFailureException( "Unable to write to file resource: [" + resource + "] with encoding=[" + encoding + "]", e); } catch (IOException e) { throw new DataAccessResourceFailureException("Unable to write to file resource: [" + resource + "]", e); } }
From source file:voldemort.store.cachestore.impl.ChannelStore.java
private void forceFlush(FileChannel channel) { try {/*from w ww.ja va 2s.c o m*/ channel.force(false); } catch (IOException e) { //swallow exception logger.error(e.getMessage(), e); } }