11package com .baeldung .lock ;
22
3+ import org .slf4j .Logger ;
4+ import org .slf4j .LoggerFactory ;
5+
36import java .io .FileInputStream ;
47import java .io .FileOutputStream ;
58import java .io .IOException ;
1417import java .nio .file .Path ;
1518import java .nio .file .StandardOpenOption ;
1619
17- import org .slf4j .Logger ;
18- import org .slf4j .LoggerFactory ;
19-
20- import com .google .common .base .Charsets ;
21-
2220public 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 ;
0 commit comments