Example usage for java.nio.channels FileChannel close

List of usage examples for java.nio.channels FileChannel close

Introduction

In this page you can find the example usage for java.nio.channels FileChannel close.

Prototype

public final void close() throws IOException 

Source Link

Document

Closes this channel.

Usage

From source file:org.pmedv.core.util.FileUtils.java

/**
 * Copies a file from in to out using a progress monitor
 * /*w ww.  ja va 2  s.c  om*/
 * @param in the file to read from
 * @param out the file to write to
 * @param monitor the monitor interface to use
 * @throws IOException
 */
public static void copyFile(File in, File out, IProgressMonitor monitor) throws IOException {

    int bufsize = 8192;
    int transferred = 0;

    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = new FileOutputStream(out).getChannel();

    try {
        while (transferred < inChannel.size()) {
            inChannel.transferTo(transferred, bufsize, outChannel);
            transferred += bufsize;
            int progress = (int) (transferred / (inChannel.size() / 100));
            monitor.setProgress(progress);
        }

    } catch (IOException e) {
        throw e;
    } finally {
        if (inChannel != null) {
            inChannel.close();
        }
        if (outChannel != null) {
            outChannel.close();
        }
    }
}

From source file:com.ah.ui.actions.home.clientManagement.service.CertificateGenSV.java

@SuppressWarnings("resource")
public static byte[] readFromFile(File file) throws IOException {
    FileChannel fileChannel = new FileInputStream(file).getChannel();
    ByteBuffer bb = ByteBuffer.allocate((int) fileChannel.size());
    fileChannel.read(bb);/*from w  w w .  j ava  2s  .  com*/
    fileChannel.close();
    bb.flip();
    byte[] bytes;

    if (bb.hasArray()) {
        bytes = bb.array();
    } else {
        bytes = new byte[bb.limit()];
        bb.get(bytes);
    }

    return bytes;
}

From source file:com.log4ic.compressor.utils.FileUtils.java

/**
 * ?/*  w  w  w.j  a  v a2s .  co m*/
 *
 * @param content
 * @param filePath
 * @return
 */
public static File writeFile(byte[] content, String filePath) {
    FileOutputStream out = null;
    FileChannel outChannel = null;
    File file = new File(filePath);

    if (file.exists()) {
        file.delete();
    }

    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();
    }

    ByteBuffer outBuffer = ByteBuffer.allocate(content.length);
    outBuffer.put(content);
    outBuffer.flip();
    try {
        out = new FileOutputStream(file);

        outChannel = out.getChannel();

        outChannel.write(outBuffer);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (outChannel != null) {
            try {
                outChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (out != null) {
            try {
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return file.exists() ? file : null;
}

From source file:ubicrypt.core.Utils.java

public static boolean isAppInUse(final Path ubiqFolder) throws IOException {
    Files.createDirectories(ubiqFolder);
    final File file = Paths.get(ubiqFolder.toString(), "lock").toFile();
    final FileChannel channel = new RandomAccessFile(file, "rw").getChannel();

    final FileLock lock;
    try {/*from   www  .  ja  va 2 s  .  c o m*/
        lock = channel.tryLock();
    } catch (final OverlappingFileLockException e) {
        return true;
    }
    if (lock == null) {
        return true;
    }
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            try {
                lock.release();
                channel.close();
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    });
    return false;
}

From source file:Main.java

public static void copyInputStreamToFile(InputStream inputStream, File file) throws IOException {
    FileOutputStream fileOutputStream = null;
    Throwable th;// ww  w. j  ava2  s.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:dsd.controller.ParserControler.java

@SuppressWarnings("resource")
private static boolean TransferImageToSite(File inFile, File outFile) {
    if (!outFile.exists()) {
        try {//from   w ww  .  java  2s  .c o m
            outFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    FileChannel inChannel = null;
    FileChannel outChannel = null;
    try {
        inChannel = new FileInputStream(inFile).getChannel();
        outChannel = new FileOutputStream(outFile).getChannel();
        int maxCount = (64 * 1024 * 1024) - (32 * 1024);
        long size = inChannel.size();
        long position = 0;
        while (position < size) {
            position += inChannel.transferTo(position, maxCount, outChannel);
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return false;
}

From source file:org.eclipse.thym.core.internal.util.FileUtils.java

private static void copyFile(File source, File target) throws IOException {

    File file = null;/* ww w.jav  a2s.com*/
    if (source.isDirectory() && source.exists() && target.isDirectory() && target.exists()) {

        File[] children = source.listFiles();
        for (File child : children) {
            file = target;
            if (child.isDirectory()) {
                file = new File(target, child.getName());
                if (!file.exists())
                    file.mkdir();
            }
            copyFile(child, file);
        }
    } else {// source is a file
        if (target.isFile()) {
            file = target;
        } else {
            file = new File(target, source.getName());
        }

        FileChannel out = null;
        FileChannel in = null;
        try {
            if (!file.exists()) {
                file.createNewFile();
            }

            out = new FileOutputStream(file).getChannel();
            in = new FileInputStream(source).getChannel();
            in.transferTo(0, in.size(), out);
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (out != null)
                out.close();
            if (in != null)
                in.close();
        }
    }
}

From source file:padl.creator.cppfile.eclipse.plugin.internal.Utils.java

static void copyFile(final File sourceFile, final File destFile) throws IOException {

    if (!destFile.exists()) {
        destFile.createNewFile();/*from w w w.  j  a  v a  2 s . co  m*/
    }

    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();
        }
    }
}

From source file:bridge.toolkit.commands.S1000DConverter.java

/**
 * copy file content//from  www.  j a  v  a 2  s .co m
 * 
 * @param from_
 *            String
 * @param to_
 *            String
 * @throws Exception
 */
public static void copy(String from_, String to_) throws Exception {

    try {

        FileChannel sourceChannel = new FileInputStream(from_).getChannel();
        FileChannel destinationChannel = new FileOutputStream(to_).getChannel();
        sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
        sourceChannel.close();
        destinationChannel.close();

    } catch (Exception ex) {

        throw (ex);
    }
}

From source file:Main.java

public static boolean fileCopy(File srcFile, File dstFile) {
    int length = 1048891;
    FileChannel inC = null;
    FileChannel outC = null;//from  ww w  .  j a  va 2s. co  m
    try {
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(dstFile);
        inC = in.getChannel();
        outC = out.getChannel();
        ByteBuffer b = null;
        while (inC.position() < inC.size()) {
            if ((inC.size() - inC.position()) < length) {
                length = (int) (inC.size() - inC.position());
            } else {
                length = 1048891;
            }
            b = ByteBuffer.allocateDirect(length);
            inC.read(b);
            b.flip();
            outC.write(b);
            outC.force(false);
        }

        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (inC != null && inC.isOpen()) {
                inC.close();
            }
            if (outC != null && outC.isOpen()) {
                outC.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}