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:Main.java

public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
    FileChannel fromChannel = null;
    FileChannel toChannel = null;
    try {/* w  ww  .j  a  v a2s .com*/
        fromChannel = fromFile.getChannel();
        toChannel = toFile.getChannel();
        fromChannel.transferTo(0, fromChannel.size(), toChannel);
    } finally {
        try {
            if (fromChannel != null) {
                fromChannel.close();
            }
        } finally {
            if (toChannel != null) {
                toChannel.close();
            }
        }
    }
}

From source file:Main.java

public static void fastFileCopy(File source, File target) {
    FileChannel in = null;/*  ww w. j a v a2  s  .c  o m*/
    FileChannel out = null;
    long start = System.currentTimeMillis();
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(source);
        in = fis.getChannel();
        fos = new FileOutputStream(target);
        out = fos.getChannel();

        long size = in.size();
        long transferred = in.transferTo(0, size, out);

        while (transferred != size) {
            transferred += in.transferTo(transferred, size - transferred, out);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        close(fis);
        close(fos);
        close(in);
        close(out);
    }
    long end = System.currentTimeMillis();

    Log.d(target.getAbsolutePath(), nf.format(source.length()) + "B: "
            + ((end - start > 0) ? nf.format(source.length() / (end - start)) : 0) + " KB/s");
}

From source file:Main.java

