当前位置: 首页>>代码示例>>Java>>正文


Java FileChannel.transferFrom方法代码示例

本文整理汇总了Java中java.nio.channels.FileChannel.transferFrom方法的典型用法代码示例。如果您正苦于以下问题:Java FileChannel.transferFrom方法的具体用法?Java FileChannel.transferFrom怎么用?Java FileChannel.transferFrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.nio.channels.FileChannel的用法示例。


在下文中一共展示了FileChannel.transferFrom方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: copyFile

import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.getParentFile().exists())
        destFile.getParentFile().mkdirs();

    if (!destFile.exists()) {
        destFile.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;

    try {
        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();
        }
    }
}
 
开发者ID:othreecodes,项目名称:Quicksend,代码行数:25,代码来源:CVFragment.java

示例2: restore_history

import java.nio.channels.FileChannel; //导入方法依赖的package包/类
private void restore_history () {
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + "jae.KidsPortal.Browser"
                    + "//databases//" + "history_DB_v01.db";
            String backupDBPath = "//Android//" + "//data//" + "//browser.backup//" + "history_DB_v01.db";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            FileChannel src = new FileInputStream(backupDB).getChannel();
            FileChannel dst = new FileOutputStream(currentDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();

            Snackbar.make(frameLayout, getString(R.string.toast_restore), Snackbar.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Snackbar.make(frameLayout, getString(R.string.toast_restore_not), Snackbar.LENGTH_LONG).show();
    }
}
 
开发者ID:JaeNuguid,项目名称:Kids-Portal-Android,代码行数:25,代码来源:Activity_settings_data.java

示例3: backup_ReadLater

import java.nio.channels.FileChannel; //导入方法依赖的package包/类
private void backup_ReadLater () {
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath3 = "//data//" + "jae.KidsPortal.Browser"
                    + "//databases//" + "readLater_DB_v01.db";
            String backupDBPath3 = "//Android//" + "//data//" + "//browser.backup//" + "readLater_DB_v01.db";
            File currentDB3 = new File(data, currentDBPath3);
            File backupDB3 = new File(sd, backupDBPath3);

            FileChannel src3 = new FileInputStream(currentDB3).getChannel();
            FileChannel dst3 = new FileOutputStream(backupDB3).getChannel();
            dst3.transferFrom(src3, 0, src3.size());
            src3.close();
            dst3.close();

            Snackbar.make(frameLayout, getString(R.string.toast_backup), Snackbar.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Snackbar.make(frameLayout, getString(R.string.toast_backup_not), Snackbar.LENGTH_LONG).show();
    }
}
 
开发者ID:JaeNuguid,项目名称:Kids-Portal-Android,代码行数:25,代码来源:Activity_settings_data.java

示例4: copyFile

import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public static void copyFile(File sourceFile, File destFile) throws IOException {
	if (!destFile.exists()) {
		destFile.createNewFile();
	}

	FileChannel source = null;
	FileChannel destination = null;
	try {
		source = new FileInputStream(sourceFile).getChannel();
		destination = new FileOutputStream(destFile).getChannel();

		// previous code: destination.transferFrom(source, 0, source.size());
		// should be to avoid infinite loops.

		long count =0;
		long size = source.size();                
		while ((count += destination.transferFrom(source, 0, size-count))<size);
	} finally {
		if (source != null) {
			source.close();
		}
		if (destination != null) {
			destination.close();
		}
	}
}
 
开发者ID:max6cn,项目名称:jmt,代码行数:27,代码来源:Jmt.java

示例5: backup_history

import java.nio.channels.FileChannel; //导入方法依赖的package包/类
private void backup_history () {
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + "jae.KidsPortal.Browser"
                    + "//databases//" + "history_DB_v01.db";
            String backupDBPath = "//Android//" + "//data//" + "//browser.backup//" + "history_DB_v01.db";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();

            Snackbar.make(frameLayout, getString(R.string.toast_backup), Snackbar.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Snackbar.make(frameLayout, getString(R.string.toast_backup_not), Snackbar.LENGTH_LONG).show();
    }
}
 
开发者ID:JaeNuguid,项目名称:Kids-Portal-Android,代码行数:25,代码来源:Activity_settings_data.java

示例6: copyFile

import java.nio.channels.FileChannel; //导入方法依赖的package包/类
static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.getParentFile().exists())
        destFile.getParentFile().mkdirs();

    if (!destFile.exists()) {
        destFile.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;

    try {
        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();
        }
    }
}
 
