Example usage for java.nio.channels FileChannel truncate

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

Introduction

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

Prototype

public abstract FileChannel truncate(long size) throws IOException;

Source Link

Document

Truncates this channel's file to the given size.

Usage

From source file:edu.rit.flick.genetics.FastFileDeflator.java

@SuppressWarnings("resource")
protected void removeUnusedBufferSpace(final String tmpOutputDirectory)
        throws IOException, InterruptedException {
    final long actualDataHcfFileSize = datahcf.position();
    final long actualNFileSize = nfile.position();

    fastIn.close();//from www.  ja v a  2  s  .  c o  m
    datahcf.close();
    headerfile.close();
    nfile.close();
    iupacfile.close();
    tailfile.close();
    metafile.close();

    fastIn = null;
    datahcf = null;
    nfile = null;

    // Give the last method a moment to garbage collect
    // System.gc();
    // Thread.sleep( 1000 );

    final File dataFile = new File(tmpOutputDirectory + SEQUENCE_DATA_FILE);
    final File nFile = new File(tmpOutputDirectory + N_FILE);

    // Remove unused buffer space
    FileChannel fc = new FileOutputStream(dataFile, true).getChannel();
    fc.force(true);
    fc.truncate(actualDataHcfFileSize).close();

    fc = new FileOutputStream(nFile, true).getChannel();
    fc.force(true);
    fc.truncate(actualNFileSize).close();
}

From source file:com.remobile.file.LocalFilesystem.java

@Override
public long truncateFileAtURL(LocalFilesystemURL inputURL, long size) throws IOException {
    File file = new File(filesystemPathForURL(inputURL));

    if (!file.exists()) {
        throw new FileNotFoundException("File at " + inputURL.uri + " does not exist.");
    }//from  w  ww .j  a v  a  2 s .  c  o  m

    RandomAccessFile raf = new RandomAccessFile(filesystemPathForURL(inputURL), "rw");
    try {
        if (raf.length() >= size) {
            FileChannel channel = raf.getChannel();
            channel.truncate(size);
            return size;
        }

        return raf.length();
    } finally {
        raf.close();
    }

}

From source file:org.alfresco.filesys.repo.CommandExecutorImpl.java

/**
 * @param sess SrvSession//from www  .j  av a  2s . c om
 * @param tree TreeConnection
 * @param command Command
 * @param result Object
 * @return Object
 * @throws IOException
 */
