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 var path = this.getAbsolutePath(); 125 if (!path) { 126 throw new GPError(module.id, GPError.OBJECT_NOT_FOUND, 0, "File " + this.name + " not found"); 127 } 128 this.is = new java.io.FileInputStream(path); 129 } 130 return this.is; 131 } 132 133 134 135 /** 136 * @private 137 * Return a java.io.FileOutputStream 138 */ 139 File.prototype.getOutputStream = function() { 140 if (typeof(this.os) == "undefined") { 141 var path = this.getAbsolutePath(); 142 if (!path) { 143 throw new GPError(module.id, GPError.OBJECT_NOT_FOUND, 0, "File " + this.name + " not found"); 144 } 145 this.os = new java.io.FileOutputStream(path); 146 } 147 return this.os; 148 } 149 150 151 152 /** 153 * Read complete file into ByteString object 154 * 155 * @type ByteString 156 * @return the binary content 157 */ 158 File.prototype.readAllAsBinary = function() { 159 var is = this.getInputStream(); 160 161 // Determine file size 162 var flen = is.available(); 163 164 // Allocate native byte array 165 var bs = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, flen); 166 167 // Read into byte array 168 var len = is.read(bs); 169 170 this.close(); 171 172 // Allocate JavaScript ByteBuffer from native/wrapped byte array 173 var bb = new ByteBuffer(bs); 174 175 // Convert to JavaScript ByteString 176 var data = bb.toByteString(); 177 178 return data; 179 } 180 181 182 183 /** 184 * Read complete file into String object 185 * 186 * @type String 187 * @return the text content 188 */ 189 File.prototype.readAllAsString = function(encoding) { 190 191 if (typeof(encoding) == "undefined") { 192 encoding = UTF8; 193 } 194 195 return this.readAllAsBinary().toString(encoding); 196 } 197 198 199 200 /** 201 * Write the object to file 202 * 203 * @param{Object} obj to write to file (Using toString() for any other than String and ByteString) 204 * @param{Number} one of UTF8 or ASCII (Default is UTF8) 205 */ 206 File.prototype.writeAll = function(obj, encoding) { 207 if ((typeof(obj) != "string") && !(obj instanceof ByteString)) { 208 obj = obj.toString(); 209 } 210 if (typeof(obj) == "string") { 211 if (typeof(encoding) == "undefined") { 212 encoding = UTF8; 213 } 214 obj = new ByteString(obj, encoding); 215 } 216 217 var os = this.getOutputStream(); 218 os.write(obj); 219 this.close(); 220 } 221 222 223 224 /** 225 * Return list of files contained in the directory referenced by the File object 226 * 227 * @type String[] 228 * @return the list of file names 229 */ 230 File.prototype.list = function() { 231 var list = this.getFile().list(); 232 var jslist = []; 233 234 if (list) { 235 for (var i = 0; i < list.length; i++) { 236 jslist.push(new String(list[i])); 237 } 238 } 239 240 return jslist; 241 } 242 243 244 245 /** 246 * Return the parent file of this file object 247 * 248 * @type File 249 * @return the parent file object or null 250 */ 251 File.prototype.getParentFile = function() { 252 var file = this.getFile(); 253 var parent = file.getParent(); 254 if (parent == null) { 255 return null; 256 } 257 return new File(parent); 258 } 259 260 261 262 File.test = function() { 263 var file = new File("test.bin"); 264 var b = new ByteString("Hello World", ASCII); 265 file.writeAll(b); 266 267 var file = new File("test.bin"); 268 var c = file.readAllAsBinary(); 269 print(c.toString(ASCII)); 270 271 var file = new File("test.txt"); 272 var s = "Hello World"; 273 file.writeAll(s); 274 275 var file = new File("test.txt"); 276 var c = file.readAllAsString(); 277 print(c); 278 279 var dir = file.getParentFile(); 280 print(dir.getAbsolutePath()); 281 print(dir.list()); 282 283 } 284