List of usage examples for java.nio.channels FileChannel close
public final void close() throws IOException
From source file:edu.ucsb.eucalyptus.storage.fs.FileSystemStorageManager.java
public void copyObject(String sourceBucket, String sourceObject, String destinationBucket, String destinationObject) throws IOException { File oldObjectFile = new File( rootDirectory + FILE_SEPARATOR + sourceBucket + FILE_SEPARATOR + sourceObject); File newObjectFile = new File( rootDirectory + FILE_SEPARATOR + destinationBucket + FILE_SEPARATOR + destinationObject); if (!oldObjectFile.equals(newObjectFile)) { FileInputStream fileInputStream = null; FileChannel fileIn = null; FileOutputStream fileOutputStream = null; FileChannel fileOut = null; try {//from www . j av a 2s . c o m fileInputStream = new FileInputStream(oldObjectFile); fileIn = fileInputStream.getChannel(); fileOutputStream = new FileOutputStream(newObjectFile); fileOut = fileOutputStream.getChannel(); fileIn.transferTo(0, fileIn.size(), fileOut); } finally { if (fileIn != null) fileIn.close(); if (fileInputStream != null) fileInputStream.close(); if (fileOut != null) fileOut.close(); if (fileOutputStream != null) fileOutputStream.close(); } } }
From source file:com.hipu.bdb.util.FileUtils.java
/** * Copy up to extent bytes of the source file to the destination */*from www .j a va 2s . co m*/ * @param src * @param dest * @param extent Maximum number of bytes to copy * @param overwrite If target file already exits, and this parameter is * true, overwrite target file (We do this by first deleting the target * file before we begin the copy). * @return True if the extent was greater than actual bytes copied. * @throws FileNotFoundException * @throws IOException */ public static boolean copyFile(final File src, final File dest, long extent, final boolean overwrite) throws FileNotFoundException, IOException { boolean result = false; if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Copying file " + src + " to " + dest + " extent " + extent + " exists " + dest.exists()); } if (dest.exists()) { if (overwrite) { dest.delete(); LOGGER.finer(dest.getAbsolutePath() + " removed before copy."); } else { // Already in place and we're not to overwrite. Return. return result; } } FileInputStream fis = null; FileOutputStream fos = null; FileChannel fcin = null; FileChannel fcout = null; try { // Get channels fis = new FileInputStream(src); fos = new FileOutputStream(dest); fcin = fis.getChannel(); fcout = fos.getChannel(); if (extent < 0) { extent = fcin.size(); } // Do the file copy long trans = fcin.transferTo(0, extent, fcout); if (trans < extent) { result = false; } result = true; } catch (IOException e) { // Add more info to the exception. Preserve old stacktrace. // We get 'Invalid argument' on some file copies. See // http://intellij.net/forums/thread.jsp?forum=13&thread=63027&message=853123 // for related issue. String message = "Copying " + src.getAbsolutePath() + " to " + dest.getAbsolutePath() + " with extent " + extent + " got IOE: " + e.getMessage(); if ((e instanceof ClosedByInterruptException) || ((e.getMessage() != null) && e.getMessage().equals("Invalid argument"))) { LOGGER.severe("Failed copy, trying workaround: " + message); workaroundCopyFile(src, dest); } else { IOException newE = new IOException(message); newE.initCause(e); throw newE; } } finally { // finish up if (fcin != null) { fcin.close(); } if (fcout != null) { fcout.close(); } if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } } return result; }
From source file:com.owncloud.android.oc_framework.network.webdav.FileRequestEntity.java
@Override public void writeRequest(final OutputStream out) throws IOException { //byte[] tmp = new byte[4096]; ByteBuffer tmp = ByteBuffer.allocate(4096); int readResult = 0; // TODO(bprzybylski): each mem allocation can throw OutOfMemoryError we need to handle it // globally in some fashionable manner RandomAccessFile raf = new RandomAccessFile(mFile, "r"); FileChannel channel = raf.getChannel(); Iterator<OnDatatransferProgressListener> it = null; long transferred = 0; long size = mFile.length(); if (size == 0) size = -1;//w ww .j a va 2 s. c o m try { while ((readResult = channel.read(tmp)) >= 0) { out.write(tmp.array(), 0, readResult); tmp.clear(); transferred += readResult; synchronized (mDataTransferListeners) { it = mDataTransferListeners.iterator(); while (it.hasNext()) { it.next().onTransferProgress(readResult, transferred, size, mFile.getName()); } } } } catch (IOException io) { Log.e("FileRequestException", io.getMessage()); throw new RuntimeException( "Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io); } finally { channel.close(); raf.close(); } }
From source file:eu.alefzero.webdav.FileRequestEntity.java
@Override public void writeRequest(final OutputStream out) throws IOException { //byte[] tmp = new byte[4096]; ByteBuffer tmp = ByteBuffer.allocate(4096); int readResult = 0; // TODO(bprzybylski): each mem allocation can throw OutOfMemoryError we need to handle it // globally in some fashionable manner RandomAccessFile raf = new RandomAccessFile(mFile, "r"); FileChannel channel = raf.getChannel(); Iterator<OnDatatransferProgressListener> it = null; long transferred = 0; long size = mFile.length(); if (size == 0) size = -1;/*from w w w. j a v a 2s . c om*/ try { while ((readResult = channel.read(tmp)) >= 0) { out.write(tmp.array(), 0, readResult); tmp.clear(); transferred += readResult; synchronized (mDataTransferListeners) { it = mDataTransferListeners.iterator(); while (it.hasNext()) { it.next().onTransferProgress(readResult, transferred, size, mFile.getName()); } } } } catch (IOException io) { Log_OC.e("FileRequestException", io.getMessage()); throw new RuntimeException( "Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io); } finally { channel.close(); raf.close(); } }
From source file:eu.planets_project.tb.impl.data.util.DataHandlerImpl.java
public File copyLocalFileAsTempFileInExternallyAccessableDir(String localFileRef) throws IOException { //get a temporary file log.debug("copyLocalFileAsTempFileInExternallyAccessableDir"); File f = this.createTempFileInExternallyAccessableDir(); //copying one file to another with FileChannel //@see http://www.exampledepot.com/egs/java.nio/File2File.html FileChannel srcChannel = new FileInputStream(localFileRef).getChannel(); FileChannel dstChannel = new FileOutputStream(f).getChannel(); log.debug("before transferring via FileChannel from src-inputStream: " + localFileRef + " to dest-outputStream: " + f.getAbsolutePath()); // Copy file contents from source to destination dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); //Close the channels srcChannel.close(); dstChannel.close();//from w w w . j av a 2s . c o m log.debug("copyLocalFileAsTempFileInExternallyAccessableDir returning: " + f.getAbsolutePath()); return f; }
From source file:com.owncloud.android.lib.common.network.FileRequestEntity.java
@Override public void writeRequest(final OutputStream out) throws IOException { //byte[] tmp = new byte[4096]; ByteBuffer tmp = ByteBuffer.allocate(4096); int readResult = 0; // globally in some fashionable manner RandomAccessFile raf = new RandomAccessFile(mFile, "r"); FileChannel channel = raf.getChannel(); Iterator<OnDatatransferProgressListener> it = null; long transferred = 0; long size = mFile.length(); if (size == 0) size = -1;/* ww w. j a va2 s .com*/ try { while ((readResult = channel.read(tmp)) >= 0) { out.write(tmp.array(), 0, readResult); tmp.clear(); transferred += readResult; synchronized (mDataTransferListeners) { it = mDataTransferListeners.iterator(); while (it.hasNext()) { it.next().onTransferProgress(readResult, transferred, size, mFile.getAbsolutePath()); } } } } catch (IOException io) { Log_OC.e("FileRequestException", io.getMessage()); throw new RuntimeException( "Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io); } finally { channel.close(); raf.close(); } }
From source file:edu.cmu.graphchi.shards.QueryShard.java
public QueryShard(String fileName, int shardNum, int numShards, VertexInterval interval, Config dbConfig) throws IOException { this.shardNum = shardNum; this.interval = interval; pinIndexToMemory = dbConfig.getBoolean("queryshard.pinindex"); queryCacheSize = dbConfig.getInt("queryshard.cachesize"); adjFile = new File(ChiFilenames.getFilenameShardsAdj(fileName, shardNum, numShards)); numEdges = (int) (adjFile.length() / BYTES_PER_EDGE); FileChannel channel = new java.io.RandomAccessFile(adjFile, "rw").getChannel(); adjBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, adjFile.length()).asLongBuffer(); channel.close(); index = (pinIndexToMemory ? null : createSparseIndex()); loadPointers();// ww w. j a v a 2 s.co m loadInEdgeStartBuffer(); }
From source file:com.cerema.cloud2.lib.common.network.FileRequestEntity.java
@Override public void writeRequest(final OutputStream out) throws IOException { //byte[] tmp = new byte[4096]; ByteBuffer tmp = ByteBuffer.allocate(4096); int readResult = 0; // TODO(bprzybylski): each mem allocation can throw OutOfMemoryError we need to handle it // globally in some fashionable manner RandomAccessFile raf = new RandomAccessFile(mFile, "r"); FileChannel channel = raf.getChannel(); Iterator<OnDatatransferProgressListener> it = null; long transferred = 0; long size = mFile.length(); if (size == 0) size = -1;// w w w . j a va 2 s. com try { while ((readResult = channel.read(tmp)) >= 0) { out.write(tmp.array(), 0, readResult); tmp.clear(); transferred += readResult; synchronized (mDataTransferListeners) { it = mDataTransferListeners.iterator(); while (it.hasNext()) { it.next().onTransferProgress(readResult, transferred, size, mFile.getAbsolutePath()); } } } } catch (IOException io) { Log_OC.e("FileRequestException", io.getMessage()); throw new RuntimeException( "Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io); } finally { channel.close(); raf.close(); } }
From source file:com.MustacheMonitor.MustacheMonitor.FileUtils.java
/** * Copy a file/*w ww. j a va 2 s . c o m*/ * * @param srcFile file to be copied * @param destFile destination to be copied to * @return a FileEntry object * @throws IOException * @throws InvalidModificationException * @throws JSONException */ private JSONObject copyFile(File srcFile, File destFile) throws IOException, InvalidModificationException, JSONException { // Renaming a file to an existing directory should fail if (destFile.exists() && destFile.isDirectory()) { throw new InvalidModificationException("Can't rename a file to a directory"); } FileChannel input = new FileInputStream(srcFile).getChannel(); FileChannel output = new FileOutputStream(destFile).getChannel(); input.transferTo(0, input.size(), output); input.close(); output.close(); /* if (srcFile.length() != destFile.length()) { return false; } */ return getEntry(destFile); }
From source file:org.apache.tez.runtime.library.common.shuffle.Fetcher.java
private void releaseLock(FileLock lock) throws IOException { if (lock != null && lock.isValid()) { FileChannel lockChannel = lock.channel(); lock.release();//w w w . jav a 2 s. com lockChannel.close(); } }