private Object executeInternal(SrvSession sess, TreeConnection tree, Command command, Object result)
        throws IOException {
    FileFilterMode.setClient(ClientHelper.getClient(sess));
    try {
        if (command instanceof CompoundCommand) {
            Object ret = null;
            logger.debug("compound command received");
            CompoundCommand x = (CompoundCommand) command;

            for (Command compoundPart : x.getCommands()) {
                logger.debug("running part of compound command");
                Object val = executeInternal(sess, tree, compoundPart, result);
                if (val != null) {
                    // Return the value from the last command.
                    ret = val;
                }
            }
            return ret;
        } else if (command instanceof CreateFileCommand) {
            logger.debug("create file command");
            CreateFileCommand create = (CreateFileCommand) command;
            return repositoryDiskInterface.createFile(create.getRootNode(), create.getPath(),
                    create.getAllocationSize(), create.isHidden());
        } else if (command instanceof RestoreFileCommand) {
            logger.debug("restore file command");
            RestoreFileCommand restore = (RestoreFileCommand) command;
            return repositoryDiskInterface.restoreFile(sess, tree, restore.getRootNode(), restore.getPath(),
                    restore.getAllocationSize(), restore.getOriginalNodeRef());
        } else if (command instanceof DeleteFileCommand) {
            logger.debug("delete file command");
            DeleteFileCommand delete = (DeleteFileCommand) command;
            return repositoryDiskInterface.deleteFile2(sess, tree, delete.getRootNode(), delete.getPath());
        } else if (command instanceof OpenFileCommand) {
            logger.debug("open file command");
            OpenFileCommand o = (OpenFileCommand) command;

            OpenFileMode mode = o.getMode();
            return repositoryDiskInterface.openFile(sess, tree, o.getRootNodeRef(), o.getPath(), mode,
                    o.isTruncate());

        } else if (command instanceof CloseFileCommand) {
            logger.debug("close file command");
            CloseFileCommand c = (CloseFileCommand) command;
            return repositoryDiskInterface.closeFile(tree, c.getRootNodeRef(), c.getPath(), c.getNetworkFile());
        } else if (command instanceof ReduceQuotaCommand) {
            logger.debug("reduceQuota file command");
            ReduceQuotaCommand r = (ReduceQuotaCommand) command;
            repositoryDiskInterface.reduceQuota(sess, tree, r.getNetworkFile());
        } else if (command instanceof RenameFileCommand) {
            logger.debug("rename command");
            RenameFileCommand rename = (RenameFileCommand) command;

            repositoryDiskInterface.renameFile(rename.getRootNode(), rename.getFromPath(), rename.getToPath(),
                    rename.isSoft(), false);
        } else if (command instanceof MoveFileCommand) {
            logger.debug("move command");
            MoveFileCommand move = (MoveFileCommand) command;
            repositoryDiskInterface.renameFile(move.getRootNode(), move.getFromPath(), move.getToPath(), false,
                    move.isMoveAsSystem());
        } else if (command instanceof CopyContentCommand) {
            if (logger.isDebugEnabled()) {
                logger.debug("Copy content command - copy content");
            }
            CopyContentCommand copy = (CopyContentCommand) command;
            repositoryDiskInterface.copyContent(copy.getRootNode(), copy.getFromPath(), copy.getToPath());
        } else if (command instanceof DoNothingCommand) {
            if (logger.isDebugEnabled()) {
                logger.debug("Do Nothing Command - doing nothing");
            }
        } else if (command instanceof ResultCallback) {
            if (logger.isDebugEnabled()) {
                logger.debug("Result Callback");
            }
            ResultCallback callback = (ResultCallback) command;
            callback.execute(result);
        } else if (command instanceof RemoveTempFileCommand) {
            RemoveTempFileCommand r = (RemoveTempFileCommand) command;
            if (logger.isDebugEnabled()) {
                logger.debug("Remove Temp File:" + r.getNetworkFile());
            }
            File file = r.getNetworkFile().getFile();
            boolean isDeleted = file.delete();

            if (!isDeleted) {
                logger.debug("unable to delete temp file:" + r.getNetworkFile() + ", closed="
                        + r.getNetworkFile().isClosed());

                /*
                 * Unable to delete temporary file
                 * Could be a bug with the file handle not being closed, but yourkit does not
                 * find anything awry.
                 * There are reported Windows JVM bugs such as 4715154 ... 
                 */
                FileOutputStream fos = new FileOutputStream(file);
                FileChannel outChan = null;
                try {
                    outChan = fos.getChannel();
                    outChan.truncate(0);
                } catch (IOException e) {
                    logger.debug("unable to clean up file", e);
                } finally {
                    if (outChan != null) {
                        try {
                            outChan.close();
                        } catch (IOException e) {
                        }
                    }
                    fos.close();
                }
            }

        } else if (command instanceof ReturnValueCommand) {
            ReturnValueCommand r = (ReturnValueCommand) command;
            if (logger.isDebugEnabled()) {
                logger.debug("Return value");
            }
            return r.getReturnValue();
        } else if (command instanceof RemoveNoContentFileOnError) {
            RemoveNoContentFileOnError r = (RemoveNoContentFileOnError) command;
            if (logger.isDebugEnabled()) {
                logger.debug("Remove no content file on error");
            }
            repositoryDiskInterface.deleteEmptyFile(r.getRootNodeRef(), r.getPath());
        }
    } finally {
        FileFilterMode.clearClient();
    }

    return null;
}

From source file:org.apache.hadoop.hdfs.TestCrcCorruption.java

/** 
  * check if DFS can handle corrupted CRC blocks
  *//* w ww .j  av a2s .  c o  m*/
