Example usage for java.nio.channels FileChannel size

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

Introduction

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

Prototype

public abstract long size() throws IOException;

Source Link

Document

Returns the current size of this channel's file.

Usage

From source file:com.kkbox.toolkit.image.KKImageRequest.java

private void moveFileTo(String originalPath, String targetPath) {
    fileLock.lock();//from  www.  ja  v a  2s  . co m
    File targetFile = new File(targetPath);
    targetFile.delete();
    FileChannel originalChannel = null;
    FileChannel targetChannel = null;
    try {
        originalChannel = new FileInputStream(originalPath).getChannel();
        targetChannel = new FileOutputStream(targetPath).getChannel();
        originalChannel.transferTo(0, originalChannel.size(), targetChannel);
    } catch (IOException e) {
    }
    try {
        if (originalChannel != null)
            originalChannel.close();
        if (targetChannel != null)
            targetChannel.close();
    } catch (IOException e) {
    }
    fileLock.unlock();
}

From source file:org.apache.htrace.impl.HTracedSpanReceiver.java

void appendToDroppedSpansLog(String text) throws IOException {
    // Is the dropped spans log is disabled?
    if (conf.droppedSpansLogPath.isEmpty() || (conf.droppedSpansLogMaxSize == 0)) {
        return;//w  ww . ja va  2 s  .  co  m
    }
    FileLock lock = null;
    String msg = ISO_DATE_FORMAT.format(new Date()) + ": " + text;
    ByteBuffer bb = ByteBuffer.wrap(msg.getBytes(StandardCharsets.UTF_8));
    // FileChannel locking corresponds to advisory locking on UNIX.  It will
    // protect multiple processes from attempting to write to the same dropped
    // spans log at once.  However, within a single process, we need this
    // synchronized block to ensure that multiple HTracedSpanReceiver objects
    // don't try to write to the same log at once.  (It is unusal to configure
    // multiple HTracedSpanReceiver objects, but possible.)
    synchronized (HTracedSpanReceiver.class) {
        FileChannel channel = FileChannel.open(Paths.get(conf.droppedSpansLogPath), APPEND, CREATE, WRITE);
        try {
            lock = channel.lock();
            long size = channel.size();
            if (size > conf.droppedSpansLogMaxSize) {
                throw new IOException("Dropped spans log " + conf.droppedSpansLogPath + " is already " + size
                        + " bytes; will not add to it.");
            } else if ((size == 0) && (DROPPED_SPANS_FILE_PERMS != null)) {
                // Set the permissions of the dropped spans file so that other
                // processes can write to it.
                Files.setPosixFilePermissions(Paths.get(conf.droppedSpansLogPath), DROPPED_SPANS_FILE_PERMS);
            }
            channel.write(bb);
        } finally {
            try {
                if (lock != null) {
                    lock.release();
                }
            } finally {
                channel.close();
            }
        }
    }
}

From source file:org.jajuk.util.UtilSystem.java

/**
 * Save a file in the same directory with name <filename>_YYYYmmddHHMM.xml and
 * with a given maximum Mb size for the file and its backup files
 * /*from w w w  . ja  v a2 s  . co m*/
 * @param file The file to back up
 * @param iMB 
 */
public static void backupFile(final File file, final int iMB) {
    try {
        if (Integer.parseInt(Conf.getString(Const.CONF_BACKUP_SIZE)) <= 0) {
            // 0 or less means no backup
            return;
        }
        // calculates total size in MB for the file to backup and its
        // backup files
        long lUsedMB = 0;
        final List<File> alFiles = new ArrayList<File>(10);
        final File[] files = new File(file.getAbsolutePath()).getParentFile().listFiles();
        if (files != null) {
            for (final File element : files) {
                if (element.getName().indexOf(UtilSystem.removeExtension(file.getName())) != -1) {
                    lUsedMB += element.length();
                    alFiles.add(element);
                }
            }
            // sort found files
            alFiles.remove(file);
            Collections.sort(alFiles);
            // too much backup files, delete older
            if (((lUsedMB - file.length()) / 1048576 > iMB) && (alFiles.size() > 0)) {
                final File fileToDelete = alFiles.get(0);
                if (fileToDelete != null) { //NOSONAR
                    if (!fileToDelete.delete()) {
                        Log.warn("Could not delete file " + fileToDelete);
                    }
                }
            }
        }
        // backup itself using nio, file name is
        // collection-backup-yyyMMdd.xml
        final String sExt = new SimpleDateFormat("yyyyMMdd", Locale.getDefault()).format(new Date());
        final File fileNew = new File(UtilSystem.removeExtension(file.getAbsolutePath()) + "-backup-" + sExt
                + "." + UtilSystem.getExtension(file));
        final FileChannel fcSrc = new FileInputStream(file).getChannel();
        try {
            final FileChannel fcDest = new FileOutputStream(fileNew).getChannel();
            try {
                fcDest.transferFrom(fcSrc, 0, fcSrc.size());
            } finally {
                fcDest.close();
            }
        } finally {
            fcSrc.close();
        }
    } catch (final IOException ie) {
        Log.error(ie);
    }
}

