Example usage for java.io RandomAccessFile setLength

List of usage examples for java.io RandomAccessFile setLength

Introduction

In this page you can find the example usage for java.io RandomAccessFile setLength.

Prototype

public native void setLength(long newLength) throws IOException;

Source Link

Document

Sets the length of this file.

Usage

From source file:org.apache.pig.test.TestJobControlCompiler.java

public static POLoad createPOLoadWithSize(long size, LoadFunc loadFunc) throws Exception {
    File file = File.createTempFile("tempFile", ".tmp");
    file.deleteOnExit();/* w w w  . j  a  va 2  s.c  o m*/
    RandomAccessFile f = new RandomAccessFile(file, "rw");
    f.setLength(size);
    f.close();

    loadFunc.setLocation(file.getAbsolutePath(), new org.apache.hadoop.mapreduce.Job(CONF));
    FuncSpec funcSpec = new FuncSpec(loadFunc.getClass().getCanonicalName());
    POLoad poLoad = new POLoad(new OperatorKey(), loadFunc);
    poLoad.setLFile(new FileSpec(file.getAbsolutePath(), funcSpec));
    poLoad.setPc(new PigContext());
    poLoad.setUp();

    return poLoad;
}

From source file:org.nuxeo.ecm.core.blob.binary.LocalBinaryManager.java

/**
 * Sets the last modification date to now on a file
 *
 * @param file the file/*w  w w .java2s. c o m*/
 */
public static void touch(File file) {
    long time = System.currentTimeMillis();
    if (file.setLastModified(time)) {
        // ok
        return;
    }
    if (!file.canWrite()) {
        // cannot write -> stop won't be able to delete anyway
        return;
    }
    try {
        // Windows: the file may be open for reading
        // workaround found by Thomas Mueller, see JCR-2872
        RandomAccessFile r = new RandomAccessFile(file, "rw");
        try {
            r.setLength(r.length());
        } finally {
            r.close();
        }
    } catch (IOException e) {
        log.error("Cannot set last modified for file: " + file, e);
    }
}

From source file:org.apache.hadoop.dfs.TestDatanodeBlockScanner.java

private static void truncateReplica(String blockName, int dnIndex) throws IOException {
    File baseDir = new File(System.getProperty("test.build.data"), "dfs/data");
    for (int i = dnIndex * 2; i < dnIndex * 2 + 2; i++) {
        File blockFile = new File(baseDir, "data" + (i + 1) + "/current/" + blockName);
        if (blockFile.exists()) {
            RandomAccessFile raFile = new RandomAccessFile(blockFile, "rw");
            raFile.setLength(raFile.length() - 1);
            raFile.close();/*from w  w w.j av a 2  s. co m*/
            break;
        }
    }
}

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

/**
 * Change the length of a block at datanode dnIndex
 *///from  w  ww.ja v a2 s .  co m
static boolean changeReplicaLength(String blockName, int dnIndex, int lenDelta) throws IOException {
    File baseDir = new File(System.getProperty("test.build.data"), "dfs/data");
    for (int i = dnIndex * 2; i < dnIndex * 2 + 2; i++) {
        File blockFile = new File(baseDir, "data" + (i + 1) + "/current/" + blockName);
        if (blockFile.exists()) {
            RandomAccessFile raFile = new RandomAccessFile(blockFile, "rw");
            raFile.setLength(raFile.length() + lenDelta);
            raFile.close();
            return true;
        }
    }
    return false;
}

From source file:org.hadoop.tdg.TestPseudoHadoop.java

@BeforeClass
public static void setUpClass() throws IOException {
    RandomAccessFile f = null;
    try {/*from  ww w.j  a  v a 2  s.c  o m*/
        /*f = new File(HOME_FILE);
        FileOutputStream out = new FileOutputStream(f);
        out.write("content".getBytes("UTF-8"));
        out.flush();*/
        f = new RandomAccessFile(HOME_FILE, "rw");
        f.setLength(SIZE);
    } finally {
        IOUtils.closeStream(f);
    }

}

From source file:org.apache.jackrabbit.core.data.FileDataStore.java

/**
 * Set the last modified date of a file, if the file is writable.
 *
 * @param file the file/*from   ww w .j  a  v  a2s . c  o  m*/
 * @param time the new last modified date
 * @throws DataStoreException if the file is writable but modifying the date fails
 */
private static void setLastModified(File file, long time) throws DataStoreException {
    if (!file.setLastModified(time)) {
        if (!file.canWrite()) {
            // if we can't write to the file, so garbage collection will also not delete it
            // (read only files or file systems)
            return;
        }
        try {
            // workaround for Windows: if the file is already open for reading
            // (in this or another process), then setting the last modified date
            // doesn't work - see also JCR-2872
            RandomAccessFile r = new RandomAccessFile(file, "rw");
            try {
                r.setLength(r.length());
            } finally {
                r.close();
            }
        } catch (IOException e) {
            throw new DataStoreException("An IO Exception occurred while trying to set the last modified date: "
                    + file.getAbsolutePath(), e);
        }
    }
}

From source file:phex.util.FileUtils.java