开发者ID:h4h13,项目名称:RetroMusicPlayer,代码行数:25,代码来源:TaggerUtils.java

示例7: update

import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public boolean update(){
    try{
        DebugLogger.log("Updating...", Level.INFO);
        String path = Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath();
        ReadableByteChannel in = Channels.newChannel(new URL(updateURL + "gfx.jar").openStream());
        FileChannel out = new FileOutputStream(path).getChannel();

        out.transferFrom(in, 0, Long.MAX_VALUE);
        in.close();
        out.close();

        DebugLogger.log("Update successful!", Level.INFO);
        return true;

    }catch(Exception e){
        DebugLogger.log("Update failed!", Level.INFO);
        return false;
    }
}
 
开发者ID:Ptrk25,项目名称:CDN-FX-2.2,代码行数:20,代码来源:Updater.java

示例8: copyResource

import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public void copyResource(OpenForReadResult input, OutputStream outputStream) throws IOException {
    assertBackgroundThread();
    try {
        InputStream inputStream = input.inputStream;
        if (inputStream instanceof FileInputStream && outputStream instanceof FileOutputStream) {
            FileChannel inChannel = ((FileInputStream)input.inputStream).getChannel();
            FileChannel outChannel = ((FileOutputStream)outputStream).getChannel();
            long offset = 0;
            long length = input.length;
            if (input.assetFd != null) {
                offset = input.assetFd.getStartOffset();
            }
            // transferFrom()'s 2nd arg is a relative position. Need to set the absolute
            // position first.
            inChannel.position(offset);
            outChannel.transferFrom(inChannel, 0, length);
        } else {
            final int BUFFER_SIZE = 8192;
            byte[] buffer = new byte[BUFFER_SIZE];
            
            for (;;) {
                int bytesRead = inputStream.read(buffer, 0, BUFFER_SIZE);
                
                if (bytesRead <= 0) {
                    break;
                }
                outputStream.write(buffer, 0, bytesRead);
            }
        }            
    } finally {
        input.inputStream.close();
        if (outputStream != null) {
            outputStream.close();
        }
    }
}
 
开发者ID:Andy-Ta,项目名称:COB,代码行数:37,代码来源:CordovaResourceApi.java

示例9: addLibrary

import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public void addLibrary(String name) throws Exception {
    OpenArch.log.info(String.format("Loading %s.", name));

    String libPath = String.format("/assets/%s/lib/%s", OpenArch.MODID, name);
    URL libUrl = getClass().getResource(libPath);
    ReadableByteChannel in = Channels.newChannel(libUrl.openStream());

    String tmpLibPath = String.format("%s/%s", tmpDir, name);
    File tmpLibFile = new File(tmpLibPath);

    if (tmpLibFile.exists()) {
        return;
    }

    FileChannel out = new FileOutputStream(tmpLibFile).getChannel();

    out.transferFrom(in, 0, Long.MAX_VALUE);

    tmpLibFile.deleteOnExit();

    if (!tmpLibFile.setReadable(true, false)) {
        throw new Exception("Failed to set readable flag on the library");
    }

    if (!tmpLibFile.setWritable(true, false)) {
        throw new Exception("Failed to set writable flag on the library");
    }

    if (!tmpLibFile.setExecutable(true, false)) {
        throw new Exception("Failed to set executable flag on the library");
    }

    out.close();
    in.close();
}
 
开发者ID:LeshaInc,项目名称:openarch,代码行数:36,代码来源:Native.java

示例10: copyResource