From source file:org.apache.camel.component.file.FileOperations.java

private void writeFileByFile(File source, File target) throws IOException {
    FileChannel in = new FileInputStream(source).getChannel();
    FileChannel out = null;//  w ww .j  a  va 2 s  . c  om
    try {
        out = prepareOutputFileChannel(target, out);

        if (LOG.isTraceEnabled()) {
            LOG.trace("Using FileChannel to transfer from: " + in + " to: " + out);
        }

        long size = in.size();
        long position = 0;
        while (position < size) {
            position += in.transferTo(position, endpoint.getBufferSize(), out);
        }
    } finally {
        IOHelper.close(in, source.getName(), LOG);
        IOHelper.close(out, source.getName(), LOG);
    }
}

From source file:eu.prestoprime.plugin.p4.CopyTasks.java

@WfService(name = "master_file_first_copy", version = "2.0.0")
public void masterFileFirstCopy(Map<String, String> sParams, Map<String, String> dParamsString,
        Map<String, File> dParamsFile) throws TaskExecutionFailedException {

    // get sipID// w  w w  . j  av  a  2 s .  co  m
    String sipID = dParamsString.get("sipID");

    SIP sip = null;
    try {
        // get sip
        sip = P4DataManager.getInstance().getSIPByID(sipID);

        String inputVideo = null;

        // get MQ file
        String[] formats = sParams.get("MQformats").split(",");
        for (String format : formats) {

            List<String> videoFileList = sip.getAVMaterial(format, "FILE");
            if (videoFileList.size() > 0) {
                inputVideo = videoFileList.get(0);
                String outputVideoName = inputVideo.substring(inputVideo.lastIndexOf(File.separator) + 1);
                String outputVideo = dParamsString.get("videosFolder") + File.separator + outputVideoName;

                File inputFile = new File(inputVideo);
                File outputFile = new File(outputVideo);

                if (!outputFile.exists()) {
                    outputFile.createNewFile();
                }
                FileChannel source = new FileInputStream(inputFile).getChannel();
                FileChannel destination = new FileOutputStream(outputFile).getChannel();
                destination.transferFrom(source, 0, source.size());

                // MD5
                MessageDigestExtractor mde = new MessageDigestExtractor();
                ToolOutput<MessageDigestExtractor.AttributeType> output = mde.extract(outputVideo);
                String md5sum = output.getAttribute(MessageDigestExtractor.AttributeType.MD5);

                // update SIP
                sip.addFile(format, "FILE", outputVideo, md5sum, new File(outputVideo).length());

                break;
            }
        }
        if (inputVideo == null) {
            throw new TaskExecutionFailedException("Unable to find supported MQ format...");
        }
    } catch (DataException e) {
        e.printStackTrace();
        throw new TaskExecutionFailedException("Unable to retrieve the SIP...");
    } catch (IPException e) {
        e.printStackTrace();
        throw new TaskExecutionFailedException("Unable to retrieve MQ file from SIP...");
    } catch (IOException e) {
        e.printStackTrace();
        throw new TaskExecutionFailedException("Unable to copy MQ file...");
    } catch (ToolException e) {
        e.printStackTrace();
        throw new TaskExecutionFailedException("Unable to compute MD5 for WebM video file...");
    } finally {
        P4DataManager.getInstance().releaseIP(sip);
    }
}

From source file:eu.prestoprime.plugin.p4.CopyTasks.java

