Code example for FileChannel

Methods: isOpen, size

0
   */ 
  public KeystoneImpl(FileChannel file, long fileOffset) throws IOException {
    // construct the instance and validate parameters.. 
    this(file, fileOffset, false);
 
    if (file.size() < fileOffset + workBuffer.capacity())
      throw new IOException("No keystone found at offset " + fileOffset + "; file is " + file.size() + " bytes");
  } 
 
 
  /** 
   * Creates a new instance with given initial value and writes its state to 
   * persistent storage. (There's no such thing as uninitialized keystone.) 
   *  
   * @param file 
   *          open channel to the undelying file 
   * @param fileOffset 
   *          the offset at which this keystone begins 
   * @param initValue 
   *          the initial value of the keystone 
   */ 
  public KeystoneImpl(FileChannel file, long fileOffset, long initValue) throws IOException {
    // construct the instance and validate parameters 
    this(file, fileOffset, false);
 
    // initialize the keystone value 
    workBuffer.clear();
    workBuffer.putLong(initValue).putLong(0).put((byte) 0);
    workBuffer.flip();
    ChannelUtils.writeRemaining(file, fileOffset, workBuffer);
 
    // sanity check.. 
    long recordedValue = get();
    if (recordedValue != initValue)
      throw new IOException("Sanity check failed: expected to read initValue <" + initValue + ">; actual was <" + recordedValue + ">");
  } 
   
 
 
 
  private KeystoneImpl(FileChannel file, long fileOffset, boolean sigDisambiguator) throws IOException {
    if (file == null)
      throw new IllegalArgumentException("null file channel");
    if (!file.isOpen())
      throw new ClosedChannelException();
    if (fileOffset < 0)
      throw new IllegalArgumentException("fileOffset: " + fileOffset);
 
    this.file = file;