當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。