KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > neu > ccs > jmk > BinaryFileOperator


1 // $Id: BinaryFileOperator.java,v 1.2 2001/12/07 11:41:24 ramsdell Exp $
2

3 // Executes a binary file operation on file operands.
4

5 /*
6  * Copyright 1998 by John D. Ramsdell
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 package edu.neu.ccs.jmk;
24
25 import java.io.*;
26
27 /**
28  * This operator is used to implement binary file commands.
29  * To be useful, subclasses must implement
30  * <code>getName(String)</code> and <code>exec(File, File, PrintWriter)</code>.
31  * @version April 1998
32  * @author John D. Ramsdell
33  */

34 abstract class BinaryFileOperator
35 implements Operator
36 {
37
38   /**
39    * The actual method implemented by a particular file operator
40    * is given by defining this method.
41    * @return true if the file operation completed successfully;
42    * false otherwise
43    */

44   abstract boolean exec(File arg1, File arg2, PrintWriter out);
45
46   /**
47    * Applies the binary file operator to the arguments.
48    * If an argument contains the wild card character '*',
49    * the argument is expanded.
50    * @param arg operands given to the file operator
51    * @param out place to write messages
52    * @exception CommandFailedException if operation failed
53    */

54   public void exec(String JavaDoc[] args, PrintWriter out)
55        throws CommandFailedException
56   {
57     args = FileOperator.glob(args);
58     if (args.length != 2) {
59       String JavaDoc msg = getName() + " expects 2 operands, but got " + args.length;
60       throw new CommandFailedException(msg);
61     }
62     if (!exec(new File(args[0]), new File(args[1]), out)) {
63       String JavaDoc msg = getName() + " " + args[0] + " " + args[1] +
64     " operation failed";
65       throw new CommandFailedException(msg);
66     }
67   }
68
69   /**
70    * An entry point for testing exec commands.
71    * It simply passes the arguments to the exec operation.
72    * @param args operands for the command
73    */

74   public static void main(String JavaDoc[] args) {
75     PrintWriter out = new PrintWriter(System.out, true);
76     BinaryFileOperator operator = new BinaryFileOperator() {
77       boolean exec(File arg1, File arg2, PrintWriter out) {
78     return arg1.renameTo(arg2);
79       }
80       public String JavaDoc getName() {
81     return "rename";
82       }
83     };
84
85     try {
86       operator.exec(args, out);
87     }
88     catch (Throwable JavaDoc t) {
89       System.err.println("Internal error: " + t.getMessage());
90       t.printStackTrace();
91     }
92     out.println("Binary file command " + operator.getName() + " completed");
93   }
94 }
95
Popular Tags