List of usage examples for java.nio.channels FileChannel close
public final void close() throws IOException
From source file:Main.java
public static Bitmap convertToMutable(Bitmap srcBitmap, String cacheDirPath, String tempFileName) { try {//from ww w .j av a 2 s . c o m // this is the file going to use temporally to save the bytes. // This file will not be a image, it will store the raw image data. int index = tempFileName.lastIndexOf("."); if (index != -1) tempFileName = tempFileName.substring(0, index); File file = new File(cacheDirPath + File.separator + tempFileName + ".tmp"); // Open an RandomAccessFile // Make sure you have added uses-permission // android:name="android.permission.WRITE_EXTERNAL_STORAGE" // into AndroidManifest.xml file RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); // get the width and height of the source bitmap. int width = srcBitmap.getWidth(); int height = srcBitmap.getHeight(); Config type = srcBitmap.getConfig(); // Copy the byte to the file // Assume source bitmap loaded using options.inPreferredConfig = // Config.ARGB_8888; FileChannel channel = randomAccessFile.getChannel(); MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, srcBitmap.getRowBytes() * height); srcBitmap.copyPixelsToBuffer(map); // recycle the source bitmap, this will be no longer used. srcBitmap.recycle(); System.gc();// try to force the bytes from the imgIn to be released // Create a new bitmap to load the bitmap again. Probably the memory // will be available. srcBitmap = Bitmap.createBitmap(width, height, type); map.position(0); // load it back from temporary srcBitmap.copyPixelsFromBuffer(map); // close the temporary file and channel , then delete that also channel.close(); randomAccessFile.close(); // delete the temp file file.delete(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return srcBitmap; }
From source file:es.uvigo.darwin.jmodeltest.io.HtmlReporter.java
@SuppressWarnings("resource") public static void copyFile(File in, File out) throws IOException { FileChannel inChannel = new FileInputStream(in).getChannel(); FileChannel outChannel = new FileOutputStream(out).getChannel(); try {/* w w w . ja v a2 s.c o m*/ 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:de.ingrid.portal.portlets.admin.AdminPortalProfilePortlet.java
/** * Copy Files in file system.//w w w . j a v a 2 s. c o m * * @param source * @param dest * @throws IOException */ private static void copy(File source, File dest) throws IOException { FileChannel in = null, out = null; try { in = new FileInputStream(source).getChannel(); out = new FileOutputStream(dest).getChannel(); in = new FileInputStream(source).getChannel(); out = new FileOutputStream(dest).getChannel(); out.transferFrom(in, 0, in.size()); } catch (Exception e) { log.error("Error copy files ('" + source.getAbsolutePath() + "' -> '" + dest.getAbsolutePath() + "')", e); } finally { if (in != null) in.close(); if (out != null) out.close(); } }
From source file:com.liferay.util.FileUtil.java
public static void copyFile(File source, File destination, boolean hardLinks) { if (!source.exists()) { return;//w w w .ja v a 2 s . com } if (hardLinks && !Config.getBooleanProperty("CONTENT_VERSION_HARD_LINK", true)) { hardLinks = false; } if ((destination.getParentFile() != null) && (!destination.getParentFile().exists())) { destination.getParentFile().mkdirs(); } if (hardLinks) { // I think we need to be sure to unlink first if (destination.exists()) { JNALibrary.unlink(destination.getAbsolutePath()); } try { JNALibrary.link(source.getAbsolutePath(), destination.getAbsolutePath()); // setting this means we will try again if we cannot hard link if (!destination.exists()) { hardLinks = false; } } catch (IOException e) { Logger.error(FileUtil.class, "Can't create hardLink. source: " + source.getAbsolutePath() + ", destination: " + destination.getAbsolutePath()); // setting this means we will try again if we cannot hard link hardLinks = false; } } if (!hardLinks) { try { FileChannel srcChannel = new FileInputStream(source).getChannel(); FileChannel dstChannel = new FileOutputStream(destination).getChannel(); dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); srcChannel.close(); dstChannel.close(); } catch (IOException ioe) { Logger.error(FileUtil.class, ioe.getMessage(), ioe); } } }
From source file:log4JToXml.xmlToProperties.XmlToLog4jConverterImpl.java
private static void copyFile(File sourceFile, File destFile) throws IOException { FileChannel source = null; FileChannel destination = null; try {/* w w w . j a va 2 s .com*/ source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } }
From source file:gov.nasa.ensemble.common.io.FileUtilities.java
/** * Make a copy of a file on the filesystem (platform independent). * /*w w w .j a va 2 s . c o m*/ * @param source * the file to copy. * @param dest * the copy to make. * @return whether the file copy exists on the filesystem upon completion. * @throws IOException */ public static boolean copyFile(File source, File dest) throws IOException { FileInputStream sourceStream = new FileInputStream(source); FileChannel sourceChannel = sourceStream.getChannel(); FileOutputStream destStream = new FileOutputStream(dest); FileChannel destChannel = destStream.getChannel(); try { sourceChannel.transferTo(0, sourceChannel.size(), destChannel); } catch (IOException ex) { if (ex.getCause() instanceof OutOfMemoryError) { IOUtils.copy(sourceStream, destStream); } } finally { destChannel.close(); IOUtils.closeQuietly(destStream); sourceChannel.close(); IOUtils.closeQuietly(sourceStream); } return dest.exists(); }
From source file:it.geosolutions.tools.io.file.Copy.java
/** * Copy the input file onto the output file using the specified buffer size. * /*from ww w . j a va 2s . c om*/ * @param sourceFile * the {@link File} to copy from. * @param destinationFile * the {@link File} to copy to. * @param size * buffer size. * @throws IOException * in case something bad happens. */ public static void copyFile(File sourceFile, File destinationFile, int size) throws IOException { Objects.notNull(sourceFile, destinationFile); if (!sourceFile.exists() || !sourceFile.canRead() || !sourceFile.isFile()) throw new IllegalStateException("Source is not in a legal state."); if (!destinationFile.exists()) { destinationFile.createNewFile(); } if (destinationFile.getAbsolutePath().equalsIgnoreCase(sourceFile.getAbsolutePath())) throw new IllegalArgumentException("Cannot copy a file on itself"); RandomAccessFile s = null, d = null; FileChannel source = null; FileChannel destination = null; try { s = new RandomAccessFile(sourceFile, "r"); source = s.getChannel(); d = new RandomAccessFile(destinationFile, "rw"); destination = d.getChannel(); IOUtils.copyFileChannel(size, source, destination); } finally { if (source != null) { try { source.close(); } catch (Throwable t) { if (LOGGER.isInfoEnabled()) LOGGER.info(t.getLocalizedMessage(), t); } } if (s != null) { try { s.close(); } catch (Throwable t) { if (LOGGER.isInfoEnabled()) LOGGER.info(t.getLocalizedMessage(), t); } } if (destination != null) { try { destination.close(); } catch (Throwable t) { if (LOGGER.isInfoEnabled()) LOGGER.info(t.getLocalizedMessage(), t); } } if (d != null) { try { d.close(); } catch (Throwable t) { if (LOGGER.isInfoEnabled()) LOGGER.info(t.getLocalizedMessage(), t); } } } }
From source file:Main.java
public static void copyInputStreamToFile(InputStream inputStream, File file) throws IOException { FileOutputStream fileOutputStream = null; FileChannel fileChannel = null; try {/*from ww w .j a v a2s. co m*/ fileOutputStream = new FileOutputStream(file); fileChannel = fileOutputStream.getChannel(); byte[] bArr = new byte[4096]; while (true) { int read = inputStream.read(bArr); if (read <= 0) { break; } fileChannel.write(ByteBuffer.wrap(bArr, SYSTEM_ROOT_STATE_DISABLE, read)); } } catch (Throwable throwable) { throwable.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } if (fileChannel != null) { try { fileChannel.close(); } catch (Exception e2) { e2.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (Exception e22) { e22.printStackTrace(); } } } }
From source file:org.mule.util.FileUtils.java
/** * Try to move a file by renaming with backup attempt by copying/deleting via NIO. * Creates intermidiate directories as required. *//*from w ww . j a va 2 s . com*/ public static boolean moveFileWithCopyFallback(File sourceFile, File destinationFile) { // try fast file-system-level move/rename first boolean success = sourceFile.renameTo(destinationFile); if (!success) { // try again using NIO copy FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(sourceFile); if (!destinationFile.exists()) { FileUtils.createFile(destinationFile.getPath()); } fos = new FileOutputStream(destinationFile); FileChannel srcChannel = fis.getChannel(); FileChannel dstChannel = fos.getChannel(); dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); srcChannel.close(); dstChannel.close(); success = sourceFile.delete(); } catch (IOException ioex) { // grr! success = false; } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(fos); } } return success; }
From source file:eu.nubomedia.nubomedia_kurento_health_communicator_android.kc_and_communicator.util.FileUtils.java
public static void copyFile(File src, File dst) throws IOException { FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dst).getChannel(); try {//from w w w .j ava 2s.c o m inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } }