Example usage for java.nio.channels FileChannel isOpen

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

Introduction

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

Prototype

public final boolean isOpen() 

Source Link

Usage

From source file:it.geosolutions.tools.io.file.IOUtils.java

/**
 * Optimize version of copy method for file channels.
 * /*from   w  w w. jav a  2 s  . c  o m*/
 * @param bufferSize
 *            size of the temp buffer to use for this copy.
 * @param source
 *            the source {@link ReadableByteChannel}.
 * @param destination
 *            the destination {@link WritableByteChannel};.
 * @throws IOException
 *             in case something bad happens.
 */
public static void copyFileChannel(int bufferSize, FileChannel source, FileChannel destination)
        throws IOException {

    Objects.notNull(source, destination);
    if (!source.isOpen() || !destination.isOpen())
        throw new IllegalStateException("Source and destination channels must be open.");
    FileLock lock = null;
    try {

        lock = destination.lock();
        final long sourceSize = source.size();
        long pos = 0;
        while (pos < sourceSize) {
            // read and flip
            final long remaining = (sourceSize - pos);
            final int mappedZoneSize = remaining >= bufferSize ? bufferSize : (int) remaining;
            destination.transferFrom(source, pos, mappedZoneSize);
            // update zone
            pos += mappedZoneSize;

        }
    } finally {
        if (lock != null) {
            try {
                lock.release();
            } catch (Throwable t) {
                if (LOGGER.isInfoEnabled())
                    LOGGER.info(t.getLocalizedMessage(), t);
            }
        }

    }
}

From source file:Main.java

public static boolean fileCopy(File srcFile, File dstFile) {
    int length = 1048891;
    FileChannel inC = null;
    FileChannel outC = null;//from  www .  ja v  a2s .  c om
    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();
        }
    }
}

From source file:com.sds.acube.ndisc.mts.xserver.util.XNDiscUtils.java

/**
 * ? /*from   w ww .  j av  a  2 s  .co m*/
 * 
 * @param nFile
 *             NFile  
 * @param bForwardMedia
 *            ?  ?   
 * @param bForce
 *              
 * @throws Exception
 */
public static void copyNFile(NFile[] nFile, boolean bForwardMedia, boolean bForce) throws Exception {
    FileChannel srcChannel = null;
    FileChannel dstChannel = null;
    String srcFile = null;
    String dstFile = null;
    try {
        for (int i = 0; i < nFile.length; i++) {
            if (false == bForce && XNDiscConfig.STAT_NONE.equals(nFile[i].getStatType())) {
                continue;
            } else {
                if (bForwardMedia) {
                    srcFile = nFile[i].getTmpPath();
                    dstFile = nFile[i].getStoragePath();
                } else {
                    srcFile = nFile[i].getStoragePath();
                    dstFile = nFile[i].getTmpPath();
                }
                srcChannel = new FileInputStream(srcFile).getChannel();
                dstChannel = new FileOutputStream(dstFile).getChannel();
                dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
                if (bForwardMedia) {
                    new File(srcFile).delete();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        if (null != srcChannel && srcChannel.isOpen()) {
            srcChannel.close();
        }
        if (null != dstChannel && dstChannel.isOpen()) {
            dstChannel.close();
        }
    }
}

From source file:org.apache.solr.core.CoreContainer.java

/** Copies a src file to a dest file:
 *  used to circumvent the platform discrepancies regarding renaming files.
 *//*from ww w.  jav  a 2s .  com*/
public static void fileCopy(File src, File dest) throws IOException {
    IOException xforward = null;
    FileInputStream fis = null;
    FileOutputStream fos = null;
    FileChannel fcin = null;
    FileChannel fcout = null;
    try {
        fis = new FileInputStream(src);
        fos = new FileOutputStream(dest);
        fcin = fis.getChannel();
        fcout = fos.getChannel();
        // do the file copy 32Mb at a time
        final int MB32 = 32 * 1024 * 1024;
        long size = fcin.size();
        long position = 0;
        while (position < size) {
            position += fcin.transferTo(position, MB32, fcout);
        }
    } catch (IOException xio) {
        xforward = xio;
    } finally {
        if (fis != null)
            try {
                fis.close();
                fis = null;
            } catch (IOException xio) {
            }
        if (fos != null)
            try {
                fos.close();
                fos = null;
            } catch (IOException xio) {
            }
        if (fcin != null && fcin.isOpen())
            try {
                fcin.close();
                fcin = null;
            } catch (IOException xio) {
            }
        if (fcout != null && fcout.isOpen())
            try {
                fcout.close();
                fcout = null;
            } catch (IOException xio) {
            }
    }
    if (xforward != null) {
        throw xforward;
    }
}

From source file:org.geoserver.rest.util.IOUtils.java

/**
 * Optimize version of copy method for file channels.
 * //from w  w  w .  j  a  v a2s . co  m
 * @param bufferSize size of the temp buffer to use for this copy.
 * @param source the source {@link ReadableByteChannel}.
 * @param destination the destination {@link WritableByteChannel};.
 * @throws IOException in case something bad happens.
 */
public static void copyFileChannel(int bufferSize, FileChannel source, FileChannel destination)
        throws IOException {

    inputNotNull(source, destination);
    if (!source.isOpen() || !destination.isOpen())
        throw new IllegalStateException("Source and destination channels must be open.");
    FileLock lock = null;
    try {

        lock = destination.lock();
        final long sourceSize = source.size();
        long pos = 0;
        while (pos < sourceSize) {
            // read and flip
            final long remaining = (sourceSize - pos);
            final int mappedZoneSize = remaining >= bufferSize ? bufferSize : (int) remaining;
            destination.transferFrom(source, pos, mappedZoneSize);
            // update zone
            pos += mappedZoneSize;

        }
    } finally {
        if (lock != null) {
            try {
                lock.release();
            } catch (Throwable t) {
                if (LOGGER.isLoggable(Level.INFO))
                    LOGGER.log(Level.INFO, t.getLocalizedMessage(), t);
            }
        }

    }
}

From source file:org.red5.io.flv.FLVReader.java

/**
 * Creates FLV reader from file channel.
 *
 * @param channel// w  w  w.j a v a  2 s.com
 * @throws IOException on error
 */
public FLVReader(FileChannel channel) throws IOException {
    if (null == channel) {
        log.warn("Reader was passed a null channel");
        log.debug("{}", org.apache.commons.lang.builder.ToStringBuilder.reflectionToString(this));
    }
    if (!channel.isOpen()) {
        log.warn("Reader was passed a closed channel");
        return;
    }
    this.channel = channel;
    channelSize = channel.size();
    //log.debug("Channel size: {}", channelSize);
    if (channel.position() > 0) {
        log.debug("Channel position: {}", channel.position());
        channel.position(0);
    }
    fillBuffer();
    postInitialize();
}