Java Code Examples for org.apache.hadoop.fs.FileSystem.truncate()

The following are Jave code examples for showing how to use truncate() of the org.apache.hadoop.fs.FileSystem class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: hadoop   File: BaseTestHttpFSWith.java   Source Code and License Vote up 6 votes
private void testTruncate() throws Exception {
  if (!isLocalFS()) {
    final short repl = 3;
    final int blockSize = 1024;
    final int numOfBlocks = 2;
    FileSystem fs = FileSystem.get(getProxiedFSConf());
    fs.mkdirs(getProxiedFSTestDir());
    Path file = new Path(getProxiedFSTestDir(), "foo.txt");
    final byte[] data = FileSystemTestHelper.getFileData(
        numOfBlocks, blockSize);
    FileSystemTestHelper.createFile(fs, file, data, blockSize, repl);

    final int newLength = blockSize;

    boolean isReady = fs.truncate(file, newLength);
    Assert.assertTrue("Recovery is not expected.", isReady);

    FileStatus fileStatus = fs.getFileStatus(file);
    Assert.assertEquals(fileStatus.getLen(), newLength);
    AppendTestUtil.checkFullFile(fs, file, newLength, data, file.toString());

    fs.close();
  }
}
 
Example 2
Project: hadoop   File: FSOperations.java   Source Code and License Vote up 3 votes
/**
 * Executes the filesystem operation.
 *
 * @param fs filesystem instance to use.
 *
 * @return <code>true</code> if the file has been truncated to the desired,
 *         <code>false</code> if a background process of adjusting the 
 *         length of the last block has been started, and clients should 
 *         wait for it to complete before proceeding with further file 
 *         updates.
 *
 * @throws IOException thrown if an IO error occured.
 */
@Override
public JSONObject execute(FileSystem fs) throws IOException {
  boolean result = fs.truncate(path, newLength);
  return toJSON(
      StringUtils.toLowerCase(HttpFSFileSystem.TRUNCATE_JSON), result);
}