View Javadoc
1   /*
2    * dpkg - Debian Package library and the Debian Package Maven plugin
3    * (c) Copyright 2016 Gerrit Hohl
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  package net.sourceforge.javadpkg.io.impl;
20  
21  import net.sourceforge.javadpkg.io.FileMode;
22  
23  /**
24   * <p>
25   * A {@link FileMode} implementation.
26   * </p>
27   *
28   * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
29   * @version <b>1.0</b>, 12.05.2016 by Gerrit Hohl
30   * @version <b>1.1</b>, 21.06.2018 by Gerrit Hohl
31   */
32  public class FileModeImpl implements FileMode {
33  
34  
35  	/** The mode. */
36  	private int mode;
37  
38  
39  	/**
40  	 * <p>
41  	 * Creates the mode of a file.
42  	 * </p>
43  	 *
44  	 * @param mode
45  	 *            The mode: A number from octal 00000 (decimal: 0) to octal
46  	 *            777777 (decimal: 262143).
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  		// --- Ignore sticky bit ---
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  		// --- Ignore sticky bit ---
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 	 * <p>
103 	 * Adds the flag.
104 	 * </p>
105 	 *
106 	 * @param sb
107 	 *            The {@link StringBuilder}. The flag will be added to it.
108 	 * @param flag
109 	 *            The flag: &quot;r&quot;, &quot;w&quot; or &quot;x&quot;.
110 	 * @param value
111 	 *            The value.
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 }