Example usage for java.io FileOutputStream getChannel

List of usage examples for java.io FileOutputStream getChannel

Introduction

In this page you can find the example usage for java.io FileOutputStream getChannel.

Prototype

public FileChannel getChannel() 

Source Link

Document

Returns the unique java.nio.channels.FileChannel FileChannel object associated with this file output stream.

Usage

From source file:org.wso2.carbon.identity.common.testng.MockInitialContextFactory.java

/**
 * Copies a resource inside a jar to external file within a directory.
 * Then returns the created file./* www  .  j  av a  2  s .  co m*/
 *
 * @param relativeFilePath
 * @param clazz
 * @return
 * @throws TestCreationException
 */
private static File copyTempFile(String relativeFilePath, Class clazz) throws TestCreationException {

    URL url = clazz.getClassLoader().getResource(relativeFilePath);
    if (url == null) {
        throw new TestCreationException("Could not find a resource on the classpath : " + relativeFilePath);
    }
    InputStream inputStream;
    try {
        inputStream = url.openStream();
        ReadableByteChannel inputChannel = Channels.newChannel(inputStream);
        File tempFile = File.createTempFile("tmp_", "_registry.sql");
        FileOutputStream fos = new FileOutputStream(tempFile);
        WritableByteChannel targetChannel = fos.getChannel();
        //Transfer data from input channel to output channel
        ((FileChannel) targetChannel).transferFrom(inputChannel, 0, Short.MAX_VALUE);
        inputStream.close();
        targetChannel.close();
        fos.close();
        return tempFile;
    } catch (IOException e) {
        throw new TestCreationException(
                "Could not copy the file content to temp file from : " + relativeFilePath);
    }
}

From source file:yui.classes.utils.IOUtils.java

public static void fastCopy(File source, File dest) throws IOException {
    FileInputStream fi = new FileInputStream(source);
    FileChannel fic = fi.getChannel();
    MappedByteBuffer mbuf = fic.map(FileChannel.MapMode.READ_ONLY, 0, source.length());
    fic.close();//from  w w  w .j  a va 2 s .c  o m
    fi.close();

    FileOutputStream fo = new FileOutputStream(dest);
    FileChannel foc = fo.getChannel();
    foc.write(mbuf);
    foc.close();
    fo.close();

}

From source file:com.alibaba.otter.shared.common.utils.NioUtilsPerformance.java

public static void sendFileTest(File source, File target) throws Exception {
    FileInputStream fis = null;/*from  www. j  a  v a  2  s. co m*/
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(source);
        fos = new FileOutputStream(target);
        FileChannel sChannel = fis.getChannel();
        FileChannel tChannel = fos.getChannel();
        target.createNewFile();
        sChannel.transferTo(0, source.length(), tChannel);
        tChannel.close();
        sChannel.close();
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(fos);
    }
}

From source file:Main.java

public static File copyFile(File destinationFile, File sourceFile) throws IOException {
    FileInputStream inputStream = null;
    FileChannel inputChannel = null;
    FileOutputStream outputStream = null;
    FileChannel outputChannel = null;
    try {/*from   ww  w .j  a v  a 2s  . co m*/
        inputStream = new FileInputStream(sourceFile);
        inputChannel = inputStream.getChannel();
        outputStream = new FileOutputStream(destinationFile);
        outputChannel = outputStream.getChannel();
        inputChannel.transferTo(0, inputChannel.size(), outputChannel);
        return destinationFile;
    } catch (IOException exception) {
        throw exception;
    } finally {
        if (inputChannel != null) {
            inputChannel.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
        if (outputChannel != null) {
            outputChannel.close();
        }
        if (outputStream != null) {
            outputStream.close();
        }
    }
}

From source file:com.alibaba.otter.shared.common.utils.NioUtilsPerformance.java

public static void channelTest(File source, File target) throws Exception {
    FileInputStream fis = null;/*from w  w  w .  ja  va  2  s .c  om*/
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(source);
        fos = new FileOutputStream(target);
        FileChannel sChannel = fis.getChannel();
        FileChannel tChannel = fos.getChannel();

        target.createNewFile();

        ByteBuffer buffer = ByteBuffer.allocate(16 * 1024);
        while (sChannel.read(buffer) > 0) {
            buffer.flip();
            tChannel.write(buffer);
            buffer.clear();
        }

        tChannel.close();
        sChannel.close();
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(fos);
    }
}

From source file:edu.umn.cs.spatialHadoop.util.FileUtil.java

