List of usage examples for java.nio.channels FileChannel size
public abstract long size() throws IOException;
From source file:nz.govt.natlib.ndha.common.FileUtils.java
/** * Name is self explanatory/* w w w . j ava2 s . com*/ * * @param from * @param to * @throws IOException */ public static void copyFile(File from, File to) throws IOException { FileChannel inChannel = new FileInputStream(from).getChannel(); FileChannel outChannel = new FileOutputStream(to).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } catch (IOException e) { throw e; } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } }
From source file:org.wso2.bam.integration.tests.cassandra.KeySpaceNameChangeTestCase.java
private static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile();//from ww w .j a v a2s . c o m } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); long count = 0; long size = source.size(); while ((count += destination.transferFrom(source, count, size - count)) < size) { } } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } }
From source file:org.totschnig.myexpenses.Utils.java
static boolean copy(File src, File dst) { FileChannel srcC; try {// w ww .jav a 2 s . c om srcC = new FileInputStream(src).getChannel(); FileChannel dstC = new FileOutputStream(dst).getChannel(); dstC.transferFrom(srcC, 0, srcC.size()); srcC.close(); dstC.close(); return true; } catch (FileNotFoundException e) { Log.e("MyExpenses", e.getLocalizedMessage()); } catch (IOException e) { Log.e("MyExpenses", e.getLocalizedMessage()); } return false; }
From source file:Main.java
public static File copyFile(File destinationFile, File sourceFile) throws IOException { FileInputStream inputStream = null; FileChannel inputChannel = null; FileOutputStream outputStream = null; FileChannel outputChannel = null; try {// w w w . j a v a2 s.co m inputStream = new FileInputStream(sourceFile); inputChannel = inputStream.getChannel(); outputStream = new FileOutputStream(destinationFile); outputChannel = outputStream.getChannel(); inputChannel.transferTo(0, inputChannel.size(), outputChannel); return destinationFile; } catch (IOException exception) { throw exception; } finally { if (inputChannel != null) { inputChannel.close(); } if (inputStream != null) { inputStream.close(); } if (outputChannel != null) { outputChannel.close(); } if (outputStream != null) { outputStream.close(); } } }
From source file:yui.classes.utils.IOUtils.java
/** * * good for Large Files >2Mb/* ww w . ja v a 2 s . com*/ * @param filename * @return */ private static byte[] largeFileReader(String filename) { byte[] bytes = null; FileChannel fc = null; try { fc = new FileInputStream(filename).getChannel(); MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); int size = byteBuffer.capacity(); if (size > 0) { // Retrieve all bytes in the buffer byteBuffer.clear(); bytes = new byte[size]; byteBuffer.get(bytes, 0, bytes.length); } fc.close(); } catch (FileNotFoundException fnf) { System.err.println("" + fnf); } catch (IOException io) { System.err.println("" + io); } finally { if (fc != null) { try { fc.close(); } catch (IOException ioe) { // ignore } } } return bytes; }
From source file:io.undertow.server.handlers.SenderTestCase.java
@BeforeClass public static void setup() { HttpHandler lotsOfSendsHandler = new HttpHandler() { @Override/*from w w w . jav a 2 s .c o m*/ public void handleRequest(final HttpServerExchange exchange) throws Exception { boolean blocking = exchange.getQueryParameters().get("blocking").getFirst().equals("true"); if (blocking) { if (exchange.isInIoThread()) { exchange.startBlocking(); exchange.dispatch(this); return; } } final Sender sender = exchange.getResponseSender(); class SendClass implements Runnable, IoCallback { int sent = 0; @Override public void run() { sent++; sender.send("a", this); } @Override public void onComplete(final HttpServerExchange exchange, final Sender sender) { if (sent++ == SENDS) { sender.close(); return; } sender.send("a", this); } @Override public void onException(final HttpServerExchange exchange, final Sender sender, final IOException exception) { exception.printStackTrace(); exchange.endExchange(); } } new SendClass().run(); } }; HttpHandler lotsOfTransferHandler = new HttpHandler() { @Override public void handleRequest(final HttpServerExchange exchange) throws Exception { boolean blocking = exchange.getQueryParameters().get("blocking").getFirst().equals("true"); if (blocking) { if (exchange.isInIoThread()) { exchange.startBlocking(); exchange.dispatch(this); return; } } URI uri = SenderTestCase.class.getResource(SenderTestCase.class.getSimpleName() + ".class").toURI(); Path file = Paths.get(uri); final FileChannel channel = FileChannel.open(file, StandardOpenOption.READ); exchange.setResponseContentLength(channel.size() * TXS); final Sender sender = exchange.getResponseSender(); class SendClass implements Runnable, IoCallback { int sent = 0; @Override public void run() { sent++; try { channel.position(0); } catch (IOException e) { } sender.transferFrom(channel, this); } @Override public void onComplete(final HttpServerExchange exchange, final Sender sender) { if (sent++ == TXS) { sender.close(); return; } try { channel.position(0); } catch (IOException e) { } sender.transferFrom(channel, this); } @Override public void onException(final HttpServerExchange exchange, final Sender sender, final IOException exception) { exception.printStackTrace(); exchange.endExchange(); } } new SendClass().run(); } }; final HttpHandler fixedLengthSender = new HttpHandler() { @Override public void handleRequest(final HttpServerExchange exchange) throws Exception { exchange.getResponseSender().send(HELLO_WORLD); } }; PathHandler handler = new PathHandler().addPrefixPath("/lots", lotsOfSendsHandler) .addPrefixPath("/fixed", fixedLengthSender).addPrefixPath("/transfer", lotsOfTransferHandler); DefaultServer.setRootHandler(handler); }
From source file:org.gnucash.android.export.ExporterTask.java
/** * Copies a file from <code>src</code> to <code>dst</code> * @param src Absolute path to the source file * @param dst Absolute path to the destination file * @throws IOException if the file could not be copied *///ww w. j a v a2s . c om public static void copyFile(File src, File dst) throws IOException { //TODO: Make this asynchronous at some time, t in the future. FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dst).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } }
From source file:com.rvl.android.getnzb.LocalNZB.java
public static String readFile(File file) throws IOException { Log.d(Tags.LOG, "readfile(): Converting file to string. (" + file.getAbsolutePath() + ")"); FileInputStream stream = new FileInputStream(file); try {/*w w w.ja v a 2 s .c o m*/ FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); /* Instead of using default, pass in a decoder. */ return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } }
From source file:org.eclipse.smila.binarystorage.persistence.io.BssIOUtils.java
/** * Copies data from input stream into persistence location, by using channels. * //from ww w. j av a 2s .c o m * @param path * @param stream * @throws BinaryStorageException */ private static void writeCopyByChannels(final String path, final InputStream stream) throws BinaryStorageException { FileChannel inChannel = null; FileChannel outChannel = null; try { inChannel = ((FileInputStream) stream).getChannel(); outChannel = new FileOutputStream(path).getChannel(); if (inChannel.size() < FILE_SIZE_64MB) { outChannel.transferFrom(inChannel, 0, inChannel.size()); } else { // the transferTo() does not transfer files > than 2^31-1 bytes final long size = inChannel.size(); long position = 0; while (position < size) { position += inChannel.transferTo(position, FILE_SIZE_TRANSFER, outChannel); } } } catch (final IOException ioe) { throw new BinaryStorageException(ioe, "Could not write binary record to :" + path); } finally { // Close the channels closeChannel(inChannel); closeChannel(outChannel); } }
From source file:org.hawk.service.api.utils.APIUtils.java
public static File convertJavaFileToThriftFile(java.io.File rawFile) throws FileNotFoundException, IOException { try (FileInputStream fIS = new FileInputStream(rawFile)) { FileChannel chan = fIS.getChannel(); /* Note: this cast limits us to 2GB files - this shouldn't be a problem, but if it were we could use FileChannel#map and call Hawk.Client#registerModels one file at a time. */ ByteBuffer buf = ByteBuffer.allocate((int) chan.size()); chan.read(buf);/*w w w. j a va 2 s. com*/ buf.flip(); File mmFile = new File(); mmFile.name = rawFile.getName(); mmFile.contents = buf; return mmFile; } }