private void thistest(Configuration conf, DFSTestUtil util) throws Exception {
    MiniDFSCluster cluster = null;
    int numDataNodes = 2;
    short replFactor = 2;
    Random random = new Random();

    try {
        cluster = new MiniDFSCluster.Builder(conf).numNameNodes(1).numDataNodes(numDataNodes).build();
        cluster.waitActive();
        FileSystem fs = cluster.getFileSystem();
        util.createFiles(fs, "/srcdat", replFactor);
        util.waitReplication(fs, "/srcdat", (short) 2);

        // Now deliberately remove/truncate meta blocks from the first
        // directory of the first datanode. The complete absense of a meta
        // file disallows this Datanode to send data to another datanode.
        // However, a client is alowed access to this block.
        //
        File storageDir = MiniDFSCluster.getStorageDir(0, 1);
        String bpid = cluster.getNamesystem().getBlockPoolId();
        File data_dir = MiniDFSCluster.getFinalizedDir(storageDir, bpid);
        assertTrue("data directory does not exist", data_dir.exists());
        File[] blocks = data_dir.listFiles();
        assertTrue("Blocks do not exist in data-dir", (blocks != null) && (blocks.length > 0));
        int num = 0;
        for (int idx = 0; idx < blocks.length; idx++) {
            if (blocks[idx].getName().startsWith("blk_") && blocks[idx].getName().endsWith(".meta")) {
                num++;
                if (num % 3 == 0) {
                    //
                    // remove .meta file
                    //
                    LOG.info("Deliberately removing file " + blocks[idx].getName());
                    assertTrue("Cannot remove file.", blocks[idx].delete());
                } else if (num % 3 == 1) {
                    //
                    // shorten .meta file
                    //
                    RandomAccessFile file = new RandomAccessFile(blocks[idx], "rw");
                    FileChannel channel = file.getChannel();
                    int newsize = random.nextInt((int) channel.size() / 2);
                    LOG.info("Deliberately truncating file " + blocks[idx].getName() + " to size " + newsize
                            + " bytes.");
                    channel.truncate(newsize);
                    file.close();
                } else {
                    //
                    // corrupt a few bytes of the metafile
                    //
                    RandomAccessFile file = new RandomAccessFile(blocks[idx], "rw");
                    FileChannel channel = file.getChannel();
                    long position = 0;
                    //
                    // The very first time, corrupt the meta header at offset 0
                    //
                    if (num != 2) {
                        position = (long) random.nextInt((int) channel.size());
                    }
                    int length = random.nextInt((int) (channel.size() - position + 1));
                    byte[] buffer = new byte[length];
                    random.nextBytes(buffer);
                    channel.write(ByteBuffer.wrap(buffer), position);
                    LOG.info("Deliberately corrupting file " + blocks[idx].getName() + " at offset " + position
                            + " length " + length);
                    file.close();
                }
            }
        }

        //
        // Now deliberately corrupt all meta blocks from the second
        // directory of the first datanode
        //
        storageDir = MiniDFSCluster.getStorageDir(0, 1);
        data_dir = MiniDFSCluster.getFinalizedDir(storageDir, bpid);
        assertTrue("data directory does not exist", data_dir.exists());
        blocks = data_dir.listFiles();
        assertTrue("Blocks do not exist in data-dir", (blocks != null) && (blocks.length > 0));

        int count = 0;
        File previous = null;
        for (int idx = 0; idx < blocks.length; idx++) {
            if (blocks[idx].getName().startsWith("blk_") && blocks[idx].getName().endsWith(".meta")) {
                //
                // Move the previous metafile into the current one.
                //
                count++;
                if (count % 2 == 0) {
                    LOG.info("Deliberately insertimg bad crc into files " + blocks[idx].getName() + " "
                            + previous.getName());
                    assertTrue("Cannot remove file.", blocks[idx].delete());
                    assertTrue("Cannot corrupt meta file.", previous.renameTo(blocks[idx]));
                    assertTrue("Cannot recreate empty meta file.", previous.createNewFile());
                    previous = null;
                } else {
                    previous = blocks[idx];
                }
            }
        }

        //
        // Only one replica is possibly corrupted. The other replica should still
        // be good. Verify.
        //
        assertTrue("Corrupted replicas not handled properly.", util.checkFiles(fs, "/srcdat"));
        LOG.info("All File still have a valid replica");

        //
        // set replication factor back to 1. This causes only one replica of
        // of each block to remain in HDFS. The check is to make sure that 
        // the corrupted replica generated above is the one that gets deleted.
        // This test is currently disabled until HADOOP-1557 is solved.
        //
        util.setReplication(fs, "/srcdat", (short) 1);
        //util.waitReplication(fs, "/srcdat", (short)1);
        //LOG.info("All Files done with removing replicas");
        //assertTrue("Excess replicas deleted. Corrupted replicas found.",
        //           util.checkFiles(fs, "/srcdat"));
        LOG.info("The excess-corrupted-replica test is disabled " + " pending HADOOP-1557");

        util.cleanup(fs, "/srcdat");
    } finally {
        if (cluster != null) {
            cluster.shutdown();
        }
    }
}

From source file:org.apache.hadoop.hdfs.server.datanode.TestDirectoryScanner.java

/**
 * Truncate a block file//from  w  w  w.j av  a  2  s  . c  o  m
 */
