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:org.lnicholls.galleon.util.Tools.java

public static void copy(File src, File dst) {
    try {/*from   ww  w  . ja va 2s .c om*/
        FileChannel srcChannel = new FileInputStream(src).getChannel();
        FileChannel dstChannel = new FileOutputStream(dst).getChannel();
        dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
        srcChannel.close();
        srcChannel = null;
        dstChannel.close();
        dstChannel = null;
    } catch (IOException ex) {
        Tools.logException(Tools.class, ex, dst.getAbsolutePath());
    }
}

From source file:com.almunt.jgcaap.systemupdater.DownloadService.java

public void copy(File src, File dst) throws IOException {
    FileInputStream inStream = new FileInputStream(src);
    FileOutputStream outStream = new FileOutputStream(dst);
    FileChannel inChannel = inStream.getChannel();
    FileChannel outChannel = outStream.getChannel();
    inChannel.transferTo(0, inChannel.size(), outChannel);
    inStream.close();//from  ww  w.  j a va2  s.c o  m
    outStream.close();
}

From source file:com.thinkberg.moxo.vfs.s3.jets3t.Jets3tFileObject.java

protected void doDetach() throws Exception {
    // TODO do not send immediately but put in some kind of upload queue
    if (attached && changed) {
        System.err.println("Detaching changed object: " + object);
        if (cacheFile != null) {
            FileChannel cacheFc = getCacheFileChannel();
            object.setContentLength(cacheFc.size());
            object.setDataInputStream(getInputStream());
        }//from ww w .ja  v  a 2 s  .  c  o m
        System.err.println(object);
        service.putObject(bucket, object);
        attached = false;
    }
}

From source file:org.cloudata.core.commitlog.FileTransferThread.java

private void transfer(FileChannel[] channelList, SocketChannel socketChannel) {
    for (FileChannel fc : channelList) {
        try {//w ww  .  j ava  2  s.c o m
            long numTransferred = 0;
            long pos = fc.position();
            long totalLength = fc.size() - pos;
            long count = totalLength;

            LOG.info(dirName + " start File Transfer:" + pos + "," + totalLength);
            while ((numTransferred += fc.transferTo(pos, count, socketChannel)) < totalLength) {
                pos += numTransferred;
                count -= numTransferred;
            }
            //LOG.info("End File Transfer:" + pos + "," + totalLength);
        } catch (IOException e) {
            LOG.warn("transfering file is fail due to : ", e);
        } finally {
            if (fc != null) {
                try {
                    fc.close();
                } catch (IOException e) {
                    LOG.warn("closing file is fail due to : ", e);
                }
            }
        }
    }
}

From source file:org.apache.tajo.tuple.offheap.OffHeapRowBlock.java

public boolean copyFromChannel(FileChannel channel, TableStats stats) throws IOException {
    if (channel.position() < channel.size()) {
        clear();//from   w  w  w  .j  a  v a  2  s  .  c o m

        buffer.clear();
        channel.read(buffer);
        memorySize = buffer.position();

        while (position < memorySize) {
            long recordPtr = address + position;

            if (remain() < SizeOf.SIZE_OF_INT) {
                channel.position(channel.position() - remain());
                memorySize = (int) (memorySize - remain());
                return true;
            }

            int recordSize = UNSAFE.getInt(recordPtr);

            if (remain() < recordSize) {
                channel.position(channel.position() - remain());
                memorySize = (int) (memorySize - remain());
                return true;
            }

            position += recordSize;
            rowNum++;
        }

        return true;
    } else {
        return false;
    }
}

From source file:com.geekandroid.sdk.sample.crop.ResultActivity.java