public static File copyFileTo(File src, File dst) {
    FileChannel in = null;//w ww  .j a va  2s .co m
    FileChannel out = null;
    FileInputStream inStream = null;
    FileOutputStream outStream = null;
    try {
        inStream = new FileInputStream(src);
        outStream = new FileOutputStream(dst);
        in = inStream.getChannel();
        out = outStream.getChannel();
        in.transferTo(0, in.size(), out);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
            if (inStream != null) {
                inStream.close();
            }
            if (outStream != null) {
                outStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return dst;
}

From source file:net.amigocraft.mpt.command.InstallCommand.java

@SuppressWarnings("unchecked")
public static void downloadPackage(String id) throws MPTException {
    JSONObject packages = (JSONObject) Main.packageStore.get("packages");
    if (packages != null) {
        JSONObject pack = (JSONObject) packages.get(id);
        if (pack != null) {
            if (pack.containsKey("name") && pack.containsKey("version") && pack.containsKey("url")) {
                if (pack.containsKey("sha1") || !Config.ENFORCE_CHECKSUM) {
                    String name = pack.get("name").toString();
                    String version = pack.get("version").toString();
                    String fullName = name + " v" + version;
                    String url = pack.get("url").toString();
                    String sha1 = pack.containsKey("sha1") ? pack.get("sha1").toString() : "";
                    if (pack.containsKey("installed")) { //TODO: compare versions
                        throw new MPTException(ID_COLOR + name + ERROR_COLOR + " is already installed");
                    }/*from w  w  w . j a va 2  s .  c  o  m*/
                    try {
                        URLConnection conn = new URL(url).openConnection();
                        conn.connect();
                        ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream());
                        File file = new File(Main.plugin.getDataFolder(),
                                "cache" + File.separator + id + ".zip");
                        file.setReadable(true, false);
                        file.setWritable(true, false);
                        file.getParentFile().mkdirs();
                        file.createNewFile();
                        FileOutputStream os = new FileOutputStream(file);
                        os.getChannel().transferFrom(rbc, 0, MiscUtil.getFileSize(new URL(url)));
                        os.close();
                        if (!sha1.isEmpty() && !sha1(file.getAbsolutePath()).equals(sha1)) {
                            file.delete();
                            throw new MPTException(ERROR_COLOR + "Failed to install package " + ID_COLOR
                                    + fullName + ERROR_COLOR + ": checksum mismatch!");
                        }
                    } catch (IOException ex) {
                        throw new MPTException(
                                ERROR_COLOR + "Failed to download package " + ID_COLOR + fullName);
                    }
                } else
                    throw new MPTException(ERROR_COLOR + "Package " + ID_COLOR + id + ERROR_COLOR
                            + " is missing SHA-1 checksum! Aborting...");
            } else
                throw new MPTException(ERROR_COLOR + "Package " + ID_COLOR + id + ERROR_COLOR
                        + " is missing required elements!");
        } else
            throw new MPTException(ERROR_COLOR + "Cannot find package with id " + ID_COLOR + id);
    } else {
        throw new MPTException(ERROR_COLOR + "Package store is malformed!");
    }
}

From source file:org.eclipse.thym.hybrid.test.TestUtils.java

public static File createTempFile(String fileName) throws IOException, FileNotFoundException {
    File f = new File(getTempDirectory(), fileName);
    f.createNewFile();//from  www  . j  a  v a2s .c  om
    f.deleteOnExit();
    FileOutputStream fout = null;
    FileChannel out = null;
    InputStream in = TestUtils.class.getResourceAsStream("/" + fileName);
    try {
        fout = new FileOutputStream(f);
        out = fout.getChannel();

        out.transferFrom(Channels.newChannel(in), 0, Integer.MAX_VALUE);
        return f;
    } finally {
        if (out != null)
            out.close();
        if (in != null)
            in.close();
        if (fout != null)
            fout.close();
    }
}

From source file:Main.java

public static void copyInputStreamToFile(InputStream inputStream, File file) throws IOException {
    FileOutputStream fileOutputStream = null;
    FileChannel fileChannel = null;
    try {/*  w ww.ja v  a 2s.  c o  m*/
        fileOutputStream = new FileOutputStream(file);
        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));
        }

    } catch (Throwable throwable) {
        throwable.printStackTrace();
    } finally {

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

From source file:Main.java

public static void copyFile(File src, File dest) throws IOException {
    FileOutputStream fileOut = null;
    FileInputStream fileIn = null;
    try {/* w w w  .  jav  a 2 s .  c om*/
        fileOut = new FileOutputStream(dest);
        fileIn = new FileInputStream(src);
        FileChannel inChannel = fileIn.getChannel();
        FileChannel outChannel = fileOut.getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (fileIn != null) {
            fileIn.close();
        }
        if (fileOut != null) {
            fileOut.close();
        }
    }
}

From source file:com.comcast.cdn.traffic_control.traffic_monitor.util.PeriodicResourceUpdater.java

static void copyDatabase(final File existingDB, final File newDB) throws IOException {
    final FileInputStream in = new FileInputStream(newDB);
    final FileOutputStream out = new FileOutputStream(existingDB);
    final FileLock lock = out.getChannel().tryLock();
    if (lock != null) {
        LOGGER.info("Updating location database.");
        IOUtils.copy(in, out);//w w w  . ja v a 2s. c  o m
        existingDB.setReadable(true, false);
        existingDB.setWritable(true, false);
        lock.release();
        LOGGER.info("Successfully updated location database.");
    } else {
        LOGGER.info("Location database locked by another process.");
    }
    IOUtils.closeQuietly(in);
    IOUtils.closeQuietly(out);
}

From source file:ja.lingo.engine.util.EngineFiles.java

private static void appendFile(String fileName, FileOutputStream fos, boolean prependWithLength)
        throws IOException {
    FileInputStream fis = new FileInputStream(fileName);
    FileChannel fic = fis.getChannel();
    FileChannel foc = fos.getChannel();

    ByteBuffer buffer = ByteBuffer.allocate(COPY_BUFFER_SIZE);

    // put header: length (1 int = 4 bytes)
    if (prependWithLength) {
        buffer.putInt((int) new File(fileName).length());
    }/*from   w  w  w .  j  av  a  2s  .com*/

    // put body
    do {
        buffer.flip();
        foc.write(buffer);
        buffer.clear();
    } while (fic.read(buffer) != -1);
    fic.close();
    // NOTE: do not close 'foc'

    Files.delete(fileName);
}

From source file:org.bml.util.geo.util.geolite.GISNetworkTool.java

public static boolean getFileFromNet(String urlIn, String fileOutName) {
    URL myURL = null;//  w ww . j av a2 s.  c o m
    ReadableByteChannel myReadableByteChannel = null;
    FileOutputStream myFileOutputStream = null;
    try {
        myURL = new URL(urlIn);
        myReadableByteChannel = Channels.newChannel(myURL.openStream());
        myFileOutputStream = new FileOutputStream(fileOutName);
        myFileOutputStream.getChannel().transferFrom(myReadableByteChannel, 0, Long.MAX_VALUE);
    } catch (MalformedURLException ex) {
        Logger.getLogger(GISNetworkTool.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    } catch (IOException ex) {
        Logger.getLogger(GISNetworkTool.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    } finally {
        IOUtils.closeQuietly(myReadableByteChannel);
        IOUtils.closeQuietly(myFileOutputStream);
    }
    return true;
}