KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > util > IOModes


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2004-2006 Thomas E Enebo <enebo@acm.org>
15  * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
16  * Copyright (C) 2005 Charles O Nutter <headius@headius.com>
17  * Copyright (C) 2006 Evan Buswell <evan@heron.sytes.net>
18  * Copyright (C) 2006 Dave Brosius <dbrosius@mebigfatguy.com>
19  *
20  * Alternatively, the contents of this file may be used under the terms of
21  * either of the GNU General Public License Version 2 or later (the "GPL"),
22  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23  * in which case the provisions of the GPL or the LGPL are applicable instead
24  * of those above. If you wish to allow use of your version of this file only
25  * under the terms of either the GPL or the LGPL, and not to allow others to
26  * use your version of this file under the terms of the CPL, indicate your
27  * decision by deleting the provisions above and replace them with the notice
28  * and other provisions required by the GPL or the LGPL. If you do not delete
29  * the provisions above, a recipient may use your version of this file under
30  * the terms of any one of the CPL, the GPL or the LGPL.
31  ***** END LICENSE BLOCK *****/

32 package org.jruby.util;
33
34 import org.jruby.Ruby;
35
36 /**
37  * @author enebo
38  *
39  */

40 public class IOModes implements Cloneable JavaDoc {
41     public static final int RDONLY = 0;
42     public static final int WRONLY = 1;
43     public static final int RDWR = 2;
44     public static final int CREAT = 64;
45     public static final int EXCL = 128;
46     public static final int NOCTTY = 256;
47     public static final int TRUNC = 512;
48     public static final int APPEND = 1024;
49     public static final int NONBLOCK = 2048;
50     public static final int BINARY = 4096;
51     
52     private Ruby runtime;
53     private int modes;
54     
55     public IOModes(Ruby runtime) {
56         modes = 0;
57         this.runtime = runtime;
58     }
59     
60     public Object JavaDoc clone() {
61         try {
62             return super.clone();
63         } catch (CloneNotSupportedException JavaDoc cnse) {
64             //won't happen
65
return null;
66         }
67     }
68     
69     public IOModes(Ruby runtime, String JavaDoc modesString) {
70         this(runtime, convertModesStringToModesInt(runtime, modesString));
71     }
72     
73     public IOModes(Ruby runtime, long modes) {
74         // TODO: Ruby does not seem to care about invalid numeric mode values
75
// I am not sure if ruby overflows here also...
76
this.modes = (int)modes;
77         this.runtime = runtime;
78     }
79     
80     public boolean isReadable() {
81         return (modes & RDWR) != 0 || modes == RDONLY || modes == BINARY;
82     }
83
84     public boolean isWriteable() {
85         return isWritable();
86     }
87
88     public boolean isBinary() {
89         return (modes & BINARY) == BINARY;
90     }
91
92     public boolean isWritable() {
93         return (modes & RDWR) != 0 || (modes & WRONLY) != 0 || (modes & CREAT) != 0;
94     }
95     
96     public boolean isAppendable() {
97         return (modes & APPEND) != 0;
98     }
99
100     public boolean shouldTruncate() {
101         return (modes & TRUNC) != 0;
102     }
103
104     // TODO: Make sure all open flags are added to this check.
105
public void checkSubsetOf(IOModes superset) {
106         if ((!superset.isReadable() && isReadable()) ||
107             (!superset.isWriteable() && isWriteable()) ||
108             !superset.isAppendable() && isAppendable()) {
109             throw runtime.newErrnoEINVALError("bad permissions");
110         }
111     }
112     
113     // TODO: Make this more intelligible value
114
public String JavaDoc toString() {
115         return ""+modes;
116     }
117     
118     public static int convertModesStringToModesInt(Ruby runtime,
119             String JavaDoc modesString) {
120         int modes = 0;
121         
122         if (modesString.length() == 0) {
123             throw runtime.newArgumentError("illegal access mode");
124         }
125
126         switch (modesString.charAt(0)) {
127         case 'r' :
128             modes |= RDONLY;
129             break;
130         case 'a' :
131             modes |= APPEND;
132             modes |= WRONLY;
133             break;
134         case 'w' :
135             modes |= WRONLY;
136             modes |= TRUNC;
137             break;
138         default :
139             throw runtime.newArgumentError("illegal access mode " + modes);
140         }
141
142         if (modesString.length() > 1) {
143             int i = modesString.charAt(1) == 'b' ? 2 : 1;
144
145             if (modesString.length() > i) {
146                 if (modesString.charAt(i) == '+') {
147                     if ((modes & APPEND) != 0) {
148                         modes = RDWR | APPEND;
149                     } else if ((modes & WRONLY) != 0){
150                         modes = RDWR | TRUNC;
151                     } else {
152                         modes = RDWR;
153                     }
154                 } else {
155                     throw runtime.newArgumentError("illegal access mode " + modes);
156                 }
157             }
158             if (i == 2) {
159                 modes |= BINARY;
160             }
161
162         }
163         return modes;
164     }
165 }
166
Popular Tags