Example usage for org.apache.commons.io FileUtils touch

List of usage examples for org.apache.commons.io FileUtils touch

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils touch.

Prototype

public static void touch(File file) throws IOException 

Source Link

Document

Implements the same behaviour as the "touch" utility on Unix.

Usage

From source file:com.linkedin.pinot.core.segment.index.loader.SegmentPreProcessor.java

private void createInvertedIndex(SegmentDirectory.Writer segmentWriter, ColumnMetadata columnMetadata)
        throws IOException {
    String segmentName = segmentWriter.toString();
    String column = columnMetadata.getColumnName();
    File inProgress = new File(segmentWriter.toSegmentDirectory().getPath().toFile(),
            column + "_inv.inprogress");

    // returning existing inverted index only if the marker file does not exist
    if (!inProgress.exists() && segmentWriter.hasIndexFor(column, ColumnIndexType.INVERTED_INDEX)) {
        LOGGER.info("Found inverted index for segment: {}, colummn {}, loading it", segmentName, column);
        return;//  w ww  . j  ava2 s .com
    }

    // creating the marker file
    FileUtils.touch(inProgress);
    if (segmentWriter.hasIndexFor(column, ColumnIndexType.INVERTED_INDEX)) {
        LOGGER.info("Deleting stale inverted index for segment: {}, column: {}", segmentName, column);
        segmentWriter.removeIndex(column, ColumnIndexType.INVERTED_INDEX);
    }

    LOGGER.info("Creating new inverted index for segment: {}, column: {}", segmentName, column);

    // creating inverted index for the column now
    OffHeapBitmapInvertedIndexCreator creator = new OffHeapBitmapInvertedIndexCreator(indexDir,
            columnMetadata.getCardinality(), columnMetadata.getTotalDocs(),
            columnMetadata.getTotalNumberOfEntries(), columnMetadata.toFieldSpec());
    DataFileReader fwdIndex = getForwardIndexReader(columnMetadata, segmentWriter);

    if (!columnMetadata.isSingleValue()) {
        SingleColumnMultiValueReader mvFwdIndex = (SingleColumnMultiValueReader) fwdIndex;
        int[] dictIds = new int[columnMetadata.getMaxNumberOfMultiValues()];
        for (int i = 0; i < metadata.getTotalDocs(); i++) {
            int len = mvFwdIndex.getIntArray(i, dictIds);
            creator.add(i, dictIds, len);
        }
    } else {
        FixedBitSingleValueReader svFwdIndex = (FixedBitSingleValueReader) fwdIndex;
        for (int i = 0; i < columnMetadata.getTotalDocs(); i++) {
            creator.add(i, svFwdIndex.getInt(i));
        }
    }
    creator.seal();
    File invertedIndexFile = creator.getInvertedIndexFile();
    // Inverted index creation does not know the size upfront so
    // we create it in a temporary file and then move to the main
    // file. For v1/v2 format, this is an overkill but it's required
    // to avoid corruption of v3 format
    File tempFile = new File(invertedIndexFile + ".temp");
    if (tempFile.exists()) {
        FileUtils.deleteQuietly(tempFile);
    }
    FileUtils.moveFile(invertedIndexFile, tempFile);
    PinotDataBuffer newIndexBuffer = null;

    try {
        if (segmentWriter.hasIndexFor(column, ColumnIndexType.INVERTED_INDEX)) {
            PinotDataBuffer tempBuffer = segmentWriter.getIndexFor(column, ColumnIndexType.INVERTED_INDEX);

            // almost always we will have matching size since segment data is immutable
            // but it's good to double check
            if (tempBuffer.size() == tempFile.length()) {
                newIndexBuffer = tempBuffer;
            } else {
                if (segmentWriter.isIndexRemovalSupported()) {
                    segmentWriter.removeIndex(column, ColumnIndexType.INVERTED_INDEX);
                    newIndexBuffer = segmentWriter.newIndexFor(column, ColumnIndexType.INVERTED_INDEX,
                            (int) tempFile.length());
                } else {
                    LOGGER.error(
                            "Segment: {} already has inverted index that can not be removed. Throwing exception to discard and download segment",
                            segmentWriter);
                    throw new IllegalStateException("Inverted Index exists and can not be removed for segment: "
                            + segmentWriter + ". Throwing exception to download fresh segment");
                }
            }
        } else { // there was no index earlier
            newIndexBuffer = segmentWriter.newIndexFor(column, ColumnIndexType.INVERTED_INDEX,
                    (int) tempFile.length());
        }
        newIndexBuffer.readFrom(tempFile);
    } finally {
        if (newIndexBuffer != null) {
            newIndexBuffer.close();
        }
    }

    // delete the marker file
    FileUtils.deleteQuietly(inProgress);
    FileUtils.deleteQuietly(tempFile);

    LOGGER.info("Created inverted index for segment: {}, colummn {}", segmentName, column);
}

