1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.javadpkg.io.impl;
20
21 import net.sourceforge.javadpkg.io.FileMode;
22
23
24
25
26
27
28
29
30
31
32 public class FileModeImpl implements FileMode {
33
34
35
36 private int mode;
37
38
39
40
41
42
43
44
45
46
47
48 public FileModeImpl(int mode) {
49 super();
50
51 if ((mode < 0000000) || (mode > 0777777))
52 throw new IllegalArgumentException(
53 "Argument mode is less than octal 00000 (decimal: 0) or greater than octal 777777 (decimal: 262143): octal "
54 + Integer.toOctalString(mode) + " (decimal: " + mode + ")");
55
56 this.mode = mode;
57 }
58
59
60 @Override
61 public int getMode() {
62 return this.mode;
63 }
64
65
66 @Override
67 public String getOctal() {
68 StringBuilder sb;
69
70
71 sb = new StringBuilder();
72
73 sb.append(Integer.toOctalString(this.mode & 0777));
74 while (sb.length() < 3) {
75 sb.insert(0, '0');
76 }
77 return sb.toString();
78 }
79
80
81 @Override
82 public String getText() {
83 StringBuilder sb;
84
85
86 sb = new StringBuilder();
87
88 this.addFlag(sb, 'r', this.isOwnerReadable());
89 this.addFlag(sb, 'w', this.isOwnerWriteable());
90 this.addFlag(sb, 'x', this.isOwnerExecutable());
91 this.addFlag(sb, 'r', this.isGroupReadable());
92 this.addFlag(sb, 'w', this.isGroupWriteable());
93 this.addFlag(sb, 'x', this.isGroupExecutable());
94 this.addFlag(sb, 'r', this.isOtherReadable());
95 this.addFlag(sb, 'w', this.isOtherWriteable());
96 this.addFlag(sb, 'x', this.isOtherExecutable());
97 return sb.toString();
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 private void addFlag(StringBuilder sb, char flag, boolean value) {
114 if (value) {
115 sb.append(flag);
116 } else {
117 sb.append('-');
118 }
119 }
120
121
122 @Override
123 public int getStickyBit() {
124 return ((this.mode >> 9) & 00007);
125 }
126
127
128 @Override
129 public boolean isOwnerReadable() {
130 return ((this.mode & 00400) != 0);
131 }
132
133
134 @Override
135 public boolean isOwnerWriteable() {
136 return ((this.mode & 00200) != 0);
137 }
138
139
140 @Override
141 public boolean isOwnerExecutable() {
142 return ((this.mode & 00100) != 0);
143 }
144
145
146 @Override
147 public boolean isGroupReadable() {
148 return ((this.mode & 00040) != 0);
149 }
150
151
152 @Override
153 public boolean isGroupWriteable() {
154 return ((this.mode & 00020) != 0);
155 }
156
157
158 @Override
159 public boolean isGroupExecutable() {
160 return ((this.mode & 00010) != 0);
161 }
162
163
164 @Override
165 public boolean isOtherReadable() {
166 return ((this.mode & 00004) != 0);
167 }
168
169
170 @Override
171 public boolean isOtherWriteable() {
172 return ((this.mode & 00002) != 0);
173 }
174
175
176 @Override
177 public boolean isOtherExecutable() {
178 return ((this.mode & 00001) != 0);
179 }
180
181
182 }