1 2 3 package Jt; 4 import java.util.*; 5 import java.lang.reflect.*; 6 import java.beans.*; 7 import java.io.*; 8 9 12 13 public class JtFile extends JtObject { 14 15 String name; 16 FileOutputStream ostream = null; 17 boolean createdir = true; 18 boolean recursive = false; 19 Vector filelist = null; 20 String filter = null; 21 byte[] buffer = null; 22 public final static int BUFFER_SIZE = 1024; public final static int MAX_LENGTH = 1024 * 50; int buffersize = BUFFER_SIZE; 25 26 public JtFile() { 27 } 28 29 33 34 public void setName (String newName) { 35 name = newName; 36 } 37 38 41 42 43 public String getName () { 44 return (name); 45 } 46 47 52 53 public void setCreatedir (boolean newCreatedir) { 54 createdir = newCreatedir; 55 } 56 57 61 62 public boolean getCreatedir () { 63 return (createdir); 64 } 65 66 68 71 72 public Vector getFilelist () { 73 74 return (filelist); 75 } 76 77 78 81 82 83 public void setFilter (String filter) { 84 this.filter = filter; 85 } 86 87 90 91 public String getFilter () { 92 return (filter); 93 } 94 95 101 102 public void setRecursive (boolean recursive) { 103 this.recursive = recursive; 104 } 105 106 111 public boolean getRecursive () { 112 return (recursive); 113 } 114 115 117 void open () { 118 119 try { 120 ostream = new FileOutputStream (name); 121 } catch (Exception e) { 122 handleException (e); 123 } 124 } 125 126 132 133 135 private Object read_file () 136 { 137 FileInputStream istream; 138 File file; 139 int len, offset = 0, i; 140 long file_size; 141 byte buf [] = new byte [buffersize]; 142 143 if (name == null) 144 return (null); 145 146 file = new File (name); 147 if (!file.exists ()) 148 { 149 handleTrace ("JtFile.readfile: file does not exist:" + name); 150 buffer = null; 151 return (null); } 153 154 if (file.length () == 0L) 155 { 156 handleTrace ("JtFile.readfile: empty file" + name); 157 buffer = null; 158 return (null); 159 } 160 161 164 file_size = file.length (); 165 166 if (file_size > MAX_LENGTH) 167 { 168 handleError 169 ("JtFile.readfile: file exceeded maximum length allowed:" + 170 name + ":" + file_size); 171 return (null); 172 } 173 174 buffer = new byte[(int) file_size]; 175 176 try { 177 istream = new FileInputStream (name); 178 offset = 0; 179 180 while ((len = istream.read (buf, 0, buffersize)) > 0) 181 { 182 for (i = 0; i < len; i++) 183 { 184 185 if (offset + i >= file_size) 186 { 187 handleError ("JtFile.readfile: file size mismatch" + 188 name); 189 buffer = null; 190 istream.close (); 191 return (null); 192 193 } 194 buffer[offset + i] = buf[i]; 195 } 196 offset += len; 197 } 198 199 if (offset != file_size) 200 handleWarning ("JtFile.readfile: file size mismatch" + 201 name); 203 istream.close (); 204 205 } catch (Exception e) { 206 buffer = null; 207 handleException (e); 208 } 209 210 if (buffer == null) 211 return (null); 212 213 218 return (this); 219 } 220 221 223 void create_directory () { 224 225 create_dir (name); 226 227 } 228 229 231 void write (byte buffer[], int len) { 232 233 if (ostream == null) 234 return; 235 236 try { 237 ostream.write (buffer, 0, len); 238 } catch (Exception e) { 239 handleException (e); 240 } 241 242 } 243 244 246 void destroy () { 247 if (ostream != null) 248 close (); 249 250 } 251 252 253 255 void close () { 256 257 if (ostream == null) 258 return; 259 260 try { 261 ostream.close (); 262 } catch (Exception e) { 263 System.err.println (e); 264 } 265 ostream = null; } 267 268 void create_dir (String name) { 269 File file; 270 String parentpath; 271 272 if (name == null) 273 return; 274 275 file = new File (name); 276 if (file == null) { 277 handleError ("JtFile: unable to create File object:" + name); 278 return; 279 } 280 281 if (!file.exists ()) 282 if (!file.mkdirs ()) 283 handleError ("JtFile: unable to create directories:" + 284 name); 285 } 286 287 288 void create_parent_dir (String name) { 289 File file; 290 String parentpath; 291 292 if (name == null) 293 return; 294 295 file = new File (name); 296 if (file == null) { 297 handleError ("JtFile: unable to create File object:" + name); 298 return; 299 } 300 301 parentpath = file.getParent (); 302 if (parentpath == null) 303 return; 304 305 file = new File (parentpath); 307 if (file == null) { 308 handleError ("JtFile: unable to get parent File:" + name); 309 return; 310 } 311 312 if (!file.exists ()) 313 if (!file.mkdirs ()) 314 handleError ("JtFile: unable to create directories:" + 315 parentpath); 316 } 317 318 319 321 void dir () { 322 int i; 323 File Dir; 324 String files[]; 325 String ext = filter; 327 if (name == null) 328 return; 329 330 332 if (filelist == null) 333 filelist = new Vector (); 334 335 if (filelist == null) 336 return; 337 else 338 filelist.removeAllElements (); 339 340 Dir = new File (name); 341 342 if (!Dir.isDirectory ()) { 343 handleError ("File.dir: invalid directory:" + 344 name); 345 return; 346 } 347 348 files = Dir.list (); 349 350 i = 0; 351 while (i < files.length) { 352 353 if (ext == null || (ext != null && files[i].endsWith (ext))) { 354 handleTrace ("JtFile.dir:" + files[i]); 355 filelist.addElement (files[i]); 356 } 357 i++; 358 } 359 } 360 361 364 void ls () { 365 int i; 366 File Dir; 367 String files[]; 368 String ext = filter; String tmp; 370 371 if (name == null) 372 return; 373 374 376 if (filelist == null) 377 filelist = new Vector (); 378 379 if (filelist == null) 380 return; 381 else 382 filelist.removeAllElements (); 383 384 Dir = new File (name); 385 386 if (!Dir.isDirectory ()) { 387 handleError ("File.ls: invalid directory:" + 388 name); 389 return; 390 } 391 392 files = Dir.list (); 393 394 i = 0; 395 396 while (i < files.length) { 397 398 tmp = name + "/" + files[i]; 399 if (ext == null || (ext != null && files[i].endsWith (ext))) { 400 handleTrace ("JtFile.ls:" + tmp); 401 filelist.addElement (tmp); 402 } 403 i++; 404 } 405 } 406 407 410 void find_extension (String dir, String extension) { 411 412 413 File Dir, file; 414 String tmp; 415 String files[]; 416 int i; 417 418 419 if (dir == null || extension == null) return; 421 422 if (filelist == null) 424 filelist = new Vector (); 425 426 if (filelist == null) 427 return; 428 429 Dir = new File (dir); 430 431 if (!Dir.isDirectory ()) { 432 handleError ("File.find_extension: invalid directory:" + 433 dir); 434 return; 435 } 436 437 files = Dir.list (); 438 439 i = 0; 440 while (i < files.length) { 441 442 tmp = dir + "/" + files[i]; 443 if (files[i].endsWith (extension)) { 445 handleTrace ("JtFile.find_extension:" + tmp); 446 filelist.addElement (tmp); 448 } 449 450 i++; 451 file = new File (tmp); 452 if (file.isDirectory ()) 453 find_extension (tmp, extension); 455 456 } 457 458 459 } 460 461 464 void find_prefix (String dir, String prefix) { 465 466 467 File Dir, file; 468 String tmp; 469 String files[]; 470 int i; 471 472 473 if (dir == null || prefix == null) return; 475 476 if (filelist == null) 478 filelist = new Vector (); 479 480 if (filelist == null) 481 return; 482 483 Dir = new File (dir); 484 485 if (!Dir.isDirectory ()) { 486 handleError ("File.find_prefix: invalid directory:" + 487 dir); 488 return; 489 } 490 491 files = Dir.list (); 492 493 i = 0; 494 while (i < files.length) { 495 496 tmp = dir + "/" + files[i]; 497 if (files[i].startsWith (prefix)) { 499 handleTrace ("JtFile.find_prefix:" + tmp); 500 filelist.addElement (tmp); 502 } 503 504 i++; 505 file = new File (tmp); 506 if (file.isDirectory ()) 507 find_prefix (tmp, prefix); 509 510 } 511 512 513 } 514 515 517 void find (String dir, String fname) { 518 519 520 File Dir, file; 521 String tmp; 522 String files[]; 523 int i; 524 525 526 if (name == null || fname == null) 527 return; 528 529 if (filelist == null) 531 filelist = new Vector (); 532 533 if (filelist == null) 534 return; 535 536 Dir = new File (dir); 537 538 if (!Dir.isDirectory ()) { 539 handleError ("File.find: invalid directory:" + 540 name); 541 return; 542 } 543 544 files = Dir.list (); 545 546 i = 0; 547 while (i < files.length) { 548 549 tmp = dir + "/" + files[i]; 550 if (fname.equals (files[i])) { 551 handleTrace ("JtFile.find:" + tmp); 552 filelist.addElement (tmp); 553 } 555 556 i++; 557 file = new File (tmp); 558 if (file.isDirectory ()) 559 find (tmp, fname); 560 561 562 } 563 564 565 } 566 567 569 private void read_lines (JtMessage ev) { 570 String line; 571 JtMessage e; 572 573 if (name == null) 574 return; 575 576 try { 577 BufferedReader d = new BufferedReader 578 (new FileReader (name)); 579 580 while ((line = d.readLine ()) != null) { 581 e = new JtMessage (); 583 e.setMsgId("JtMESSAGE"); 584 e.setMsgSubject (ev.getMsgId ()); 585 e.setMsgContent (line); 586 if (ev.getMsgReplyTo () != null) 587 this.sendMessage (ev.getMsgReplyTo (), e); 588 } 589 590 } catch (Exception ex) { 591 handleException (ex); 592 } 593 } 594 595 609 610 public Object processMessage (Object message) { 611 612 String msgid = null; 613 byte buffer[]; 614 JtBuffer buf; 615 File file; 616 JtMessage e = (JtMessage) message; 617 618 if (e == null) 619 return null; 620 621 msgid = (String ) e.getMsgId (); 622 623 if (msgid == null) 624 return null; 625 626 if (msgid.equals ("JtREMOVE")) { 628 return (null); 629 } 630 631 if (msgid.equals ("JtOPEN")) { 632 633 635 if (name == null) 636 return null; 637 638 if (createdir) { 639 create_parent_dir (name); 640 } 641 open (); 642 return null; 643 } 644 645 if (msgid.equals ("JtCREATE_DIRECTORY")) { 646 create_directory (); 647 return null; 648 } 649 650 if (msgid.equals ("JtCREATE_PARENT_DIRECTORY")) { 651 652 if (name == null) 653 return null; 654 655 create_parent_dir (name); 656 return null; 657 } 658 659 if (msgid.equals ("JtCLOSE")) { 660 close (); 661 return null; 662 } 663 664 if (msgid.equals ("JtREAD_FILE")) { 665 return (read_file ()); 666 } 667 668 if (msgid.equals ("JtCONVERT_TO_STRING")) { 669 read_file (); 670 if (this.buffer == null) 671 return (null); 672 return (new String (this.buffer)); 673 } 674 675 if (msgid.equals ("JtREAD_LINES")) { 676 read_lines (e); 677 return (null); 678 } 679 680 if (msgid.equals ("JtCLEANUP")) { 681 if (name == null) 682 return (null); 683 684 cleanup (name); 685 return (null); 686 } 687 688 700 701 if (msgid.equals ("JtDATA_BUFFER")) { 702 if (e.getMsgContent () == null) 703 return null; 704 705 buf = (JtBuffer) e.getMsgContent (); 706 buffer = buf.getBuffer (); 707 handleTrace ("JtFile: writing " + buffer.length + " bytes"); 708 write (buffer, buffer.length); 709 return null; 710 } 711 712 if (msgid.equals ("JtWRITEBYTE")) { 713 if (e.getMsgContent () == null) 714 return null; 715 716 buffer = (byte[]) e.getMsgContent (); 717 handleTrace ("JtFile: writing " + buffer.length + " bytes"); 719 write (buffer, buffer.length); 720 return null; 721 } 722 723 if (msgid.equals ("JtREMOVE")) { 724 if (name == null) { 725 handleError ("JtFile: Invalid attribute (name):" 726 + name); 727 return null; 728 } 729 file = new File (name); 730 file.delete (); 731 return null; 732 } 733 734 if (msgid.equals ("JtDIR")) { 735 dir (); 736 return (null); 737 } 738 739 741 if (msgid.equals ("JtLS")) { 742 ls (); 743 return (null); 744 } 745 746 747 if (msgid.equals ("JtFIND")) { 748 749 if (filelist != null) 750 filelist.removeAllElements (); 751 752 find (name, (String ) e.getMsgContent ()); 753 return (null); 754 } 755 756 if (msgid.equals ("JtFIND_PREFIX")) { 757 758 if (filelist != null) 759 filelist.removeAllElements (); 760 761 find_prefix (name, (String ) e.getMsgContent ()); 762 return (null); 763 } 764 765 if (msgid.equals ("JtFIND_EXTENSION")) { 766 767 if (filelist != null) 768 filelist.removeAllElements (); 769 770 find_extension (name, (String ) e.getMsgContent ()); 771 return (null); 772 } 773 774 775 handleError ("JtFile.processMessage: invalid message id:" + msgid); 776 return (null); 777 778 } 779 780 782 void cleanup (String dname) { 783 File dir, file; 784 int i; 785 String files[]; 786 String tmp; 787 788 if (dname == null) 789 return; 790 791 dir = new File (dname); 792 793 if (!dir.isDirectory ()) { 794 handleWarning ("File.cleanup: invalid directory:" + 795 dname); 796 return; 797 } 798 799 files = dir.list (); 800 801 i = 0; 802 while (i < files.length) { 803 tmp = dname + "/" + files[i++]; file = new File (tmp); 805 if (file.isDirectory ()) 806 if (!recursive) 807 continue; 808 else { 809 handleTrace ("JtFile.cleanup (recursive): cleaning " + tmp); 810 cleanup (tmp); 811 } 812 handleTrace ("JtFile.cleanup: deleting " + tmp); 813 file.delete (); 814 } 815 } 816 817 818 819 822 823 public static void main(String [] args) { 824 825 JtObject main = new JtObject (); 826 JtMessage msg; 827 File tmp; 828 JtFile jfile; 829 830 832 834 main.createObject ("Jt.JtFile", "file"); 835 main.setValue ("file", "name", "JtFile"); 836 main.setValue ("file", "createdir", "true"); 837 838 msg = (JtMessage) main.createObject ("Jt.JtMessage", "message"); 839 842 843 845 main.setValue ("message", "msgId", "JtOPEN"); 846 847 tmp = new File ("JtFile"); 848 849 main.sendMessage ("file", "message"); 850 main.setValue ("message", "msgId", "JtCLOSE"); 851 main.sendMessage ("file", "message"); 852 853 if (!tmp.exists ()) 854 System.err.println ("JtFile(JtOPEN): FAILED"); 855 else 856 System.err.println ("JtFile(JtOPEN): GO"); 857 858 860 main.setValue ("message", "msgId", "JtREMOVE"); 861 main.sendMessage ("file", "message"); 862 863 tmp = new File ("JtFile"); 864 if (!tmp.exists ()) 865 System.err.println ("JtFile(JtREMOVE): GO"); 866 else { 867 System.err.println ("JtFile(JtREMOVE): FAILED"); 868 } 869 870 872 main.setValue ("file", "name", "/tmp/JtFile/JtFile"); 873 main.setValue ("file", "createdir", "true"); 874 main.setValue ("message", "msgId", "JtOPEN"); 875 main.sendMessage ("file", "message"); 876 main.setValue ("message", "msgId", "JtCLOSE"); 877 main.sendMessage ("file", "message"); 878 879 tmp = new File ("/tmp/JtFile/JtFile"); 880 if (!tmp.exists ()) 881 main.handleError ("JtFile(JtOPEN/creatdir=true): FAILED"); 882 else 883 System.err.println ("JtFile(JtOPEN/createdir=true): GO"); 884 885 886 main.setValue ("message", "msgId", "JtREMOVE"); 887 main.sendMessage ("file", "message"); 888 if (!tmp.exists ()) 889 System.err.println ("JtFile(JtREMOVE): GO"); 890 else 891 main.handleError ("JtFile(JtREMOVE): FAILED"); 892 893 899 900 901 913 914 main.setValue ("file", "name", "/tmp/tmpdirectory"); 915 main.setValue ("message", "msgId", "JtCREATE_DIRECTORY"); 916 main.sendMessage ("file", "message"); 917 918 920 tmp = new File ("/tmp/tmpdirectory"); 921 if (!tmp.exists ()) 922 main.handleError ("JtFile(JtCREATE_DIRECTORY): FAILED"); 923 else 924 System.err.println ("JtFile(JtCREATE_DIRECTORY): GO"); 925 926 928 main.setValue ("file", "name", "/tmp"); 929 main.setValue ("message", "msgId", "JtFIND"); 930 main.setValue ("message", "msgContent", "tmpdirectory"); 931 main.sendMessage ("file", "message"); 932 933 934 main.setValue ("file", "name", "/tmp"); 935 939 940 942 main.setValue ("message", "msgId", "JtLS"); 943 main.sendMessage ("file", "message"); 944 945 main.removeObject ("file"); 946 947 958 } 959 960 } 961 962 963
| Popular Tags
|