New version of the article
This commit is contained in:
@@ -8,4 +8,5 @@ This module contains articles about core Java non-blocking input and output (IO)
|
||||
- [Create a Symbolic Link with Java](https://www.baeldung.com/java-symlink)
|
||||
- [Introduction to the Java NIO Selector](https://www.baeldung.com/java-nio-selector)
|
||||
- [Using Java MappedByteBuffer](https://www.baeldung.com/java-mapped-byte-buffer)
|
||||
- [How to Lock a File in Java](https://www.baeldung.com/how-to-lock-a-file-in-java)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-nio)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -14,22 +14,5 @@
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/com.github.jnr/jnr-ffi -->
|
||||
<dependency>
|
||||
<groupId>com.github.jnr</groupId>
|
||||
<artifactId>jnr-ffi</artifactId>
|
||||
<version>2.1.11</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.github.jnr/jnr-constants -->
|
||||
<dependency>
|
||||
<groupId>com.github.jnr</groupId>
|
||||
<artifactId>jnr-constants</artifactId>
|
||||
<version>0.9.14</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
File diff suppressed because it is too large
Load Diff
-21
@@ -1,21 +0,0 @@
|
||||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||
#include <jni.h>
|
||||
/* Header for class com_baeldung_lock_FileLocks */
|
||||
|
||||
#ifndef _Included_com_baeldung_lock_FileLocks
|
||||
#define _Included_com_baeldung_lock_FileLocks
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/*
|
||||
* Class: com_baeldung_lock_FileLocks
|
||||
* Method: getpid
|
||||
* Signature: ()J
|
||||
*/
|
||||
JNIEXPORT jlong JNICALL Java_com_baeldung_lock_FileLocks_getpid
|
||||
(JNIEnv *, jclass);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.baeldung.lock;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.channels.FileLock;
|
||||
import java.nio.channels.NonReadableChannelException;
|
||||
import java.nio.channels.NonWritableChannelException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FileLocksTest {
|
||||
|
||||
// Exclusive locks
|
||||
/**
|
||||
* Getting an exclusive (write) lock on the entire file.
|
||||
* Fails when getting the lock from a FileChannel obtained through a FileInputStream.
|
||||
*/
|
||||
@Test
|
||||
void givenAnInputStream_whenGetWriteLock_throwNonWritableChannelException() {
|
||||
assertThrows(NonWritableChannelException.class,
|
||||
() -> FileLocks.getExclusiveLockFromInputStream());
|
||||
}
|
||||
|
||||
/**
|
||||
* Getting and exclusive lock from a RandomAccessFile
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
void givenARandomAccessFileWithReadWriteAccess_whenGetWriteLock_lock() throws IOException {
|
||||
FileLock lock = FileLocks.getExclusiveLockFromRandomAccessFile(0, 10);
|
||||
assertNotNull(lock);
|
||||
assertFalse(lock.isShared());
|
||||
}
|
||||
|
||||
/**
|
||||
* Getting an exclusive lock from FileChannel::open
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
void givenAPath_whenGetExclusiveLock_lock() throws IOException {
|
||||
FileLock lock = FileLocks.getExclusiveLockFromFileChannelOpen(0, 10);
|
||||
assertNotNull(lock);
|
||||
assertFalse(lock.isShared());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Getting a shared (read) lock works fine when getting the lock from a FileChannel obtained through a FileInputStream.
|
||||
* @throws IOException
|
||||
*/
|
||||
@Test
|
||||
void givenAnInputStream_whenGetSharedLock_lock() throws IOException {
|
||||
FileLock lock = FileLocks.getReadLockFromInputStream(0, 10);
|
||||
assertNotNull(lock);
|
||||
assertTrue(lock.isShared());
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void givenAFileOutputStream_whenGetSharedLock_throwNonReadableChannelException() {
|
||||
assertThrows(NonReadableChannelException.class,
|
||||
() -> FileLocks.getReadLockFromOutputStream(0, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenARandomAccessFile_whenGetSharedLock_lock() throws IOException {
|
||||
FileLock lock = FileLocks.getReadLockFromRandomAccessFile(0, 10);
|
||||
assertNotNull(lock);
|
||||
assertTrue(lock.isShared());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user