Skip to content

Commit d03b119

Browse files
committed
[BAEL-3601] - Reformatted code with eclipse. Removed unused imports.
1 parent 8134888 commit d03b119

2 files changed

Lines changed: 33 additions & 18 deletions

File tree

core-java-modules/core-java-nio-2/src/main/java/com/baeldung/lock/FileLocks.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.baeldung.lock;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
36
import java.io.FileInputStream;
47
import java.io.FileOutputStream;
58
import java.io.IOException;
@@ -14,11 +17,6 @@
1417
import java.nio.file.Path;
1518
import java.nio.file.StandardOpenOption;
1619

17-
import org.slf4j.Logger;
18-
import org.slf4j.LoggerFactory;
19-
20-
import com.google.common.base.Charsets;
21-
2220
public class FileLocks {
2321

2422
private static final Logger LOG = LoggerFactory.getLogger(FileLocks.class);
@@ -30,7 +28,9 @@ public class FileLocks {
3028
*/
3129
static void getExclusiveLockFromInputStream() throws IOException {
3230
Path path = Files.createTempFile("foo", "txt");
33-
try (FileInputStream fis = new FileInputStream(path.toFile()); FileLock lock = fis.getChannel().lock()) {
31+
try (FileInputStream fis = new FileInputStream(path.toFile());
32+
FileLock lock = fis.getChannel()
33+
.lock()) {
3434
LOG.debug("This won't happen");
3535
} catch (NonWritableChannelException e) {
3636
LOG.error("The channel obtained through a FileInputStream isn't writable. You can't obtain an exclusive lock on it!");
@@ -47,7 +47,9 @@ static void getExclusiveLockFromInputStream() throws IOException {
4747
*/
4848
static FileLock getExclusiveLockFromRandomAccessFile(long from, long size) throws IOException {
4949
Path path = Files.createTempFile("foo", "txt");
50-
try (RandomAccessFile file = new RandomAccessFile(path.toFile(), "rw"); FileLock lock = file.getChannel().lock(from, size, false)) {
50+
try (RandomAccessFile file = new RandomAccessFile(path.toFile(), "rw");
51+
FileLock lock = file.getChannel()
52+
.lock(from, size, false)) {
5153
if (lock.isValid()) {
5254
LOG.debug("This is a valid exclusive lock");
5355
return lock;
@@ -70,14 +72,16 @@ static FileLock getExclusiveLockFromFileChannelOpen(long from, long size) throws
7072
Path path = Files.createTempFile("foo", "txt");
7173
try (FileChannel channel = FileChannel.open(path, StandardOpenOption.APPEND); FileLock lock = channel.lock(from, size, false)) {
7274
String text = "Hello, world.";
73-
ByteBuffer buffer = ByteBuffer.allocate(text.length() + System.lineSeparator().length());
75+
ByteBuffer buffer = ByteBuffer.allocate(text.length() + System.lineSeparator()
76+
.length());
7477
buffer.put((text + System.lineSeparator()).getBytes(StandardCharsets.UTF_8));
7578
buffer.flip();
7679
while (buffer.hasRemaining()) {
7780
channel.write(buffer, channel.size());
7881
}
7982
LOG.debug("This was written to the file");
80-
Files.lines(path).forEach(LOG::debug);
83+
Files.lines(path)
84+
.forEach(LOG::debug);
8185
return lock;
8286
}
8387
}
@@ -89,7 +93,9 @@ static FileLock getExclusiveLockFromFileChannelOpen(long from, long size) throws
8993
*/
9094
static void getReadLockFromOutputStream() throws IOException {
9195
Path path = Files.createTempFile("foo", "txt");
92-
try (FileOutputStream fis = new FileOutputStream(path.toFile()); FileLock lock = fis.getChannel().lock(0, Long.MAX_VALUE, true)) {
96+
try (FileOutputStream fis = new FileOutputStream(path.toFile());
97+
FileLock lock = fis.getChannel()
98+
.lock(0, Long.MAX_VALUE, true)) {
9399
LOG.debug("This won't happen");
94100
} catch (NonReadableChannelException e) {
95101
LOG.error("The channel obtained through a FileOutputStream isn't readable. " + "You can't obtain an shared lock on it!");
@@ -106,7 +112,9 @@ static void getReadLockFromOutputStream() throws IOException {
106112
*/
107113
static FileLock getReadLockFromInputStream(long from, long size) throws IOException {
108114
Path path = Files.createTempFile("foo", "txt");
109-
try (FileInputStream fis = new FileInputStream(path.toFile()); FileLock lock = fis.getChannel().lock(from, size, true)) {
115+
try (FileInputStream fis = new FileInputStream(path.toFile());
116+
FileLock lock = fis.getChannel()
117+
.lock(from, size, true)) {
110118
if (lock.isValid()) {
111119
LOG.debug("This is a valid shared lock");
112120
return lock;
@@ -125,7 +133,8 @@ static FileLock getReadLockFromInputStream(long from, long size) throws IOExcept
125133
static FileLock getReadLockFromRandomAccessFile(long from, long size) throws IOException {
126134
Path path = Files.createTempFile("foo", "txt");
127135
try (RandomAccessFile file = new RandomAccessFile(path.toFile(), "r"); // could also be "rw", but "r" is sufficient for reading
128-
FileLock lock = file.getChannel().lock(from, size, true)) {
136+
FileLock lock = file.getChannel()
137+
.lock(from, size, true)) {
129138
if (lock.isValid()) {
130139
LOG.debug("This is a valid shared lock");
131140
return lock;

core-java-modules/core-java-nio-2/src/test/java/com/baeldung/lock/FileLocksUnitTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,39 @@
1414

1515
class FileLocksUnitTest {
1616

17-
@Test void givenAnInputStream_whenGetWriteLock_throwNonWritableChannelException() {
17+
@Test
18+
void givenAnInputStream_whenGetWriteLock_throwNonWritableChannelException() {
1819
assertThrows(NonWritableChannelException.class, () -> FileLocks.getExclusiveLockFromInputStream());
1920
}
2021

21-
@Test void givenARandomAccessFileWithReadWriteAccess_whenGetWriteLock_lock() throws IOException {
22+
@Test
23+
void givenARandomAccessFileWithReadWriteAccess_whenGetWriteLock_lock() throws IOException {
2224
FileLock lock = FileLocks.getExclusiveLockFromRandomAccessFile(0, 10);
2325
assertNotNull(lock);
2426
assertFalse(lock.isShared());
2527
}
2628

27-
@Test void givenAPath_whenGetExclusiveLock_lock() throws IOException {
29+
@Test
30+
void givenAPath_whenGetExclusiveLock_lock() throws IOException {
2831
FileLock lock = FileLocks.getExclusiveLockFromFileChannelOpen(0, 10);
2932
assertNotNull(lock);
3033
assertFalse(lock.isShared());
3134
}
3235

33-
@Test void givenAFileOutputStream_whenGetSharedLock_throwNonReadableChannelException() {
36+
@Test
37+
void givenAFileOutputStream_whenGetSharedLock_throwNonReadableChannelException() {
3438
assertThrows(NonReadableChannelException.class, FileLocks::getReadLockFromOutputStream);
3539
}
3640

37-
@Test void givenAnInputStream_whenGetSharedLock_lock() throws IOException {
41+
@Test
42+
void givenAnInputStream_whenGetSharedLock_lock() throws IOException {
3843
FileLock lock = FileLocks.getReadLockFromInputStream(0, 10);
3944
assertNotNull(lock);
4045
assertTrue(lock.isShared());
4146
}
4247

43-
@Test void givenARandomAccessFile_whenGetSharedLock_lock() throws IOException {
48+
@Test
49+
void givenARandomAccessFile_whenGetSharedLock_lock() throws IOException {
4450
FileLock lock = FileLocks.getReadLockFromRandomAccessFile(0, 10);
4551
assertNotNull(lock);
4652
assertTrue(lock.isShared());

0 commit comments

Comments
 (0)