List of usage examples for java.nio.channels FileChannel transferTo
public abstract long transferTo(long position, long count, WritableByteChannel target) throws IOException;
From source file:com.shinymayhem.radiopresets.ServiceRadioPlayer.java
public void copyLog() { //String path = Environment.getExternalStorageDirectory().getAbsolutePath(); String path = getExternalFilesDir(null).getAbsolutePath(); File src = getFileStreamPath(ActivityLogger.LOG_FILENAME); File dst = new File(path + File.separator + ActivityLogger.LOG_FILENAME); try {/*from w w w . j a v a 2 s . co m*/ if (dst.createNewFile()) { if (LOCAL_LOGV) log("sd file created", "v"); } else { if (LOCAL_LOGV) log("sd file exists?", "v"); } } catch (IOException e2) { log("sd file error", "e"); Toast.makeText(this, "sd file error", Toast.LENGTH_SHORT).show(); e2.printStackTrace(); } FileChannel in = null; FileChannel out = null; try { in = new FileInputStream(src).getChannel(); } catch (FileNotFoundException e1) { log("in file not found", "e"); Toast.makeText(this, "in file not found", Toast.LENGTH_SHORT).show(); e1.printStackTrace(); } try { out = new FileOutputStream(dst).getChannel(); } catch (FileNotFoundException e1) { log("out file not found", "e"); Toast.makeText(this, "out file not found", Toast.LENGTH_SHORT).show(); e1.printStackTrace(); } try { in.transferTo(0, in.size(), out); if (LOCAL_LOGD) log("log file copied to " + path + File.separator + ActivityLogger.LOG_FILENAME, "d"); if (in != null) { in.close(); } if (out != null) { out.close(); } clearLog(); } catch (IOException e) { log("error copying log file", "e"); Toast.makeText(this, "error copying log file", Toast.LENGTH_SHORT).show(); if (LOCAL_LOGV) e.printStackTrace(); } finally { } }
From source file:com.androidquery.AbstractAQuery.java
/** * Create a temporary file on EXTERNAL storage (sdcard) that holds the cached content of the url. * Returns null if url is not cached, or the system cannot create such file (sdcard is absent, such as in emulator). * /*from w w w . java 2 s.co m*/ * The returned file is accessable to all apps, therefore it is ideal for sharing content (such as photo) via the intent mechanism. * * <br> * <br> * Example Usage: * * <pre> * Intent intent = new Intent(Intent.ACTION_SEND); * intent.setType("image/jpeg"); * intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); * startActivityForResult(Intent.createChooser(intent, "Share via:"), 0); * </pre> * * <br> * The temp file will be deleted when AQUtility.cleanCacheAsync is invoked, or the file can be explicitly deleted after use. * * @param url The url of the desired cached content. * @param filename The desired file name, which might be used by other apps to describe the content, such as an email attachment. * @return temp file * */ public File makeSharedFile(String url, String filename) { File file = null; try { File cached = getCachedFile(url); if (cached != null) { File temp = AQUtility.getTempDir(); if (temp != null) { file = new File(temp, filename); file.createNewFile(); FileInputStream fis = new FileInputStream(cached); FileOutputStream fos = new FileOutputStream(file); FileChannel ic = fis.getChannel(); FileChannel oc = fos.getChannel(); try { ic.transferTo(0, ic.size(), oc); } finally { AQUtility.close(fis); AQUtility.close(fos); AQUtility.close(ic); AQUtility.close(oc); } } } } catch (Exception e) { AQUtility.debug(e); } return file; }
From source file:edu.harvard.iq.dvn.core.web.servlet.FileDownloadServlet.java
public void streamData(FileChannel in, WritableByteChannel out, String varHeader) { long position = 0; long howMany = 32 * 1024; try {//from w ww. j av a2 s . c o m // If we are streaming a TAB-delimited file, we will need to add the // variable header line: if (varHeader != null) { ByteBuffer varHeaderByteBuffer = ByteBuffer.wrap(varHeader.getBytes()); out.write(varHeaderByteBuffer); } while (position < in.size()) { in.transferTo(position, howMany, out); position += howMany; } in.close(); out.close(); } catch (IOException ex) { // whatever. we don't care at this point. } }
From source file:es.pode.publicacion.negocio.servicios.SrvPublicacionServiceImpl.java
/** * Mueve el contenido de un directorio a otro directorio. * /* ww w .j a v a 2s . c om*/ * @param oldDir * Directorio origen. * @param newDir * Directorio destino. * @return devuelve el tamanio del directorio movido. * @throws Exception * */ protected Long handleMoveDir(File oldDir, File newDir) throws IOException { long longitudTransferida = 0; if (oldDir.isDirectory()) { newDir.mkdirs(); String list[] = oldDir.list(); for (int i = 0; i < list.length; i++) { String dest1 = newDir.getAbsolutePath() + "/" + list[i]; String src1 = oldDir.getAbsolutePath() + "/" + list[i]; longitudTransferida += handleMoveDir(new File(src1), new File(dest1)).longValue(); } } else { FileInputStream fin = new FileInputStream(oldDir); FileOutputStream fos = new FileOutputStream(newDir); FileChannel sourceChannel = fin.getChannel(); FileChannel targetChannel = fos.getChannel(); longitudTransferida = sourceChannel.size(); sourceChannel.transferTo(0, sourceChannel.size(), targetChannel); sourceChannel.close(); targetChannel.close(); } return new Long(longitudTransferida); }