From source file:com.mbrlabs.mundus.assets.EditorAssetManager.java

public TerrainAsset createTerrainAsset(int vertexResolution, int size) throws IOException {
    String terraFilename = "terrain_" + UUID.randomUUID().toString() + ".terra";
    String metaFilename = terraFilename + ".meta";

    // create meta file
    String metaPath = FilenameUtils.concat(rootFolder.path(), metaFilename);
    MetaFile meta = createNewMetaFile(new FileHandle(metaPath), AssetType.TERRAIN);
    meta.setTerrainSize(size);//from w ww  .  j  a  v  a 2  s.com
    meta.save();

    // create terra file
    String terraPath = FilenameUtils.concat(rootFolder.path(), terraFilename);
    File terraFile = new File(terraPath);
    FileUtils.touch(terraFile);

    // create initial height data
    float[] data = new float[vertexResolution * vertexResolution];
    for (int i = 0; i < data.length; i++) {
        data[i] = 0;
    }

    // write terra file
    DataOutputStream outputStream = new DataOutputStream(
            new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(terraFile))));
    for (float f : data) {
        outputStream.writeFloat(f);
    }
    outputStream.flush();
    outputStream.close();

    // load & apply standard chessboard texture
    TerrainAsset asset = new TerrainAsset(meta, new FileHandle(terraFile));
    asset.load();

    TextureAsset chessboard = (TextureAsset) findAssetByID(STANDARD_ASSET_TEXTURE_CHESSBOARD);
    if (chessboard != null) {
        // create splatmap
        PixmapTextureAsset splatmap = createPixmapTextureAsset(SplatMap.DEFAULT_SIZE);
        asset.setSplatmap(splatmap);
        asset.setSplatBase(chessboard);
        asset.applyDependencies();
    }

    addAsset(asset);
    return asset;
}

From source file:gov.redhawk.sca.efs.tests.ScaFileStoreTest.java

/**
 * Test method for {@link gov.redhawk.efs.sca.internal.ScaFileStore#openInputStream(int, org.eclipse.core.runtime.IProgressMonitor)}.
 * @throws Exception /*from   w ww  .java  2s.com*/
 */
@Test
public void testOpenInputStreamIntIProgressMonitor() throws Exception {
    this.deleteFile = new File(ScaFileStoreTest.session.getRootFile(), "test");
    FileUtils.touch(this.deleteFile);
    final IFileStore fileStore = this.rootFileStore.getFileStore(new Path("test"));
    this.inputStream = fileStore.openInputStream(0, null);
}

From source file:com.orange.mmp.dao.flf.ModuleDaoFlfImpl.java

