Initial import
[jpf-core.git] / src / classes / java / io / File.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package java.io;
19
20 import gov.nasa.jpf.annotation.FilterField;
21
22 import java.net.URI;
23 import java.net.URISyntaxException;
24 import java.net.URL;
25
26
27 /**
28  * MJI model class for java.io.File
29  *
30  * NOTE - a number of methods are only stubbed out here to make Eclipse compile
31  * JPF code that uses java.io.File (there is no way to tell Eclipse to exclude the
32  * model classes from ths build-path)
33  *
34  * @author Owen O'Malley
35  */
36 public class File
37 {
38   public static final String separator = System.getProperty("file.separator");
39   public static final char separatorChar = separator.charAt(0);
40   public static final String pathSeparator = System.getProperty("path.separator");
41   public static final char pathSeparatorChar = pathSeparator.charAt(0);
42
43   @FilterField int id; // link to the real File object
44   private String filename;
45
46   public File(String filename) {
47     if (filename == null){
48       throw new NullPointerException();
49     }
50     
51     this.filename = filename;
52   }
53
54   public File (String parent, String child) {
55         filename = parent + separator + child;
56   }
57   
58   public File (File parent, String child) {
59     filename = parent.filename + separator + child;
60   }
61   
62   public File(java.net.URI uri) { throw new UnsupportedOperationException(); }
63   
64   public String getName() {
65     int idx = filename.lastIndexOf(separatorChar);
66     if (idx >= 0){
67       return filename.substring(idx+1);
68     } else {
69       return filename;
70     }
71   }
72
73   public String getParent() {
74     int idx = filename.lastIndexOf(separatorChar);
75     if (idx >= 0){
76       return filename.substring(0,idx);
77     } else {
78       return null;
79     }
80   }
81   
82   public int compareTo(File that) {
83     return this.filename.compareTo(that.filename);
84   }
85   
86   @Override
87   public boolean equals(Object o) {
88     if (o instanceof File){
89       File otherFile = (File) o;
90       return filename.equals(otherFile.filename);
91     } else {
92       return false;
93     }
94   }
95   
96   @Override
97   public int hashCode() {
98     return filename.hashCode();
99   }
100   
101   @Override
102   public String toString()  {
103     return filename;
104   }
105   
106   
107   //--- native peer intercepted (hopefully)
108   
109   int getPrefixLength() { return 0; }
110   public native File getParentFile();
111   
112   public String getPath() {
113     return filename;
114   }
115
116   public native boolean isAbsolute();
117   public native String getAbsolutePath();
118   public native File getAbsoluteFile();
119   public native String getCanonicalPath() throws java.io.IOException;
120
121   public native File getCanonicalFile() throws java.io.IOException;
122
123   private native String getURLSpec();
124   public java.net.URL toURL() throws java.net.MalformedURLException {
125     return new URL(getURLSpec());
126   }
127
128   private native String getURISpec();
129   public java.net.URI toURI() {
130     try {
131       return new URI(getURISpec());
132     } catch (URISyntaxException x){
133       return null;
134     }
135   }
136
137   public native boolean canRead();
138   public native boolean canWrite();
139   public native boolean exists();
140   public boolean isDirectory() { return false; }
141   public boolean isFile() { return false; }
142   public boolean isHidden() { return false; }
143   public long lastModified() { return -1L; }
144   public long length() { return -1; }
145   public native boolean createNewFile() throws java.io.IOException;
146   public boolean delete()  { return false; }
147   public void deleteOnExit() {}
148   public String[] list()  { return null; }
149   public String[] list(FilenameFilter fnf)  { return null; }
150   public File[] listFiles()  { return null; }
151   public File[] listFiles(FilenameFilter fnf)  { return null; }
152   public File[] listFiles(FileFilter ff)  { return null; }
153   public boolean mkdir()  { return false; }
154   public boolean mkdirs() { return false; }
155   public boolean renameTo(File f)  { return false; }
156   public boolean setLastModified(long t)  { return false; }
157   public boolean setReadOnly()  { return false; }
158   
159   public static native File[] listRoots();
160   
161   public static File createTempFile(String prefix, String suffix, File dir) throws IOException  {
162     if (prefix == null){
163       throw new NullPointerException();
164     }
165     
166     String tmpDir;
167     if (dir == null){
168       tmpDir = System.getProperty("java.io.tmpdir");
169       if (tmpDir == null){
170         tmpDir = ".";
171       }
172       if (tmpDir.charAt(tmpDir.length()-1) != separatorChar){
173         tmpDir += separatorChar;
174       }
175       
176       if (suffix == null){
177         suffix = ".tmp";
178       }
179     } else {
180       tmpDir = dir.getPath();
181     }
182     
183     return new File(tmpDir + prefix + suffix);
184   }
185   
186   public static File createTempFile(String prefix, String suffix) throws IOException  {
187     return createTempFile(prefix, suffix, null);
188   }
189 }