private long truncateBlockFile() throws IOException {
    synchronized (fds) {
        for (ReplicaInfo b : FsDatasetTestUtil.getReplicas(fds, bpid)) {
            File f = b.getBlockFile();
            File mf = b.getMetaFile();
            // Truncate a block file that has a corresponding metadata file
            if (f.exists() && f.length() != 0 && mf.exists()) {
                FileOutputStream s = null;
                FileChannel channel = null;
                try {
                    s = new FileOutputStream(f);
                    channel = s.getChannel();
                    channel.truncate(0);
                    LOG.info("Truncated block file " + f.getAbsolutePath());
                    return b.getBlockId();
                } finally {
                    IOUtils.cleanup(LOG, channel, s);
                }
            }
        }
    }
    return 0;
}

From source file:org.apache.bookkeeper.bookie.BookieJournalTest.java

private void writePartialIndexFileForLedger(File indexDir, long ledgerId, byte[] masterKey,
        boolean truncateToMasterKey) throws Exception {
    File fn = new File(indexDir, IndexPersistenceMgr.getLedgerName(ledgerId));
    fn.getParentFile().mkdirs();//  w w  w .j a  va2s.com
    FileInfo fi = new FileInfo(fn, masterKey);
    // force creation of index file
    fi.write(new ByteBuffer[] { ByteBuffer.allocate(0) }, 0);
    fi.close(true);
    // file info header
    int headerLen = 8 + 4 + masterKey.length;
    // truncate the index file
    int leftSize;
    if (truncateToMasterKey) {
        leftSize = r.nextInt(headerLen);
    } else {
        leftSize = headerLen + r.nextInt(1024 - headerLen);
    }
    FileChannel fc = new RandomAccessFile(fn, "rw").getChannel();
    fc.truncate(leftSize);
    fc.close();
}

From source file:com.warfrog.bitmapallthethings.BattEngine.java

private void decodeBitmap(String filename) throws IOException {
    System.out.println("Decoding " + filename);

    File inputFile = new File(filename);
    File outputFile = new File(
            outputDirectory + File.separator + FilenameUtils.removeExtension(inputFile.getName()));

    FileInputStream fis = new FileInputStream(filename);
    //skip 6 bytes
    fis.skip(6);/*from   w  w  w . ja  v a 2s .  co m*/
    //read the length we encoded
    int fileSize = EndianUtils.readSwappedInteger(fis);
    //skip the rest of the header
    fis.skip(44);
    Files.copy(fis, outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    //truncate the file
    FileChannel outChan = new FileOutputStream(outputFile, true).getChannel();
    outChan.truncate(fileSize);
    outChan.close();

    //clean up
    if (isCleanUp()) {
        //delete the bitmap
        System.out.println("Deleting: " + inputFile);
        FileUtils.deleteQuietly(inputFile);
    }
}

From source file:com.pheromone.plugins.FileUtils.java

/**
 * Truncate the file to size//from  w w  w.  j  a  va2s  . c o m
 *
 * @param filename
 * @param size
 * @throws FileNotFoundException, IOException
 */
private long truncateFile(String filename, long size) throws FileNotFoundException, IOException {
    RandomAccessFile raf = new RandomAccessFile(filename, "rw");

    if (raf.length() >= size) {
        FileChannel channel = raf.getChannel();
        channel.truncate(size);
        return size;
    }

    return raf.length();
}

From source file:com.MustacheMonitor.MustacheMonitor.FileUtils.java

/**
 * Truncate the file to size/*  www.  j a va 2  s . c  om*/
 *
 * @param filename
 * @param size
 * @throws FileNotFoundException, IOException
 */
private long truncateFile(String filename, long size) throws FileNotFoundException, IOException {
    filename = stripFileProtocol(filename);

    RandomAccessFile raf = new RandomAccessFile(filename, "rw");

    if (raf.length() >= size) {
        FileChannel channel = raf.getChannel();
        channel.truncate(size);
        return size;
    }

    return raf.length();
}

From source file:org.apache.cordova.core.FileUtils.java

/**
 * Truncate the file to size/*from  w  w  w  . j  a  va 2 s  .  c om*/
 *
 * @param filename
 * @param size
 * @throws FileNotFoundException, IOException
 * @throws NoModificationAllowedException
 */
private long truncateFile(String filename, long size)
        throws FileNotFoundException, IOException, NoModificationAllowedException {
    if (filename.startsWith("content://")) {
        throw new NoModificationAllowedException("Couldn't truncate file given its content URI");
    }

    filename = FileHelper.getRealPath(filename, cordova);

    RandomAccessFile raf = new RandomAccessFile(filename, "rw");
    try {
        if (raf.length() >= size) {
            FileChannel channel = raf.getChannel();
            channel.truncate(size);
            return size;
        }

        return raf.length();
    } finally {
        raf.close();
    }
}