1 /** 2 * --------- 3 * |.##> <##.| Open Smart Card Development Platform (www.openscdp.org) 4 * |# #| 5 * |# #| Copyright (c) 1999-2006 CardContact Software & System Consulting 6 * |'##> <##'| Andreas Schwier, 32429 Minden, Germany (www.cardcontact.de) 7 * --------- 8 * 9 * This file is part of OpenSCDP. 10 * 11 * OpenSCDP is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License version 2 as 13 * published by the Free Software Foundation. 14 * 15 * OpenSCDP is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with OpenSCDP; if not, write to the Free Software 22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 * 24 * @fileoverview General File I/O Class 25 */ 26 27 28 29 /** 30 * Create a reference to a file system object (file or directory) 31 * 32 * @class Class implementing basic support for files 33 * @constructor 34 * @param {String} name relative or absolute file path 35 * @param {Number} location one of GPSystem.CWD, GPSystem.USR, GPSystem.SYS, GPSystem.AUTO to resolve relative names. Default GPSystem.CWD 36 */ 37 function File(name, location) { 38 this.name = name; 39 this.location = location; 40 } 41 42 exports.File = File; 43 44 45 46 /** 47 * Return absolute path 48 * 49 * @param{Number} mode one of GPSystem.AUTO, GPSystem.CWD, GPSystem.USR or GPSystem.SYS. See GPSystem.mapFilename() for details 50 * @type String 51 * @return the absolute path 52 */ 53 File.prototype.getAbsolutePath = function(mode) { 54 if (typeof(mode) == "undefined") { 55 if (typeof(this.location) == "undefined") { 56 mode = GPSystem.AUTO; 57 } else { 58 mode = this.location; 59 } 60 } 61 62 var path = GPSystem.mapFilename(this.name, mode); 63 if (path == null) { 64 return undefined; 65 } 66 return path; 67 } 68 69 70 71 /** 72 * @private 73 * Return a java.io.File object 74 */ 75 File.prototype.getFile = function() { 76 if (typeof(this.file) == "undefined") { 77 var path = this.getAbsolutePath(); 78 if (path) { 79 this.file = new java.io.File(path); 80 } 81 } 82 return this.file; 83 } 84 85 86 87 /** 88 * Return true if file exists 89 */ 90 File.prototype.exists = function() { 91 var f = this.getFile(); 92 93 if (!f) { 94 return false; 95 } 96 return f.exists(); 97 } 98 99 100 101 /** 102 * Close streams associated with file 103 */ 104 File.prototype.close = function() { 105 if (typeof(this.os) != "undefined") { 106 this.os.close(); 107 delete(this.os); 108 } 109 110 if (typeof(this.is) != "undefined") { 111 this.is.close(); 112 delete(this.is); 113 } 114 } 115 116 117 118 /** 119 * @private 120 * Return a java.io.FileInputStream 121 */ 122 File.prototype.getInputStream = function() { 123 if (typeof(this.is) == "undefined") { 124 this.is = new java.io.FileInputStream(this.getAbsolutePath()); 125 } 126 return this.is; 127 } 128 129 130 131 /** 132 * @private 133 * Return a java.io.FileOutputStream 134 */ 135 File.prototype.getOutputStream = function() { 136 if (typeof(this.os) == "undefined") { 137 this.os = new java.io.FileOutputStream(this.getAbsolutePath()); 138 } 139 return this.os; 140 } 141 142 143 144 /** 145 * Read complete file into ByteString object 146 * 147 * @type ByteString 148 * @return the binary content 149 */ 150 File.prototype.readAllAsBinary = function() { 151 var is = this.getInputStream(); 152 153 // Determine file size 154 var flen = is.available(); 155 156 // Allocate native byte array 157 var bs = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, flen); 158 159 // Read into byte array 160 var len = is.read(bs); 161 162 this.close(); 163 164 // Allocate JavaScript ByteBuffer from native/wrapped byte array 165 var bb = new ByteBuffer(bs); 166 167 // Convert to JavaScript ByteString 168 var data = bb.toByteString(); 169 170 return data; 171 } 172 173 174 175 /** 176 * Read complete file into String object 177 * 178 * @type String 179 * @return the text content 180 */ 181 File.prototype.readAllAsString = function(encoding) { 182 183 if (typeof(encoding) == "undefined") { 184 encoding = UTF8; 185 } 186 187 return this.readAllAsBinary().toString(encoding); 188 } 189 190 191 192 /** 193 * Write the object to file 194 * 195 * @param{Object} obj to write to file (Using toString() for any other than String and ByteString) 196 * @param{Number} one of UTF8 or ASCII (Default is UTF8) 197 */ 198 File.prototype.writeAll = function(obj, encoding) { 199 if ((typeof(obj) != "string") && !(obj instanceof ByteString)) { 200 obj = obj.toString(); 201 } 202 if (typeof(obj) == "string") { 203 if (typeof(encoding) == "undefined") { 204 encoding = UTF8; 205 } 206 obj = new ByteString(obj, encoding); 207 } 208 209 var os = this.getOutputStream(); 210 os.write(obj); 211 this.close(); 212 } 213 214 215 216 /** 217 * Return list of files contained in the directory referenced by the File object 218 * 219 * @type String[] 220 * @return the list of file names 221 */ 222 File.prototype.list = function() { 223 var list = this.getFile().list(); 224 var jslist = []; 225 226 if (list) { 227 for (var i = 0; i < list.length; i++) { 228 jslist.push(new String(list[i])); 229 } 230 } 231 232 return jslist; 233 } 234 235 236 237 /** 238 * Return the parent file of this file object 239 * 240 * @type File 241 * @return the parent file object or null 242 */ 243 File.prototype.getParentFile = function() { 244 var file = this.getFile(); 245 var parent = file.getParent(); 246 if (parent == null) { 247 return null; 248 } 249 return new File(parent); 250 } 251 252 253 254 File.test = function() { 255 var file = new File("test.bin"); 256 var b = new ByteString("Hello World", ASCII); 257 file.writeAll(b); 258 259 var file = new File("test.bin"); 260 var c = file.readAllAsBinary(); 261 print(c.toString(ASCII)); 262 263 var file = new File("test.txt"); 264 var s = "Hello World"; 265 file.writeAll(s); 266 267 var file = new File("test.txt"); 268 var c = file.readAllAsString(); 269 print(c); 270 271 var dir = file.getParentFile(); 272 print(dir.getAbsolutePath()); 273 print(dir.list()); 274 275 } 276