/**
 * Truncates an input file to the requested size. If the file doesnt exists
 * or is of equal or smaller size noting is done.
 *
 * @param file The file to truncate.// w  ww  .j ava  2 s.  c om
 * @param size The size to truncate to.
 * @throws IOException in case an IO error occures during truncating.
 */
public static void truncateFile(File file, long size) throws IOException {
    if (size < 0) {
        throw new IllegalArgumentException("File size < 0: " + size);
    }
    if (file.exists() && file.length() > size) {
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile(file, "rws");
            raf.setLength(size);
        } finally {
            IOUtil.closeQuietly(raf);
        }
    }
}

From source file:org.apache.jackrabbit.oak.run.SegmentUtils.java

static void compact(File directory, boolean force) throws IOException {
    FileStore store = openFileStore(directory.getAbsolutePath(), force);
    try {//from w  w w . j a v a2 s .  c o  m
        boolean persistCM = Boolean.getBoolean("tar.PersistCompactionMap");
        CompactionStrategy compactionStrategy = new CompactionStrategy(false,
                CompactionStrategy.CLONE_BINARIES_DEFAULT, CompactionStrategy.CleanupType.CLEAN_ALL, 0,
                CompactionStrategy.MEMORY_THRESHOLD_DEFAULT) {

            @Override
            public boolean compacted(Callable<Boolean> setHead) throws Exception {
                // oak-run is doing compaction single-threaded
                // hence no guarding needed - go straight ahead
                // and call setHead
                return setHead.call();
            }
        };
        compactionStrategy.setOfflineCompaction(true);
        compactionStrategy.setPersistCompactionMap(persistCM);
        store.setCompactionStrategy(compactionStrategy);
        store.compact();
    } finally {
        store.close();
    }

    System.out.println("    -> cleaning up");
    store = openFileStore(directory.getAbsolutePath(), false);
    try {
        for (File file : store.cleanup()) {
            if (!file.exists() || file.delete()) {
                System.out.println("    -> removed old file " + file.getName());
            } else {
                System.out.println("    -> failed to remove old file " + file.getName());
            }
        }

        String head;
        File journal = new File(directory, "journal.log");
        JournalReader journalReader = new JournalReader(journal);
        try {
            head = journalReader.iterator().next() + " root " + System.currentTimeMillis() + "\n";
        } finally {
            journalReader.close();
        }

        RandomAccessFile journalFile = new RandomAccessFile(journal, "rw");
        try {
            System.out.println("    -> writing new " + journal.getName() + ": " + head);
            journalFile.setLength(0);
            journalFile.writeBytes(head);
            journalFile.getChannel().force(false);
        } finally {
            journalFile.close();
        }
    } finally {
        store.close();
    }
}

From source file:org.apache.jackrabbit.oak.run.SegmentTarUtils.java

static void compact(File directory, boolean force) throws IOException {
    FileStore store = newFileStoreBuilder(directory.getAbsolutePath(), force)
            .withGCOptions(defaultGCOptions().setOffline()).build();
    try {/*from w  ww . j av  a  2  s .  co  m*/
        store.compact();
    } finally {
        store.close();
    }

    System.out.println("    -> cleaning up");
    store = newFileStoreBuilder(directory.getAbsolutePath(), force)
            .withGCOptions(defaultGCOptions().setOffline()).build();
    try {
        for (File file : store.cleanup()) {
            if (!file.exists() || file.delete()) {
                System.out.println("    -> removed old file " + file.getName());
            } else {
                System.out.println("    -> failed to remove old file " + file.getName());
            }
        }

        String head;
        File journal = new File(directory, "journal.log");
        JournalReader journalReader = new JournalReader(journal);
        try {
            head = journalReader.next() + " root " + System.currentTimeMillis() + "\n";
        } finally {
            journalReader.close();
        }

        RandomAccessFile journalFile = new RandomAccessFile(journal, "rw");
        try {
            System.out.println("    -> writing new " + journal.getName() + ": " + head);
            journalFile.setLength(0);
            journalFile.writeBytes(head);
            journalFile.getChannel().force(false);
        } finally {
            journalFile.close();
        }
    } finally {
        store.close();
    }
}

From source file:com.ettrema.zsync.UploadReader.java

/**
 * Copies the contents of the source file to the destination file and sets the destination
 * file's length.// w w  w  .j  ava 2  s. co  m
 * 
 * @param inFile The source file   
 * @param outFile The destination file
 * @param length The desired length of the destination file
 * @throws IOException
 */
private static void copyFile(File inFile, File outFile, long length) throws IOException {

    InputStream fIn = null;
    OutputStream fOut = null;
    RandomAccessFile randAccess = null;

    try {

        fIn = new FileInputStream(inFile);
        fOut = new FileOutputStream(outFile);
        PartialEntity.sendBytes(fIn, fOut, inFile.length());
    } finally {
        StreamUtils.close(fIn);
        StreamUtils.close(fOut);
    }

    try {

        randAccess = new RandomAccessFile(outFile, "rw");
        randAccess.setLength(length);
    } finally {
        Util.close(randAccess);
    }
}