@WfService(name = "master_file_second_copy", version = "2.0.0")
public void masterFileSecondCopy(Map<String, String> sParams, Map<String, String> dParamsString,
        Map<String, File> dParamsFile) throws TaskExecutionFailedException {

    // get sipID/*from  w  ww  .jav a 2  s.c o m*/
    String sipID = dParamsString.get("sipID");

    SIP sip = null;
    try {
        // get sip
        sip = P4DataManager.getInstance().getSIPByID(sipID);

        String inputVideo = null;

        // get MQ file
        String[] formats = sParams.get("MQformats").split(",");
        for (String format : formats) {

            List<String> videoFileList = sip.getAVMaterial(format, "FILE");
            if (videoFileList.size() > 0) {
                inputVideo = videoFileList.get(0);

                String outputFolder = sParams.get("second.local.copy.folder") + File.separator + sipID;
                new File(outputFolder).mkdirs();
                String outputVideoName = inputVideo.substring(inputVideo.lastIndexOf(File.separator) + 1);
                String outputVideo = outputFolder + File.separator + outputVideoName;

                File inputFile = new File(inputVideo);
                File outputFile = new File(outputVideo);

                if (!outputFile.exists()) {
                    outputFile.createNewFile();
                }
                FileChannel source = new FileInputStream(inputFile).getChannel();
                FileChannel destination = new FileOutputStream(outputFile).getChannel();
                destination.transferFrom(source, 0, source.size());

                // MD5
                MessageDigestExtractor mde = new MessageDigestExtractor();
                ToolOutput<MessageDigestExtractor.AttributeType> output = mde.extract(outputVideo);
                String md5sum = output.getAttribute(MessageDigestExtractor.AttributeType.MD5);

                // update SIP
                sip.addFile(format, "FILE-BCK", outputVideo, md5sum, new File(outputVideo).length());

                break;
            }
        }
        if (inputVideo == null) {
            throw new TaskExecutionFailedException("Unable to find supported MQ format...");
        }
    } catch (DataException e) {
        e.printStackTrace();
        throw new TaskExecutionFailedException("Unable to retrieve the SIP...");
    } catch (IPException e) {
        e.printStackTrace();
        throw new TaskExecutionFailedException("Unable to retrieve MQ file from SIP...");
    } catch (IOException e) {
        e.printStackTrace();
        throw new TaskExecutionFailedException("Unable to copy MQ file...");
    } catch (ToolException e) {
        e.printStackTrace();
        throw new TaskExecutionFailedException("Unable to compute MD5 for WebM video file...");
    } finally {
        P4DataManager.getInstance().releaseIP(sip);
    }
}

From source file:org.carbondata.processing.store.writer.AbstractFactDataWriter.java

/**
 * This method will write metadata at the end of file file format in thrift format
 *//*from www. j  a v a  2s.  c  o m*/
protected void writeleafMetaDataToFile(List<BlockletInfoColumnar> infoList, FileChannel channel,
        String filePath) throws CarbonDataWriterException {
    try {
        long currentPosition = channel.size();
        CarbonFooterWriter writer = new CarbonFooterWriter(filePath);
        FileFooter convertFileMeta = CarbonMetadataUtil.convertFileFooter(infoList, localCardinality.length,
                localCardinality, thriftColumnSchemaList);
        writer.writeFooter(convertFileMeta, currentPosition);
    } catch (IOException e) {
        throw new CarbonDataWriterException("Problem while writing the carbon file: ", e);
    }

}

From source file:eu.europa.esig.dss.DSSUtils.java