@SuppressWarnings("unchecked")
public Module createOrUdpdate(Module module) throws MMPDaoException {
    if (module == null || module.getLocation() == null) {
        throw new MMPDaoException("missing or bad data access object");
    }//from   w w  w.  j a  v  a2  s  .  c  o m

    JarFile jarFile = null;
    try {
        this.lock.lock();
        jarFile = new JarFile(new File(module.getLocation()));
        Manifest manifest = jarFile.getManifest();
        if (manifest == null) {
            throw new MMPDaoException("invalid module archive, MANIFEST file not found");
        }
        String symbolicName = manifest.getMainAttributes().getValue(MODULE_ID_HEADER);
        if (manifest.getMainAttributes().getValue(MODULE_ID_HEADER) != null) {
            if (module instanceof Widget) {
                String[] widgetId = symbolicName.split(com.orange.mmp.widget.Constants.BRANCH_SUFFIX_PATTERN);
                module.setId(widgetId[0]);
            } else {
                module.setId(symbolicName);
            }
        } else
            throw new MMPDaoException("invalid module archive, missing " + MODULE_ID_HEADER + " header");
        if (manifest.getMainAttributes().getValue(MODULE_NAME_HEADER) != null) {
            module.setName(manifest.getMainAttributes().getValue(MODULE_NAME_HEADER));
        } else
            throw new MMPDaoException("invalid module archive, missing " + MODULE_NAME_HEADER + " header");
        if (manifest.getMainAttributes().getValue(MODULE_VERSION_HEADER) != null) {
            module.setVersion(new Version(manifest.getMainAttributes().getValue(MODULE_VERSION_HEADER)));
        } else
            throw new MMPDaoException("invalid module archive, missing " + MODULE_VERSION_HEADER + " header");
        if (manifest.getMainAttributes().getValue(MODULE_CATEGORY_HEADER) != null) {
            module.setCategory(manifest.getMainAttributes().getValue(MODULE_CATEGORY_HEADER));
        } else
            module.setCategory(Constants.MODULE_CATEGORY_LIBRARY);

        File moduleFile = new File(module.getLocation());
        File dstFile;
        if (module instanceof Widget) {
            String[] nameAndBranch = symbolicName.split(com.orange.mmp.widget.Constants.BRANCH_SUFFIX_PATTERN);
            if (nameAndBranch.length > 1)
                dstFile = new File(this.path,
                        nameAndBranch[0].concat(com.orange.mmp.widget.Constants.BRANCH_SUFFIX_PATTERN)
                                .concat(nameAndBranch[1]).concat(".jar"));
            else {
                String defaultBranchId = null;
                Branch defaultBranch = new Branch();
                defaultBranch.setDefault(true);
                Branch defaultBranchesResult[] = (Branch[]) DaoManagerFactory.getInstance().getDaoManager()
                        .getDao("branch").find(defaultBranch);
                if (defaultBranchesResult.length > 0) {
                    defaultBranchId = defaultBranchesResult[0].getId();
                }
                defaultBranch = defaultBranchesResult[0];
                dstFile = new File(this.path,
                        nameAndBranch[0].concat(com.orange.mmp.widget.Constants.BRANCH_SUFFIX_PATTERN)
                                .concat(defaultBranchId).concat(".jar"));
            }
        } else {
            dstFile = new File(this.path, symbolicName.concat(".jar"));
        }
        FileUtils.copyFile(moduleFile, dstFile);
        module.setLocation(dstFile.toURI());
        module.setLastModified(dstFile.lastModified());
        jarFile.close();

        FileUtils.touch(new File(this.path));

        return module;
    } catch (IOException ioe) {
        throw new MMPDaoException("failed to add module : " + ioe.getMessage());
    } finally {
        try {
            if (jarFile != null)
                jarFile.close();
        } catch (IOException ioe) {
            //Nop just log
        }
        this.lock.unlock();
    }
}

From source file:com.ning.metrics.collector.processing.TestEventSpoolWriterFactory.java

public void testProcessLeftBelowFilesWithFilesRemaining() throws Exception {
    final EventSpoolWriterFactory factory = new EventSpoolWriterFactory(
            new HashSet<EventSpoolProcessor>(Arrays.asList(new NoWriteHadoopWriterFactory(null, config))),
            config, configFactory);//from  w ww .ja  v  a 2s. c o  m
    factory.setCutoffTime(CUTOFF_TIME);

    FileUtils.touch(new File(tmpDirectory.getPath() + "/dont_touch_me"));
    FileUtils.touch(new File(lockDirectory.getPath() + "/some_file_which_should_be_sent_1"));
    FileUtils.touch(new File(lockDirectory.getPath() + "/some_file_which_should_be_sent_2"));
    FileUtils.touch(new File(quarantineDirectory.getPath() + "/some_other_file_which_should_be_sent"));

    Assert.assertEquals(FileUtils
            .listFiles(spoolDirectory, FileFilterUtils.trueFileFilter(), FileFilterUtils.trueFileFilter())
            .size(), 4);
    Assert.assertTrue(spoolDirectory.exists());
    Assert.assertTrue(tmpDirectory.exists());
    Assert.assertTrue(lockDirectory.exists());
    Assert.assertTrue(quarantineDirectory.exists());

    Thread.sleep(2 * CUTOFF_TIME);

    factory.processLeftBelowFiles();

    // The file in /_tmp should no have been sent
    Assert.assertEquals(FileUtils
            .listFiles(spoolDirectory, FileFilterUtils.trueFileFilter(), FileFilterUtils.trueFileFilter())
            .size(), 1);
    Assert.assertTrue(spoolDirectory.exists());
    Assert.assertTrue(tmpDirectory.exists());
    Assert.assertFalse(lockDirectory.exists());
    Assert.assertFalse(quarantineDirectory.exists());

    // We could even test the mapping in HDFS here (with the keys)
    Assert.assertEquals(hdfs.values().size(), 3);

}