private void copyFileToDownloads(Uri croppedFileUri) throws Exception {
    String downloadsDirectoryPath = Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
    String filename = String.format("%d_%s", Calendar.getInstance().getTimeInMillis(),
            croppedFileUri.getLastPathSegment());

    File saveFile = new File(downloadsDirectoryPath, filename);

    FileInputStream inStream = new FileInputStream(new File(croppedFileUri.getPath()));
    FileOutputStream outStream = new FileOutputStream(saveFile);
    FileChannel inChannel = inStream.getChannel();
    FileChannel outChannel = outStream.getChannel();
    inChannel.transferTo(0, inChannel.size(), outChannel);
    inStream.close();/*from ww w .  j  a va2  s.c om*/
    outStream.close();

    showNotification(saveFile);
}

From source file:ar.com.init.agros.license.LicenseVerifier.java

private boolean isMasterLicense(String file) throws IOException, FileNotFoundException {
    File licenseFile = new File(file);
    FileChannel fc = (new FileInputStream(licenseFile)).getChannel();
    byte[] bytes = new byte[(int) fc.size()];
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    fc.read(bb);/*from   w ww .j a v  a2 s.  c o m*/
    String hash = DigestUtils.md5Hex(bytes);
    fc.close();
    boolean isMaster = hash.equals(MASTER_LICENSE_HASH);
    return isMaster;
}

From source file:org.lenskit.data.packed.BinaryRatingDAOTest.java

@Test
public void testBufferDAO() throws IOException {
    File file = folder.newFile("ratings.bin");
    BinaryRatingPacker packer = BinaryRatingPacker.open(file);
    try {/*from   w ww  .ja v  a  2 s  .  c  o m*/
        packer.writeRatings(ratings);
    } finally {
        packer.close();
    }

    ByteBuffer buffer;
    try (FileInputStream istr = new FileInputStream(file)) {
        FileChannel chan = istr.getChannel();
        buffer = chan.map(FileChannel.MapMode.READ_ONLY, 0, chan.size());
    }

    BinaryRatingDAO dao = BinaryRatingDAO.fromBuffer(buffer);
    verifySimpleDAO(dao);
}

From source file:com.sm.store.utils.FileStore.java

private boolean checkSignature(FileChannel channel) throws IOException {
    ByteBuffer intBytes = ByteBuffer.allocate(OFFSET);
    if (channel.size() == 0) {
        intBytes.putInt(MAGIC);/* w  w  w  .  java  2s.c  o m*/
        intBytes.flip();
        channel.write(intBytes);
        return true;
    } else {
        channel.read(intBytes);
        intBytes.rewind();
        if (intBytes.getInt() != MAGIC)
            throw new StoreException("Header mismatch expect " + MAGIC + " read " + intBytes.getInt());
    }
    return true;
}

From source file:org.openmrs.module.formentry.FormEntryUtil.java

/**
 * Generates an expanded 'starter XSN'. This starter is essentially a blank XSN template to play
 * with in Infopath. Should be used similar to
 * <code>org.openmrs.module.formentry.FormEntryUtil.expandXsnContents(java.lang.String)</code>
 * Generates an expanded 'starter XSN'. This starter is essentially a blank XSN template to play
 * with in Infopath. Should be used similar to
 * <code>org.openmrs.formentry.FormEntryUtil.expandXsnContents(java.lang.String)</code>
 * /*from   w w  w  . j a v a2s  .c o  m*/
 * @return File directory holding blank xsn contents
 * @throws IOException
 */
public static File getExpandedStarterXSN() throws IOException {

    // temp directory to hold the new xsn contents
    File tempDir = FormEntryUtil.createTempDirectory("XSN-starter");
    if (tempDir == null)
        throw new IOException("Failed to create temporary directory");

    // iterate over and copy each file in the given folder
    File starterDir = getResourceFile(FormEntryConstants.FORMENTRY_STARTER_XSN_FOLDER_PATH);
    for (File f : starterDir.listFiles()) {
        File newFile = new File(tempDir, f.getName());
        FileChannel in = null, out = null;
        try {
            in = new FileInputStream(f).getChannel();
            out = new FileOutputStream(newFile).getChannel();
            in.transferTo(0, in.size(), out);
        } finally {
            if (in != null)
                in.close();
            if (out != null)
                out.close();
        }
    }

    return tempDir;
}