public static void copyFile(final String path, final File sourceFile, final File destinationFile)
        throws IOException {

    final File destinationPath = new File(path);
    if (!destinationPath.exists()) {
        destinationPath.mkdirs();//from   www . j  a va  2  s .c om
        destinationFile.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;

    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destinationFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

From source file:com.symbian.driver.plugins.romflash.Activator.java

public boolean FlashRom(String romLocation) {

    try {//from   ww w .  j a v a2  s . com
        File rom = new File(romLocation);
        if (method.compareToIgnoreCase("serial") == 0) {
            if (!(switchOff() && switchOn())) {
                LOGGER.log(Level.SEVERE, "Could not reboot device, so No rom flashing.");
                return false;
            }
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                LOGGER.log(Level.SEVERE, "Failed to wait for sync.exe to finish.");
            }
            File trgtTestFile = JarUtils.extractResource(Activator.class, "/resource/trgtest.exe");
            if (trgtTestFile.isFile()) {
                ProcessBuilder ld = new ProcessBuilder(trgtTestFile.getAbsolutePath(), portNumber,
                        rom.getCanonicalPath());
                ld.directory(trgtTestFile.getParentFile());
                Process pp = ld.start();
                LOGGER.log(Level.INFO, "started trgtest process");
                BufferedReader br = new BufferedReader(new InputStreamReader(pp.getInputStream()));
                StringBuffer sb = new StringBuffer();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line).append("\n");
                    LOGGER.log(Level.INFO, line);
                }
                //String answer = sb.toString();

                try {
                    LOGGER.log(Level.INFO, "going to wait now for trgtest to finish");
                    pp.waitFor();
                } catch (InterruptedException e) {
                    LOGGER.log(Level.SEVERE, "Failed to wait for trgtest.exe to finish.");
                }
                LOGGER.log(Level.INFO, "trgtest returned: " + pp.exitValue());
                pp.destroy();

            } else {
                LOGGER.log(Level.SEVERE, "Could not find trgtest.exe file.");
            }
        } else // usb rom loading
        {
            // switch the device off...
            switchOff();
            List<File> lis1 = Arrays.asList(File.listRoots());
            // get reboot plugin, and reboot the device
            switchOn(); // or just switch on as things may be
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                LOGGER.log(Level.SEVERE, "Failed to wait for sync.exe to finish.");
            }
            File[] listRoots2 = File.listRoots();
            // find the drive that made the difference!!
            File diff = null;
            for (File root : listRoots2) {
                if (!lis1.contains(root)) // found new drive
                {
                    diff = root;
                    break;
                }
            }
            File romfl = new File(diff, rom.getName());
            romfl.delete();
            File aCopyFrom = new File(rom.getCanonicalPath());
            FileChannel lSrcChannel = new FileInputStream(aCopyFrom).getChannel();
            FileChannel lDstChannel = new FileOutputStream(romfl).getChannel();
            lDstChannel.transferFrom(lSrcChannel, 0, lSrcChannel.size());
            lSrcChannel.close();
            lDstChannel.close();

            File syncFile = JarUtils.extractResource(Activator.class, "/resource/sync.exe");
            if (syncFile.isFile()) {

                ProcessBuilder ld = new ProcessBuilder(syncFile.getAbsolutePath(), "-r", "-e", diff.toString());

                ld.directory(syncFile.getParentFile());
                ld.start();
                // wait 10 seconds for the rom to load ...
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    LOGGER.log(Level.SEVERE, "Failed to wait for sync.exe to finish.");
                }
            } else {
                LOGGER.log(Level.SEVERE, "Could not find sync.exe file.");
            }
        }
    } catch (IOException lIOException) {
        LOGGER.log(Level.SEVERE, "Could not flash ROM " + lIOException.getMessage());
        return false;
    }
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        LOGGER.log(Level.SEVERE, "Failed to wait for sync.exe to finish.");
    }
    return true;
}

From source file:org.apache.hadoop.hdfs.server.namenode.TestListCorruptFileBlocks.java

/**
 * Check that listCorruptFileBlocks works while the namenode is still in safemode.
 *//*from  w ww. j a  v a  2 s.c o m*/