import java.nio.channels.FileChannel; //导入方法依赖的package包/类
private static void copyResource(CordovaResourceApi.OpenForReadResult input, OutputStream outputStream) throws IOException {
    try {
        InputStream inputStream = input.inputStream;
        if (inputStream instanceof FileInputStream && outputStream instanceof FileOutputStream) {
            FileChannel inChannel = ((FileInputStream)input.inputStream).getChannel();
            FileChannel outChannel = ((FileOutputStream)outputStream).getChannel();
            long offset = 0;
            long length = input.length;
            if (input.assetFd != null) {
                offset = input.assetFd.getStartOffset();
            }
            // transferFrom()'s 2nd arg is a relative position. Need to set the absolute
            // position first.
            inChannel.position(offset);
            outChannel.transferFrom(inChannel, 0, length);
        } else {
            final int BUFFER_SIZE = 8192;
            byte[] buffer = new byte[BUFFER_SIZE];

            for (;;) {
                int bytesRead = inputStream.read(buffer, 0, BUFFER_SIZE);

                if (bytesRead <= 0) {
                    break;
                }
                outputStream.write(buffer, 0, bytesRead);
            }
        }
    } finally {
        input.inputStream.close();
        if (outputStream != null) {
            outputStream.close();
        }
    }
}
 
开发者ID:rodrigonsh,项目名称:alerta-fraude,代码行数:36,代码来源:LocalFilesystem.java

示例11: migrateFile

import java.nio.channels.FileChannel; //导入方法依赖的package包/类
private static void migrateFile(File from, File to) {
  try {
    if (from.exists()) {
      FileChannel source      = new FileInputStream(from).getChannel();
      FileChannel destination = new FileOutputStream(to).getChannel();

      destination.transferFrom(source, 0, source.size());
      source.close();
      destination.close();
    }
  } catch (IOException ioe) {
    Log.w("EncryptedBackupExporter", ioe);
  }
}
 
开发者ID:XecureIT,项目名称:PeSanKita-android,代码行数:15,代码来源:EncryptedBackupExporter.java

示例12: writeMetadataSameSize

import java.nio.channels.FileChannel; //导入方法依赖的package包/类
/**
 * Replace the ilst metadata
 * <p/>
 * Because it is the same size as the original data nothing else has to be modified
 *
 * @param rawIlstData
 * @param oldIlstSize
 * @param startIstWithinFile
 * @param fileReadChannel
 * @param fileWriteChannel
 * @throws CannotWriteException
 * @throws IOException
 */
private void writeMetadataSameSize(ByteBuffer rawIlstData,
                                   long oldIlstSize,
                                   long startIstWithinFile,
                                   FileChannel fileReadChannel,
                                   FileChannel fileWriteChannel,
                                   Mp4BoxHeader tagsHeader) throws CannotWriteException, IOException {
    fileReadChannel.position(0);
    fileWriteChannel.transferFrom(fileReadChannel, 0, startIstWithinFile);
    fileWriteChannel.position(startIstWithinFile);
    fileWriteChannel.write(rawIlstData);
    fileReadChannel.position(startIstWithinFile + oldIlstSize);

    writeDataAfterIlst(fileReadChannel, fileWriteChannel, tagsHeader);
}
 
开发者ID:openaudible,项目名称:openaudible,代码行数:28,代码来源:Mp4TagWriter.java

示例13: writeNeroData

import java.nio.channels.FileChannel; //导入方法依赖的package包/类
/**
 * If the existing files contains a tags atom and chp1 atom underneath the meta atom that means the file was
 * encoded by Nero. Applications such as foobar read this non-standard tag before the more usual data within
 * ilst causing problems. So the solution is to convert the tags atom and its children into a free atom whilst
 * leaving the chp1 atom alone.
 *
 * @param fileReadChannel
 * @param fileWriteChannel
 * @param tagsHeader
 * @throws IOException
 */
