Package java.nio.channels

Examples of java.nio.channels.FileChannel.truncate()


            FileChannel channel = file.getChannel();
            int newsize = random.nextInt((int)channel.size()/2);
            System.out.println("Deliberately truncating file " +
                               blocks[idx].getName() +
                               " to size " + newsize + " bytes.");
            channel.truncate(newsize);
            file.close();
          } else {
            //
            // corrupt a few bytes of the metafile
            //
View Full Code Here


        File mf = b.getMetaFile();
        // Truncate a block file that has a corresponding metadata file
        if (f.exists() && f.length() != 0 && mf.exists()) {
          FileOutputStream s = new FileOutputStream(f);
          FileChannel channel = s.getChannel();
          channel.truncate(0);
          LOG.info("Truncated block file " + f.getAbsolutePath());
          return b.getBlockId();
        }
      }
    }
View Full Code Here

        File mf = b.getMetaFile();
        // Truncate a block file that has a corresponding metadata file
        if (f.exists() && f.length() != 0 && mf.exists()) {
          FileOutputStream s = new FileOutputStream(f);
          FileChannel channel = s.getChannel();
          channel.truncate(0);
          LOG.info("Truncated block file " + f.getAbsolutePath());
          return b.getBlockId();
        }
      }
    }
View Full Code Here

      public void purge(File f) throws IOException {
         // Avoid a delete per-se because it hampers any fsync-like functionality
         // cos any cached file channel write won't change the file's exists
         // status. So, clear the file rather than delete it.
         FileChannel channel = streams.get(f.getPath());
         channel.truncate(0);
         // Apart from truncating, it's necessary to reset the position!
         channel.position(0);
      }

      @Override
View Full Code Here

  @Test
  public void testTruncate() throws IOException {
    RegularFile file = regularFile(10);
    FileChannel channel = channel(file, WRITE);

    channel.truncate(10); // no resize, >= size
    assertEquals(10, file.size());
    channel.truncate(11); // no resize, > size
    assertEquals(10, file.size());
    channel.truncate(5); // resize down to 5
    assertEquals(5, file.size());
View Full Code Here

    RegularFile file = regularFile(10);
    FileChannel channel = channel(file, WRITE);

    channel.truncate(10); // no resize, >= size
    assertEquals(10, file.size());
    channel.truncate(11); // no resize, > size
    assertEquals(10, file.size());
    channel.truncate(5); // resize down to 5
    assertEquals(5, file.size());

    channel.position(20);
View Full Code Here

    channel.truncate(10); // no resize, >= size
    assertEquals(10, file.size());
    channel.truncate(11); // no resize, > size
    assertEquals(10, file.size());
    channel.truncate(5); // resize down to 5
    assertEquals(5, file.size());

    channel.position(20);
    channel.truncate(10);
    assertEquals(10, channel.position());
View Full Code Here

    assertEquals(10, file.size());
    channel.truncate(5); // resize down to 5
    assertEquals(5, file.size());

    channel.position(20);
    channel.truncate(10);
    assertEquals(10, channel.position());
    channel.truncate(2);
    assertEquals(2, channel.position());
  }
View Full Code Here

    assertEquals(5, file.size());

    channel.position(20);
    channel.truncate(10);
    assertEquals(10, channel.position());
    channel.truncate(2);
    assertEquals(2, channel.position());
  }

  @Test
  public void testFileTimeUpdates() throws IOException {
View Full Code Here

    assertNotEquals(modifiedTime, file.getLastModifiedTime());

    modifiedTime = file.getLastModifiedTime();
    Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);

    channel.truncate(0);
    assertNotEquals(modifiedTime, file.getLastModifiedTime());

    modifiedTime = file.getLastModifiedTime();
    Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);
View Full Code Here

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.