@Test(timeout = 300000)
public void testListCorruptFileBlocksInSafeMode() throws Exception {
    MiniDFSCluster cluster = null;
    Random random = new Random();

    try {
        Configuration conf = new HdfsConfiguration();
        // datanode scans directories
        conf.setInt(DFSConfigKeys.DFS_DATANODE_DIRECTORYSCAN_INTERVAL_KEY, 1);
        // datanode sends block reports
        conf.setInt(DFSConfigKeys.DFS_BLOCKREPORT_INTERVAL_MSEC_KEY, 3 * 1000);
        // never leave safemode automatically
        conf.setFloat(DFSConfigKeys.DFS_NAMENODE_SAFEMODE_THRESHOLD_PCT_KEY, 1.5f);
        // start populating repl queues immediately 
        conf.setFloat(DFSConfigKeys.DFS_NAMENODE_REPL_QUEUE_THRESHOLD_PCT_KEY, 0f);
        // Set short retry timeouts so this test runs faster
        conf.setInt(DFSConfigKeys.DFS_CLIENT_RETRY_WINDOW_BASE, 10);
        cluster = new MiniDFSCluster.Builder(conf).waitSafeMode(false).build();
        cluster.getNameNodeRpc().setSafeMode(HdfsConstants.SafeModeAction.SAFEMODE_LEAVE, false);
        FileSystem fs = cluster.getFileSystem();

        // create two files with one block each
        DFSTestUtil util = new DFSTestUtil.Builder().setName("testListCorruptFileBlocksInSafeMode")
                .setNumFiles(2).setMaxLevels(1).setMaxSize(512).build();
        util.createFiles(fs, "/srcdat10");

        // fetch bad file list from namenode. There should be none.
        Collection<FSNamesystem.CorruptFileBlockInfo> badFiles = cluster.getNameNode().getNamesystem()
                .listCorruptFileBlocks("/", null);
        assertTrue("Namenode has " + badFiles.size() + " corrupt files. Expecting None.", badFiles.size() == 0);

        // Now deliberately corrupt one block
        File storageDir = cluster.getInstanceStorageDir(0, 0);
        File data_dir = MiniDFSCluster.getFinalizedDir(storageDir, cluster.getNamesystem().getBlockPoolId());
        assertTrue("data directory does not exist", data_dir.exists());
        List<File> metaFiles = MiniDFSCluster.getAllBlockMetadataFiles(data_dir);
        assertTrue("Data directory does not contain any blocks or there was an " + "IO error",
                metaFiles != null && !metaFiles.isEmpty());
        File metaFile = metaFiles.get(0);
        RandomAccessFile file = new RandomAccessFile(metaFile, "rw");
        FileChannel channel = file.getChannel();
        long position = channel.size() - 2;
        int length = 2;
        byte[] buffer = new byte[length];
        random.nextBytes(buffer);
        channel.write(ByteBuffer.wrap(buffer), position);
        file.close();
        LOG.info("Deliberately corrupting file " + metaFile.getName() + " at offset " + position + " length "
                + length);

        // read all files to trigger detection of corrupted replica
        try {
            util.checkFiles(fs, "/srcdat10");
        } catch (BlockMissingException e) {
            System.out.println("Received BlockMissingException as expected.");
        } catch (IOException e) {
            assertTrue("Corrupted replicas not handled properly. " + "Expecting BlockMissingException "
                    + " but received IOException " + e, false);
        }

        // fetch bad file list from namenode. There should be one file.
        badFiles = cluster.getNameNode().getNamesystem().listCorruptFileBlocks("/", null);
        LOG.info("Namenode has bad files. " + badFiles.size());
        assertTrue("Namenode has " + badFiles.size() + " bad files. Expecting 1.", badFiles.size() == 1);

        // restart namenode
        cluster.restartNameNode(0);
        fs = cluster.getFileSystem();

        // wait until replication queues have been initialized
        while (!cluster.getNameNode().namesystem.isPopulatingReplQueues()) {
            try {
                LOG.info("waiting for replication queues");
                Thread.sleep(1000);
            } catch (InterruptedException ignore) {
            }
        }

        // read all files to trigger detection of corrupted replica
        try {
            util.checkFiles(fs, "/srcdat10");
        } catch (BlockMissingException e) {
            System.out.println("Received BlockMissingException as expected.");
        } catch (IOException e) {
            assertTrue("Corrupted replicas not handled properly. " + "Expecting BlockMissingException "
                    + " but received IOException " + e, false);
        }

        // fetch bad file list from namenode. There should be one file.
        badFiles = cluster.getNameNode().getNamesystem().listCorruptFileBlocks("/", null);
        LOG.info("Namenode has bad files. " + badFiles.size());
        assertTrue("Namenode has " + badFiles.size() + " bad files. Expecting 1.", badFiles.size() == 1);

        // check that we are still in safe mode
        assertTrue("Namenode is not in safe mode", cluster.getNameNode().isInSafeMode());

        // now leave safe mode so that we can clean up
        cluster.getNameNodeRpc().setSafeMode(HdfsConstants.SafeModeAction.SAFEMODE_LEAVE, false);

        util.cleanup(fs, "/srcdat10");
    } catch (Exception e) {
        LOG.error(StringUtils.stringifyException(e));
        throw e;
    } finally {
        if (cluster != null) {
            cluster.shutdown();
        }
    }
}