private void writeNeroData(FileChannel fileReadChannel, FileChannel fileWriteChannel, Mp4BoxHeader tagsHeader)
        throws IOException, CannotWriteException {
    //Write from after ilst upto tags atom
    long writeBetweenIlstAndTags = tagsHeader.getFilePos() - fileReadChannel.position();
    fileWriteChannel.transferFrom(fileReadChannel, fileWriteChannel.position(), writeBetweenIlstAndTags);
    fileWriteChannel.position(fileWriteChannel.position() + writeBetweenIlstAndTags);

    //Replace tags atom (and children) by a free atom
    convertandWriteTagsAtomToFreeAtom(fileWriteChannel, tagsHeader);

    //Write after tags atom
    fileReadChannel.position(tagsHeader.getFilePos() + tagsHeader.getLength());
    writeDataInChunks(fileReadChannel, fileWriteChannel);
}
 
开发者ID:openaudible,项目名称:openaudible,代码行数:26,代码来源:Mp4TagWriter.java

示例14: writeFromEndOfIlstToNeroTagsAndMakeNeroFree

import java.nio.channels.FileChannel; //导入方法依赖的package包/类
/**
 * If any data between existing {@code ilst} atom and {@code tags} atom write it to new file, then convert
 * {@code tags} atom to a {@code free} atom.
 *
 * @param endOfMoov
 * @param fileReadChannel
 * @param fileWriteChannel
 * @param neroTagsHeader
 * @throws IOException
 */
private void writeFromEndOfIlstToNeroTagsAndMakeNeroFree(long endOfMoov, FileChannel fileReadChannel, FileChannel fileWriteChannel, Mp4BoxHeader neroTagsHeader)
        throws IOException
{
    //Write from after ilst upto tags atom
    long writeBetweenIlstAndTags = neroTagsHeader.getFilePos() - fileReadChannel.position();
    fileWriteChannel.transferFrom(fileReadChannel, fileWriteChannel.position(), writeBetweenIlstAndTags);
    fileWriteChannel.position(fileWriteChannel.position() + writeBetweenIlstAndTags);
    convertandWriteTagsAtomToFreeAtom(fileWriteChannel, neroTagsHeader);

    //Write after tags atom upto end of moov
    fileReadChannel.position(neroTagsHeader.getFileEndPos());
    long extraData = endOfMoov - fileReadChannel.position();
    fileWriteChannel.transferFrom(fileReadChannel, fileWriteChannel.position(), extraData);
}
 
开发者ID:GlennioTech,项目名称:MetadataEditor,代码行数:25,代码来源:Mp4TagWriter.java

示例15: testReadableByteChannel

import java.nio.channels.FileChannel; //导入方法依赖的package包/类
@Test
public void testReadableByteChannel() throws Exception {
    int[] testSizes = { 0, 10, 1023, 1024, 1025, 2047, 2048, 2049 };

    for (int size : testSizes) {
        SelectorProvider sp = SelectorProvider.provider();
        Pipe p = sp.openPipe();
        Pipe.SinkChannel sink = p.sink();
        Pipe.SourceChannel source = p.source();
        sink.configureBlocking(false);

        ByteBuffer outgoingdata = ByteBuffer.allocateDirect(size + 10);
        byte[] someBytes = new byte[size + 10];
        generator.nextBytes(someBytes);
        outgoingdata.put(someBytes);
        outgoingdata.flip();

        int totalWritten = 0;
        while (totalWritten < size + 10) {
            int written = sink.write(outgoingdata);
            if (written < 0)
                throw new Exception("Write failed");
            totalWritten += written;
        }

        File f = File.createTempFile("blah"+size, null);
        f.deleteOnExit();
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
        FileChannel fc = raf.getChannel();
        long oldPosition = fc.position();

        long bytesWritten = fc.transferFrom(source, 0, size);
        fc.force(true);
        if (bytesWritten != size)
            throw new RuntimeException("Transfer failed");

        if (fc.position() != oldPosition)
            throw new RuntimeException("Position changed");

        if (fc.size() != size)
            throw new RuntimeException("Unexpected sink size "+ fc.size());

        fc.close();
        sink.close();
        source.close();

        f.delete();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:50,代码来源:Transfer.java


注:本文中的java.nio.channels.FileChannel.transferFrom方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。