|
15 | 15 | */ |
16 | 16 | package org.codehaus.plexus.components.io.attributes; |
17 | 17 |
|
| 18 | +import javax.annotation.Nonnull; |
| 19 | +import javax.annotation.Nullable; |
| 20 | + |
18 | 21 | import java.io.File; |
19 | 22 | import java.io.IOException; |
20 | 23 | import java.nio.file.Files; |
|
27 | 30 | import java.util.HashSet; |
28 | 31 | import java.util.Set; |
29 | 32 |
|
30 | | -import javax.annotation.Nonnull; |
31 | | -import javax.annotation.Nullable; |
32 | | - |
33 | 33 | /** |
34 | 34 | * @author Kristian Rosenvold |
35 | 35 | */ |
36 | | -public class AttributeUtils |
37 | | -{ |
| 36 | +public class AttributeUtils { |
38 | 37 | /* |
39 | 38 | Reads last-modified with proper failure handling if something goes wrong. |
40 | 39 | */ |
41 | | - public static long getLastModified( @Nonnull File file ) |
42 | | - { |
43 | | - try |
44 | | - { |
45 | | - BasicFileAttributes basicFileAttributes = Files.readAttributes( file.toPath(), BasicFileAttributes.class ); |
| 40 | + public static long getLastModified(@Nonnull File file) { |
| 41 | + try { |
| 42 | + BasicFileAttributes basicFileAttributes = Files.readAttributes(file.toPath(), BasicFileAttributes.class); |
46 | 43 | return basicFileAttributes.lastModifiedTime().toMillis(); |
| 44 | + } catch (IOException e) { |
| 45 | + throw new RuntimeException(e); |
47 | 46 | } |
48 | | - catch ( IOException e ) |
49 | | - { |
50 | | - throw new RuntimeException( e ); |
51 | | - } |
52 | | - |
53 | 47 | } |
54 | 48 |
|
55 | | - public static void chmod( @Nonnull File file, int mode ) |
56 | | - throws IOException |
57 | | - { |
| 49 | + public static void chmod(@Nonnull File file, int mode) throws IOException { |
58 | 50 | final Path path = file.toPath(); |
59 | | - if ( !Files.isSymbolicLink( path ) ) |
60 | | - { |
61 | | - Files.setPosixFilePermissions( path, getPermissions( mode ) ); |
| 51 | + if (!Files.isSymbolicLink(path)) { |
| 52 | + Files.setPosixFilePermissions(path, getPermissions(mode)); |
62 | 53 | } |
63 | 54 | } |
64 | 55 |
|
65 | 56 | @Nonnull |
66 | | - public static Set<PosixFilePermission> getPermissions( int mode ) |
67 | | - { |
| 57 | + public static Set<PosixFilePermission> getPermissions(int mode) { |
68 | 58 | Set<PosixFilePermission> perms = new HashSet<>(); |
69 | 59 | // add owners permission |
70 | | - if ( ( mode & 0400 ) > 0 ) |
71 | | - { |
72 | | - perms.add( PosixFilePermission.OWNER_READ ); |
| 60 | + if ((mode & 0400) > 0) { |
| 61 | + perms.add(PosixFilePermission.OWNER_READ); |
73 | 62 | } |
74 | | - if ( ( mode & 0200 ) > 0 ) |
75 | | - { |
76 | | - perms.add( PosixFilePermission.OWNER_WRITE ); |
| 63 | + if ((mode & 0200) > 0) { |
| 64 | + perms.add(PosixFilePermission.OWNER_WRITE); |
77 | 65 | } |
78 | | - if ( ( mode & 0100 ) > 0 ) |
79 | | - { |
80 | | - perms.add( PosixFilePermission.OWNER_EXECUTE ); |
| 66 | + if ((mode & 0100) > 0) { |
| 67 | + perms.add(PosixFilePermission.OWNER_EXECUTE); |
81 | 68 | } |
82 | 69 | // add group permissions |
83 | | - if ( ( mode & 0040 ) > 0 ) |
84 | | - { |
85 | | - perms.add( PosixFilePermission.GROUP_READ ); |
| 70 | + if ((mode & 0040) > 0) { |
| 71 | + perms.add(PosixFilePermission.GROUP_READ); |
86 | 72 | } |
87 | | - if ( ( mode & 0020 ) > 0 ) |
88 | | - { |
89 | | - perms.add( PosixFilePermission.GROUP_WRITE ); |
| 73 | + if ((mode & 0020) > 0) { |
| 74 | + perms.add(PosixFilePermission.GROUP_WRITE); |
90 | 75 | } |
91 | | - if ( ( mode & 0010 ) > 0 ) |
92 | | - { |
93 | | - perms.add( PosixFilePermission.GROUP_EXECUTE ); |
| 76 | + if ((mode & 0010) > 0) { |
| 77 | + perms.add(PosixFilePermission.GROUP_EXECUTE); |
94 | 78 | } |
95 | 79 | // add others permissions |
96 | | - if ( ( mode & 0004 ) > 0 ) |
97 | | - { |
98 | | - perms.add( PosixFilePermission.OTHERS_READ ); |
| 80 | + if ((mode & 0004) > 0) { |
| 81 | + perms.add(PosixFilePermission.OTHERS_READ); |
99 | 82 | } |
100 | | - if ( ( mode & 0002 ) > 0 ) |
101 | | - { |
102 | | - perms.add( PosixFilePermission.OTHERS_WRITE ); |
| 83 | + if ((mode & 0002) > 0) { |
| 84 | + perms.add(PosixFilePermission.OTHERS_WRITE); |
103 | 85 | } |
104 | | - if ( ( mode & 0001 ) > 0 ) |
105 | | - { |
106 | | - perms.add( PosixFilePermission.OTHERS_EXECUTE ); |
| 86 | + if ((mode & 0001) > 0) { |
| 87 | + perms.add(PosixFilePermission.OTHERS_EXECUTE); |
107 | 88 | } |
108 | 89 | return perms; |
109 | 90 | } |
110 | 91 |
|
111 | 92 | @Nonnull |
112 | | - public static PosixFileAttributes getPosixFileAttributes( @Nonnull File file ) |
113 | | - throws IOException |
114 | | - { |
115 | | - return Files.readAttributes( file.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS ); |
| 93 | + public static PosixFileAttributes getPosixFileAttributes(@Nonnull File file) throws IOException { |
| 94 | + return Files.readAttributes(file.toPath(), PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS); |
116 | 95 | } |
117 | 96 |
|
118 | 97 | @Nonnull |
119 | | - public static BasicFileAttributes getFileAttributes( @Nonnull File file ) |
120 | | - throws IOException |
121 | | - { |
122 | | - return getFileAttributes( file.toPath() ); |
| 98 | + public static BasicFileAttributes getFileAttributes(@Nonnull File file) throws IOException { |
| 99 | + return getFileAttributes(file.toPath()); |
123 | 100 | } |
124 | 101 |
|
125 | | - public static BasicFileAttributes getFileAttributes( Path path ) |
126 | | - throws IOException |
127 | | - { |
128 | | - if ( isUnix( path ) ) |
129 | | - { |
| 102 | + public static BasicFileAttributes getFileAttributes(Path path) throws IOException { |
| 103 | + if (isUnix(path)) { |
130 | 104 |
|
131 | | - try |
132 | | - { |
133 | | - return Files.readAttributes( path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS ); |
134 | | - } |
135 | | - catch ( UnsupportedOperationException ignore ) |
136 | | - { |
| 105 | + try { |
| 106 | + return Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS); |
| 107 | + } catch (UnsupportedOperationException ignore) { |
137 | 108 | // Maybe ignoring is dramatic. Maybe not. But we do get the basic attrs anyway |
138 | 109 | } |
139 | 110 | } |
140 | | - return Files.readAttributes( path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS ); |
| 111 | + return Files.readAttributes(path, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS); |
141 | 112 | } |
142 | 113 |
|
143 | | - public static boolean isUnix( Path path ) |
144 | | - { |
145 | | - return path.getFileSystem().supportedFileAttributeViews().contains( "unix" ); |
| 114 | + public static boolean isUnix(Path path) { |
| 115 | + return path.getFileSystem().supportedFileAttributeViews().contains("unix"); |
146 | 116 | } |
147 | 117 |
|
148 | 118 | @Nullable |
149 | | - public static FileOwnerAttributeView getFileOwnershipInfo( @Nonnull File file ) |
150 | | - throws IOException |
151 | | - { |
152 | | - try |
153 | | - { |
154 | | - return Files.getFileAttributeView( file.toPath(), FileOwnerAttributeView.class, LinkOption.NOFOLLOW_LINKS ); |
155 | | - } |
156 | | - catch ( UnsupportedOperationException e ) |
157 | | - { |
| 119 | + public static FileOwnerAttributeView getFileOwnershipInfo(@Nonnull File file) throws IOException { |
| 120 | + try { |
| 121 | + return Files.getFileAttributeView(file.toPath(), FileOwnerAttributeView.class, LinkOption.NOFOLLOW_LINKS); |
| 122 | + } catch (UnsupportedOperationException e) { |
158 | 123 | return null; |
159 | 124 | } |
160 | 125 | } |
|
0 commit comments