1:
9:
10: package ;
11: import ;
12:
13: public class Convert
14: {
15: static void error (String message)
16: {
17: System.err.print("jv-convert: ");
18: System.err.println(message);
19: System.err.println("Try `jv-convert --help' for more information.");
20: System.exit(1);
21: }
22:
23: static void help ()
24: {
25: System.out.println("Usage: jv-convert [OPTIONS] [INPUTFILE [OUTPUTFILE]]");
26: System.out.println();
27: System.out.println("Convert from one encoding to another.");
28: System.out.println();
29: System.out.println(" --encoding FROM");
30: System.out.println(" --from FROM use FROM as source encoding name");
31: System.out.println(" --to TO use TO as target encoding name");
32: System.out.println(" -i FILE read from FILE");
33: System.out.println(" -o FILE print output to FILE");
34: System.out.println(" --reverse swap FROM and TO encodings");
35: System.out.println(" --help print this help, then exit");
36: System.out.println(" --version print version number, then exit");
37: System.out.println();
38: System.out.println("`-' as a file name argument can be used to refer to stdin or stdout.");
39: System.exit(0);
40: }
41:
42: static void version ()
43: {
44: System.out.println("jv-convert ("
45: + System.getProperty("java.vm.name")
46: + ") "
47: + System.getProperty("java.vm.version"));
48: System.out.println();
49: System.out.println("Copyright (C) 2009 Free Software Foundation, Inc.");
50: System.out.println("This is free software; see the source for copying conditions. There is NO");
51: System.out.println("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
52: System.exit(0);
53: }
54:
55: static void missing (String arg)
56: {
57: error("missing arg after `" + arg + "' option");
58: }
59:
60: public static void main (String[] args)
61: {
62: String inName = "-";
63: String outName = "-";
64: String inEncodingName = null;
65: String outEncodingName = "JavaSrc";
66: int seenNames = 0;
67: boolean reverse = false;
68:
69: for (int i = 0; i < args.length; i++)
70: {
71: String arg = args[i];
72: if (arg.length() == 0)
73: error("zero-length argument");
74: if (arg.charAt(0) == '-')
75: {
76: if (arg.equals("-encoding") || arg.equals("--encoding")
77: || args.equals("-from") || arg.equals("--from"))
78: {
79: if (++i == args.length) missing(arg);
80: inEncodingName = args[i];
81: }
82: else if (arg.equals("-to") || arg.equals("--to"))
83: {
84: if (++i == args.length) missing(arg);
85: outEncodingName = args[i];
86: }
87: else if (arg.equals("-i"))
88: {
89: if (++i == args.length) missing(arg);
90: inName = args[i];
91: }
92: else if (arg.equals("-o"))
93: {
94: if (++i == args.length) missing(arg);
95: outName = args[i];
96: }
97: else if (arg.equals("-reverse") || arg.equals("--reverse"))
98: {
99: reverse = true;
100: }
101: else if (arg.equals("-help") || arg.equals("--help"))
102: {
103: help ();
104: }
105: else if (arg.equals("-version") || arg.equals("--version"))
106: {
107: version ();
108: }
109: else if (arg.equals("-"))
110: {
111: switch (seenNames)
112: {
113: case 0:
114: inName = "-";
115: seenNames++;
116: break;
117: case 1:
118: outName = "-";
119: seenNames++;
120: break;
121: default:
122: error("too many `-' arguments");
123: }
124: }
125: else
126: error("unrecognized argument `" + arg + "'");
127: }
128: else
129: {
130: switch (seenNames)
131: {
132: case 0:
133: inName = arg;
134: seenNames++;
135: break;
136: case 1:
137: outName = arg;
138: seenNames++;
139: break;
140: default:
141: error("too many filename arguments");
142: }
143: }
144: }
145:
146: if (reverse)
147: {
148: String tmp = inEncodingName;
149: inEncodingName = outEncodingName;
150: outEncodingName = tmp;
151: }
152:
153: try
154: {
155: InputStream inStream = inName.equals("-") ? System.in
156: : new FileInputStream(inName);
157: OutputStream outStream;
158: if (outName.equals("-"))
159: outStream = System.out;
160: else
161: outStream = new FileOutputStream(outName);
162: InputStreamReader in
163: = (inEncodingName == null
164: ? new InputStreamReader(inStream)
165: : new InputStreamReader(inStream, inEncodingName));
166: OutputStreamWriter out
167: = (outEncodingName == null
168: ? new OutputStreamWriter(outStream)
169: : new OutputStreamWriter(outStream, outEncodingName));
170: char[] buffer = new char[2048];
171: for (;;)
172: {
173: int count = in.read(buffer);
174: if (count < 0)
175: break;
176: out.write(buffer, 0, count);
177: }
178:
179: in.close();
180: out.close();
181: }
182: catch (java.io.IOException ex)
183: {
184: System.err.print("jv-convert exception: ");
185: System.err.println(ex);
186: System.exit(-1);
187: }
188: }
189: }