jdk/src/share/classes/sun/nio/ch/FileLockImpl.java
changeset 2057 3acf8e5e2ca0
parent 2 90ce3da70b43
child 2594 3755ecdb395d
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
     1 /*
     1 /*
     2  * Copyright 2001-2003 Sun Microsystems, Inc.  All Rights Reserved.
     2  * Copyright 2001-2009 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     7  * published by the Free Software Foundation.  Sun designates this
    24  */
    24  */
    25 
    25 
    26 package sun.nio.ch;
    26 package sun.nio.ch;
    27 
    27 
    28 import java.io.IOException;
    28 import java.io.IOException;
    29 import java.nio.channels.ClosedChannelException;
    29 import java.nio.channels.*;
    30 import java.nio.channels.FileLock;
       
    31 import java.nio.channels.FileChannel;
       
    32 
    30 
    33 public class FileLockImpl
    31 public class FileLockImpl
    34     extends FileLock
    32     extends FileLock
    35 {
    33 {
    36     boolean valid;
    34     boolean valid;
    37 
    35 
    38     FileLockImpl(FileChannel channel, long position, long size, boolean shared)
    36     FileLockImpl(FileChannel channel, long position, long size, boolean shared)
       
    37     {
       
    38         super(channel, position, size, shared);
       
    39         this.valid = true;
       
    40     }
       
    41 
       
    42     FileLockImpl(AsynchronousFileChannel channel, long position, long size, boolean shared)
    39     {
    43     {
    40         super(channel, position, size, shared);
    44         super(channel, position, size, shared);
    41         this.valid = true;
    45         this.valid = true;
    42     }
    46     }
    43 
    47 
    48     synchronized void invalidate() {
    52     synchronized void invalidate() {
    49         valid = false;
    53         valid = false;
    50     }
    54     }
    51 
    55 
    52     public synchronized void release() throws IOException {
    56     public synchronized void release() throws IOException {
    53         if (!channel().isOpen())
    57         Channel ch = acquiredBy();
       
    58         if (!ch.isOpen())
    54             throw new ClosedChannelException();
    59             throw new ClosedChannelException();
    55         if (valid) {
    60         if (valid) {
    56             ((FileChannelImpl)channel()).release(this);
    61             if (ch instanceof FileChannelImpl)
       
    62                 ((FileChannelImpl)ch).release(this);
       
    63             else if (ch instanceof AsynchronousFileChannelImpl)
       
    64                 ((AsynchronousFileChannelImpl)ch).release(this);
       
    65             else throw new AssertionError();
    57             valid = false;
    66             valid = false;
    58         }
    67         }
    59     }
    68     }
    60 
    69 
    61 }
    70 }