From source file:gov.redhawk.sca.efs.tests.ScaFileStoreTest.java

/**
 * Test method for {@link gov.redhawk.efs.sca.internal.ScaFileStore#openOutputStream(int, org.eclipse.core.runtime.IProgressMonitor)}.
 *///  w ww . ja v  a  2  s.  c o m
@Test
public void testOpenOutputStreamIntIProgressMonitor() throws Exception {
    this.deleteFile = new File(ScaFileStoreTest.session.getRootFile(), "test");
    FileUtils.touch(this.deleteFile);
    final IFileStore fileStore = this.rootFileStore.getFileStore(new Path("test"));
    this.outputStream = fileStore.openOutputStream(0, null);
}

From source file:com.silverpeas.util.FileUtilTest.java

@Test
public void testDeleteEmptyDir() throws IOException {
    File root = File.createTempFile("prefix", "suffix");
    FileUtils.deleteQuietly(root);/*from ww  w  .  j ava2  s.  c  om*/
    assertThat(root.exists(), is(false));

    File newFile = new File(root, "aFile.txt");
    FileUtils.touch(newFile);
    assertThat(root.exists(), is(true));
    assertThat(root.isDirectory(), is(true));
    assertThat(newFile.exists(), is(true));
    assertThat(newFile.isFile(), is(true));

    assertThat(FileUtil.deleteEmptyDir(root), is(false));
    assertThat(root.exists(), is(true));
    assertThat(root.isDirectory(), is(true));
    assertThat(newFile.exists(), is(true));
    assertThat(newFile.isFile(), is(true));

    assertThat(newFile.delete(), is(true));
    assertThat(newFile.exists(), is(false));

    assertThat(FileUtil.deleteEmptyDir(root), is(true));
    assertThat(root.exists(), is(false));
}

From source file:com.yifanlu.PSXperiaTool.Extractor.CrashBandicootExtractor.java

private void extractZip(File zipFile, File output, FileFilter filter) throws IOException {
    Logger.info("Extracting ZIP file: %s to: %s", zipFile.getPath(), output.getPath());
    if (!output.exists())
        output.mkdirs();//  w w w  .j a va  2s  .  c  o  m
    ZipInputStream zip = new ZipInputStream(new FileInputStream(zipFile));
    ZipEntry entry;
    while ((entry = zip.getNextEntry()) != null) {
        File file = new File(output, entry.getName());
        if (file.isDirectory())
            continue;
        if (filter != null && !filter.accept(file))
            continue;
        Logger.verbose("Unzipping %s", entry.getName());
        FileUtils.touch(file);
        FileOutputStream out = new FileOutputStream(file.getPath());
        int n;
        byte[] buffer = new byte[BLOCK_SIZE];
        while ((n = zip.read(buffer)) != -1) {
            out.write(buffer, 0, n);
        }
        out.close();
        zip.closeEntry();
        Logger.verbose("Done extracting %s", entry.getName());
    }
    zip.close();
    Logger.debug("Done extracting ZIP.");
}

From source file:com.alibaba.otter.node.etl.load.FileLoadActionTest.java

