OLD | NEW |
1 library java.io; | 1 library java.io; |
2 | 2 |
3 import "dart:io"; | 3 import "dart:io"; |
4 | 4 |
5 import 'package:path/path.dart' as pathos; | 5 import 'package:path/path.dart' as path; |
6 | 6 |
7 import 'java_core.dart' show JavaIOException; | 7 import 'java_core.dart' show JavaIOException; |
8 | 8 |
9 class JavaFile { | 9 class JavaFile { |
| 10 static path.Context pathContext = path.context; |
10 static final String separator = Platform.pathSeparator; | 11 static final String separator = Platform.pathSeparator; |
11 static final int separatorChar = Platform.pathSeparator.codeUnitAt(0); | 12 static final int separatorChar = Platform.pathSeparator.codeUnitAt(0); |
12 String _path; | 13 String _path; |
13 JavaFile(String path) { | 14 JavaFile(String path) { |
14 _path = path; | 15 _path = path; |
15 } | 16 } |
16 JavaFile.fromUri(Uri uri) : this(pathos.fromUri(uri)); | 17 JavaFile.fromUri(Uri uri) : this(pathContext.fromUri(uri)); |
17 JavaFile.relative(JavaFile base, String child) { | 18 JavaFile.relative(JavaFile base, String child) { |
18 if (child.isEmpty) { | 19 if (child.isEmpty) { |
19 this._path = base._path; | 20 this._path = base._path; |
20 } else { | 21 } else { |
21 this._path = pathos.join(base._path, child); | 22 this._path = pathContext.join(base._path, child); |
22 } | 23 } |
23 } | 24 } |
24 int get hashCode => _path.hashCode; | 25 int get hashCode => _path.hashCode; |
25 bool operator ==(other) { | 26 bool operator ==(other) { |
26 return other is JavaFile && other._path == _path; | 27 return other is JavaFile && other._path == _path; |
27 } | 28 } |
| 29 |
28 bool exists() { | 30 bool exists() { |
29 if (_newFile().existsSync()) { | 31 if (_newFile().existsSync()) { |
30 return true; | 32 return true; |
31 } | 33 } |
32 if (_newDirectory().existsSync()) { | 34 if (_newDirectory().existsSync()) { |
33 return true; | 35 return true; |
34 } | 36 } |
35 return false; | 37 return false; |
36 } | 38 } |
| 39 |
37 JavaFile getAbsoluteFile() => new JavaFile(getAbsolutePath()); | 40 JavaFile getAbsoluteFile() => new JavaFile(getAbsolutePath()); |
38 String getAbsolutePath() { | 41 String getAbsolutePath() { |
39 String path = pathos.absolute(_path); | 42 String path = pathContext.absolute(_path); |
40 path = pathos.normalize(path); | 43 path = pathContext.normalize(path); |
41 return path; | 44 return path; |
42 } | 45 } |
| 46 |
43 JavaFile getCanonicalFile() => new JavaFile(getCanonicalPath()); | 47 JavaFile getCanonicalFile() => new JavaFile(getCanonicalPath()); |
44 String getCanonicalPath() { | 48 String getCanonicalPath() { |
45 try { | 49 try { |
46 return _newFile().resolveSymbolicLinksSync(); | 50 return _newFile().resolveSymbolicLinksSync(); |
47 } catch (e) { | 51 } catch (e) { |
48 throw new JavaIOException('IOException', e); | 52 throw new JavaIOException('IOException', e); |
49 } | 53 } |
50 } | 54 } |
51 String getName() => pathos.basename(_path); | 55 |
| 56 String getName() => pathContext.basename(_path); |
52 String getParent() { | 57 String getParent() { |
53 var result = pathos.dirname(_path); | 58 var result = pathContext.dirname(_path); |
54 // "." or "/" or "C:\" | 59 // "." or "/" or "C:\" |
55 if (result.length < 4) return null; | 60 if (result.length < 4) return null; |
56 return result; | 61 return result; |
57 } | 62 } |
| 63 |
58 JavaFile getParentFile() { | 64 JavaFile getParentFile() { |
59 var parent = getParent(); | 65 var parent = getParent(); |
60 if (parent == null) return null; | 66 if (parent == null) return null; |
61 return new JavaFile(parent); | 67 return new JavaFile(parent); |
62 } | 68 } |
| 69 |
63 String getPath() => _path; | 70 String getPath() => _path; |
64 bool isDirectory() { | 71 bool isDirectory() { |
65 return _newDirectory().existsSync(); | 72 return _newDirectory().existsSync(); |
66 } | 73 } |
| 74 |
67 bool isExecutable() { | 75 bool isExecutable() { |
68 return _newFile().statSync().mode & 0x111 != 0; | 76 return _newFile().statSync().mode & 0x111 != 0; |
69 } | 77 } |
| 78 |
70 bool isFile() { | 79 bool isFile() { |
71 return _newFile().existsSync(); | 80 return _newFile().existsSync(); |
72 } | 81 } |
| 82 |
73 int lastModified() { | 83 int lastModified() { |
74 try { | 84 try { |
75 return _newFile().lastModifiedSync().millisecondsSinceEpoch; | 85 return _newFile().lastModifiedSync().millisecondsSinceEpoch; |
76 } catch (exception) { | 86 } catch (exception) { |
77 return -1; | 87 return -1; |
78 } | 88 } |
79 } | 89 } |
| 90 |
80 List<JavaFile> listFiles() { | 91 List<JavaFile> listFiles() { |
81 var files = <JavaFile>[]; | 92 var files = <JavaFile>[]; |
82 var entities = _newDirectory().listSync(); | 93 var entities = _newDirectory().listSync(); |
83 for (FileSystemEntity entity in entities) { | 94 for (FileSystemEntity entity in entities) { |
84 files.add(new JavaFile(entity.path)); | 95 files.add(new JavaFile(entity.path)); |
85 } | 96 } |
86 return files; | 97 return files; |
87 } | 98 } |
| 99 |
88 String readAsStringSync() => _newFile().readAsStringSync(); | 100 String readAsStringSync() => _newFile().readAsStringSync(); |
89 String toString() => _path.toString(); | 101 String toString() => _path.toString(); |
90 Uri toURI() { | 102 Uri toURI() { |
91 String path = getAbsolutePath(); | 103 String path = getAbsolutePath(); |
92 return pathos.toUri(path); | 104 return pathContext.toUri(path); |
93 } | 105 } |
| 106 |
94 Directory _newDirectory() => new Directory(_path); | 107 Directory _newDirectory() => new Directory(_path); |
95 File _newFile() => new File(_path); | 108 File _newFile() => new File(_path); |
96 } | 109 } |
97 | 110 |
98 class JavaSystemIO { | 111 class JavaSystemIO { |
99 static Map<String, String> _properties = new Map(); | 112 static Map<String, String> _properties = new Map(); |
100 static String getenv(String name) => Platform.environment[name]; | 113 static String getenv(String name) => Platform.environment[name]; |
101 static String getProperty(String name) { | 114 static String getProperty(String name) { |
102 { | 115 { |
103 String value = _properties[name]; | 116 String value = _properties[name]; |
104 if (value != null) { | 117 if (value != null) { |
105 return value; | 118 return value; |
106 } | 119 } |
107 } | 120 } |
108 if (name == 'os.name') { | 121 if (name == 'os.name') { |
109 return Platform.operatingSystem; | 122 return Platform.operatingSystem; |
110 } | 123 } |
111 if (name == 'line.separator') { | 124 if (name == 'line.separator') { |
112 if (Platform.isWindows) { | 125 if (Platform.isWindows) { |
113 return '\r\n'; | 126 return '\r\n'; |
114 } | 127 } |
115 return '\n'; | 128 return '\n'; |
116 } | 129 } |
117 if (name == 'com.google.dart.sdk') { | 130 if (name == 'com.google.dart.sdk') { |
118 String exec = Platform.executable; | 131 String exec = Platform.executable; |
119 if (exec.length != 0) { | 132 if (exec.length != 0) { |
120 String sdkPath; | 133 String sdkPath; |
121 // may be "xcodebuild/ReleaseIA32/dart" with "sdk" sibling | 134 // may be "xcodebuild/ReleaseIA32/dart" with "sdk" sibling |
122 { | 135 { |
123 var outDir = pathos.dirname(pathos.dirname(exec)); | 136 var outDir = |
124 sdkPath = pathos.join(pathos.dirname(outDir), "sdk"); | 137 JavaFile.pathContext.dirname(JavaFile.pathContext.dirname(exec)); |
| 138 sdkPath = JavaFile.pathContext |
| 139 .join(JavaFile.pathContext.dirname(outDir), "sdk"); |
125 if (new Directory(sdkPath).existsSync()) { | 140 if (new Directory(sdkPath).existsSync()) { |
126 _properties[name] = sdkPath; | 141 _properties[name] = sdkPath; |
127 return sdkPath; | 142 return sdkPath; |
128 } | 143 } |
129 } | 144 } |
130 // probably be "dart-sdk/bin/dart" | 145 // probably be "dart-sdk/bin/dart" |
131 sdkPath = pathos.dirname(pathos.dirname(exec)); | 146 sdkPath = |
| 147 JavaFile.pathContext.dirname(JavaFile.pathContext.dirname(exec)); |
132 _properties[name] = sdkPath; | 148 _properties[name] = sdkPath; |
133 return sdkPath; | 149 return sdkPath; |
134 } | 150 } |
135 } | 151 } |
136 return null; | 152 return null; |
137 } | 153 } |
| 154 |
138 static String setProperty(String name, String value) { | 155 static String setProperty(String name, String value) { |
139 String oldValue = _properties[name]; | 156 String oldValue = _properties[name]; |
140 _properties[name] = value; | 157 _properties[name] = value; |
141 return oldValue; | 158 return oldValue; |
142 } | 159 } |
143 } | 160 } |
OLD | NEW |