/**
 * Copies a part of a file from a remote file system (e.g., HDFS) to a local
 * file. Returns a path to a local temporary file.
 * /*w  ww . java  2 s  .c  o m*/
 * @param conf
 * @param split
 * @return
 * @throws IOException
 */
public static String copyFileSplit(Configuration conf, FileSplit split) throws IOException {
    FileSystem fs = split.getPath().getFileSystem(conf);

    // Special case of a local file. Skip copying the file
    if (fs instanceof LocalFileSystem && split.getStart() == 0)
        return split.getPath().toUri().getPath();

    File destFile = File.createTempFile(split.getPath().getName(), "tmp");
    // Special handling for HTTP files for more efficiency
    /*if (fs instanceof HTTPFileSystem && split.getStart() == 0) {
      URL website = split.getPath().toUri().toURL();
      ReadableByteChannel rbc = Channels.newChannel(website.openStream());
      FileOutputStream fos = new FileOutputStream(destFile);
      fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
      fos.close();
      return destFile.getAbsolutePath();
    }*/

    // Length of input file. We do not depend on split.length because it is
    // not
    // set by input format for performance reason. Setting it in the input
    // format would cost a lot of time because it runs on the client machine
    // while the record reader runs on slave nodes in parallel
    long length = fs.getFileStatus(split.getPath()).getLen();

    FSDataInputStream in = fs.open(split.getPath());
    in.seek(split.getStart());
    ReadableByteChannel rbc = Channels.newChannel(in);

    // Prepare output file for write
    FileOutputStream out = new FileOutputStream(destFile);

    out.getChannel().transferFrom(rbc, 0, length);

    in.close();
    out.close();
    return destFile.getAbsolutePath();
}

From source file:Main.java

@SuppressWarnings("unused")
private static boolean copy2SingleFileByChannel(File sourceFile, File destFile) {
    boolean copyOK = true;
    FileInputStream inputStream = null;
    FileOutputStream outputStream = null;
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {// w  w  w  .  ja  v a  2  s.c om
        inputStream = new FileInputStream(sourceFile);
        outputStream = new FileOutputStream(destFile);
        inputChannel = inputStream.getChannel();
        outputChannel = outputStream.getChannel();
        inputChannel.transferTo(0, inputChannel.size(), outputChannel);
    } catch (Exception e) {
        copyOK = false;
    } finally {
        try {
            inputChannel.close();
            inputStream.close();
            outputChannel.close();
            outputStream.close();
        } catch (IOException e) {
            copyOK = false;
            e.printStackTrace();
        }
    }
    return copyOK;
}

From source file:net.lyonlancer5.mcmp.karasu.util.ModFileUtils.java

static long download(String url, File output) throws IOException {
    URL url1 = new URL(url);
    ReadableByteChannel rbc = Channels.newChannel(url1.openStream());
    FileOutputStream fos = new FileOutputStream(output);

    if (!output.exists()) {
        output.getParentFile().mkdirs();
        output.createNewFile();/*  w  w w  .jav a 2 s.  co m*/
    }

    long f = fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
    return f;
}

From source file:Main.java

public static void copyInputStreamToFile(InputStream inputStream, File file) throws IOException {
    FileOutputStream fileOutputStream = null;
    Throwable th;/*from   w w  w .  j  ava 2s .  c o  m*/
    FileChannel fileChannel = null;
    try {
        fileOutputStream = new FileOutputStream(file);
        try {
            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));
            }
            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();
                }
            }
        } catch (Throwable th2) {
            th = th2;
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e3) {
                    e3.printStackTrace();
                }
            }
            if (fileChannel != null) {
                try {
                    fileChannel.close();
                } catch (Exception e4) {
                    e4.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (Exception e42) {
                    e42.printStackTrace();
                }
            }
            throw th;
        }
    } catch (Throwable th3) {
        th = th3;
        Object obj = fileChannel;
        if (inputStream != null) {
            inputStream.close();
        }
        if (fileChannel != null) {
            fileChannel.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }

    }
}

From source file:lyonlancer5.karasu.util.ModFileUtils.java

static void download(String url, File output) throws IOException {
    URL url1 = new URL(url);
    ReadableByteChannel rbc = Channels.newChannel(url1.openStream());
    FileOutputStream fos = new FileOutputStream(output);

    if (!output.exists()) {
        output.getParentFile().mkdirs();
        output.createNewFile();/*from www . j  a  v  a2  s  . co m*/
    }

    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
}