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