protected List<FileData> buildFileDatas(String namespace, EventType eventType, int start, int count,
        boolean create) throws IOException {
    List<FileData> files = new ArrayList<FileData>();

    for (int i = start; i < count; i++) {
        FileData fileData = new FileData();
        fileData.setNameSpace(namespace); // namespace is null means file is
                                          // local file
        fileData.setEventType(eventType);
        fileData.setPairId(i % NUMBER_OF_FILE_DATA_COPIES);
        fileData.setPath(ROOT_DIR.getAbsolutePath() + "/target/" + eventType.getValue() + i);

        String parentPath = ROOT_DIR.getPath();
        if (namespace != null) {
            parentPath = parentPath + "/" + namespace;
        }// w ww  . j  av a2s  . c  o m
        File file = new File(parentPath, fileData.getPath());
        if (!file.exists() && create) {
            FileUtils.touch(file);
        }

        fileData.setSize(file.exists() ? file.length() : 0);
        fileData.setLastModifiedTime(
                file.exists() ? file.lastModified() : Calendar.getInstance().getTimeInMillis());
        fileData.setTableId(TABLE_ID);

        files.add(fileData);
    }

    return files;
}

From source file:com.izforge.izpack.event.BSFInstallerListenerTest.java

/**
 * Tests the {@link BSFInstallerListener}.
 *
 * @param resources the resources/*from  w  ww . j av  a2s . co m*/
 * @param suffix    the file name suffix
 * @throws IOException for any I/O error
 */
private void checkListener(Resources resources, String suffix) throws IOException {
    Pack pack = new Pack("Base", null, null, null, null, true, true, false, null, true, 0);
    List<Pack> packs = Arrays.asList(pack);

    ProgressListener progressListener = Mockito.mock(ProgressListener.class);

    UninstallData uninstallData = new UninstallData();
    ProgressNotifiers notifiers = new ProgressNotifiersImpl();
    BSFInstallerListener listener = new BSFInstallerListener(installData, replacer, resources, uninstallData,
            notifiers);
    listener.initialise();

    // Verify that when the beforePacks method is invoked, the corresponding BSF action is called.
    assertFileNotExists(installDir, "beforepacks" + suffix);
    listener.beforePacks(packs);
    assertFileExists(installDir, "beforepacks" + suffix);

    // Verify that when the beforePack method is invoked, the corresponding BSF action is called.
    assertFileNotExists(installDir, "beforepack" + suffix);
    listener.beforePack(pack, 0);
    assertFileExists(installDir, "beforepack" + suffix);

    // Verify that when the beforeDir method is invoked, the corresponding BSF action is called.
    File dir = new File(installDir, "dir");
    assertTrue(dir.mkdir());
    assertFileNotExists(installDir, "beforedir" + suffix);
    PackFile dirPackFile = new PackFile(installDir, dir, dir.getName(), null, OverrideType.OVERRIDE_TRUE, null,
            Blockable.BLOCKABLE_NONE);
    listener.beforeDir(dir, dirPackFile, pack);
    assertFileExists(installDir, "beforedir" + suffix);

    // Verify that when the afterDir method is invoked, the corresponding BSF action is called.
    assertFileNotExists(installDir, "afterdir" + suffix);
    listener.afterDir(dir, dirPackFile, pack);
    assertFileExists(installDir, "afterdir" + suffix);

    // Verify that when the beforeFile method is invoked, the corresponding BSF action is called.
    File file = new File(installDir, "file.txt");
    FileUtils.touch(file);
    assertFileNotExists(installDir, "beforefile" + suffix);
    PackFile packFile = new PackFile(installDir, file, file.getName(), null, OverrideType.OVERRIDE_TRUE, null,
            Blockable.BLOCKABLE_NONE);
    listener.beforeFile(file, packFile, pack);
    assertFileExists(installDir, "beforefile" + suffix);

    // Verify that when the afterFile method is invoked, the corresponding BSF action is called.
    assertFileNotExists(installDir, "afterfile" + suffix);
    listener.afterFile(file, packFile, pack);
    assertFileExists(installDir, "afterfile" + suffix);

    // Verify that when the afterPack method is invoked, the corresponding BSF action is called.
    assertFileNotExists(installDir, "afterpack" + suffix);
    listener.afterPack(pack, 0);
    assertFileExists(installDir, "afterpack" + suffix);

    // Verify that when the afterPacks method is invoked, the corresponding BSF action is called.
    // This touches a file "afterpacks.txt"
    assertFileNotExists(installDir, "afterpacks" + suffix);
    listener.afterPacks(packs, progressListener);
    assertFileExists(installDir, "afterpacks" + suffix);
}