1 34 35 36 package com.micronova.util.codec; 37 38 import com.micronova.util.*; 39 import java.io.*; 40 import java.util.*; 41 import java.util.regex.*; 42 import javax.activation.*; 43 44 45 46 public class CodecFile extends Codec 47 { 48 49 50 public static Object list(Object object) 51 { 52 Object result = null; 53 54 File file = TypeUtil.isFile(object); 55 56 if (file != null) 57 { 58 result = TypeUtil.isList(file.list()); 59 } 60 61 return result; 62 } 63 64 65 66 public static Object listFiles(Object object) 67 { 68 Object result = null; 69 70 File file = TypeUtil.isFile(object); 71 72 if (file != null) 73 { 74 result = TypeUtil.isList(file.listFiles()); 75 } 76 77 return result; 78 } 79 80 81 82 public static Object delete(Object object) 83 { 84 Object result = null; 85 86 File file = TypeUtil.isFile(object); 87 88 if (file != null) 89 { 90 result = new Boolean (file.delete()); 91 } 92 93 return result; 94 } 95 96 97 98 public static Object rename(Object object, Object renameTo) 99 { 100 Object result = null; 101 102 File file = TypeUtil.isFile(object); 103 File to = TypeUtil.isFile(renameTo); 104 105 if ((file != null) && (to != null)) 106 { 107 try 108 { 109 if (file.renameTo(to)) 110 { 111 result = to; 112 } 113 } 114 catch (Exception e) 115 { 116 } 117 } 118 119 return result; 120 } 121 122 123 124 public static Object read(Object object, Object encoding) 125 { 126 Object result = null; 127 128 InputStream inputStream = null; 129 Reader reader = null; 130 131 try 132 { 133 if (object instanceof InputStream) 134 { 135 inputStream = (InputStream)object; 136 } 137 else if (object instanceof Reader) 138 { 139 reader = (Reader)object; 140 } 141 else 142 { 143 File file = TypeUtil.isFile(object); 144 145 if (file != null) 146 { 147 inputStream = new FileInputStream(file); 148 } 149 } 150 151 if (inputStream != null) 152 { 153 result = new String (IOUtil.readAll(inputStream), encoding.toString()); 154 } 155 156 if (reader != null) 157 { 158 result = new String (IOUtil.readAll(reader)); 159 } 160 } 161 catch (Exception e) 162 { 163 result = null; 164 } 165 finally 166 { 167 IOUtil.tryClose(inputStream); 168 IOUtil.tryClose(reader); 169 } 170 171 return result; 172 } 173 174 175 176 public static Object read(Object object) 177 { 178 return read(object, "iso-8859-1"); 179 } 180 181 182 private static Object write(Object object, Object fileObject, String encoding, boolean doesAppend) 183 { 184 Object result = null; 185 186 if (object != null) 187 { 188 File file = TypeUtil.isFile(fileObject); 189 190 if (file != null) 191 { 192 OutputStream outputStream = null; 193 Writer writer = null; 194 195 try 196 { 197 if (object instanceof Reader) 198 { 199 writer = new FileWriter(file, doesAppend); 200 201 IOUtil.copy((Reader)object, writer); 202 203 writer.close(); 204 205 writer = null; 206 207 result = object; 208 } 209 else if (object instanceof InputStream) 210 { 211 outputStream = new FileOutputStream(file, doesAppend); 212 213 IOUtil.copy((InputStream)object, outputStream); 214 215 outputStream.close(); 216 217 outputStream = null; 218 219 result = object; 220 } 221 else 222 { 223 outputStream = new FileOutputStream(file, doesAppend); 224 225 outputStream.write(object.toString().getBytes(encoding)); 226 227 outputStream.close(); 228 229 outputStream = null; 230 231 result = object; 232 } 233 } 234 catch (Exception e) 235 { 236 result = null; 237 } 238 finally 239 { 240 IOUtil.tryClose(outputStream); 241 IOUtil.tryClose(writer); 242 } 243 } 244 } 245 246 return result; 247 } 248 249 250 public static Object write(Object object, Object fileObject, Object encoding) 251 { 252 return write(object, fileObject, encoding.toString(), false); 253 } 254 255 256 public static Object write(Object object, Object fileObject) 257 { 258 return write(object, fileObject, "iso-8859-1", false); 259 } 260 261 262 public static Object append(Object object, Object fileObject, Object encoding) 263 { 264 return write(object, fileObject, encoding.toString(), true); 265 } 266 267 268 public static Object append(Object object, Object fileObject) 269 { 270 return write(object, fileObject, "iso-8859-1", true); 271 } 272 273 274 275 public static Object mkdir(Object object, Object parents) 276 { 277 Object result = null; 278 279 File file = TypeUtil.isFile(object); 280 281 if (file != null) 282 { 283 try 284 { 285 if ((parents == null) ? file.mkdir() : file.mkdirs()) 286 { 287 result = file; 288 } 289 } 290 catch (Exception e) 291 { 292 } 293 } 294 295 return result; 296 } 297 298 299 300 public static Object mkdir(Object object) 301 { 302 return mkdir(object, null); 303 } 304 305 306 307 public static Object mkdirs(Object object) 308 { 309 return mkdir(object, Boolean.TRUE); 310 } 311 312 313 314 public static Object type(Object fileObject) 315 { 316 File file = TypeUtil.isFile(fileObject); 317 318 if (file != null) 319 { 320 return (new FileDataSource(file)).getContentType(); 321 } 322 323 return null; 324 } 325 326 327 328 public static Object tempfile(Object object) 329 { 330 if (object == null) 331 { 332 object = "temp*"; 333 } 334 335 try 336 { 337 String prefix = ""; 338 String suffix = ""; 339 340 String [] part = object.toString().split("[*]"); 341 342 int partLength = part.length; 343 344 if (partLength >= 1) 345 { 346 prefix = part[0]; 347 } 348 349 if (partLength >= 2) 350 { 351 suffix = part[1]; 352 } 353 354 File directory = null; 355 356 int slashIndex = prefix.lastIndexOf('/'); 357 358 if (slashIndex >= 0) 359 { 360 String directoryPrefix = prefix.substring(0, slashIndex); 361 362 directory = new File(directoryPrefix); 363 364 prefix = prefix.substring(slashIndex + 1); 365 } 366 367 if (directory != null) 368 { 369 object = File.createTempFile(prefix, suffix, directory); 370 } 371 else 372 { 373 object = File.createTempFile(prefix, suffix); 374 } 375 } 376 catch (Exception e) 377 { 378 object = null; 379 } 380 381 return object; 382 } 383 384 385 386 public static Object close(Object object) throws Exception 387 { 388 if (object instanceof InputStream) 389 { 390 ((InputStream)object).close(); 391 } 392 else if (object instanceof OutputStream) 393 { 394 ((OutputStream)object).close(); 395 } 396 else if (object instanceof Writer) 397 { 398 ((Writer)object).close(); 399 } 400 else if (object instanceof Reader) 401 { 402 ((Reader)object).close(); 403 } 404 405 return object; 406 } 407 } 408 | Popular Tags |