1 /* Copyright (C) 1999, 2002, 2005 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 package gnu
.gcj
.convert
;
14 static void error (String message
)
16 System
.err
.print("jv-convert: ");
17 System
.err
.println(message
);
18 System
.err
.println("Try `jv-convert --help' for more information.");
24 System
.out
.println("Usage: jv-convert [OPTIONS] [INPUTFILE [OUTPUTFILE]]");
26 System
.out
.println("Convert from one encoding to another.");
28 System
.out
.println(" --encoding FROM");
29 System
.out
.println(" --from FROM use FROM as source encoding name");
30 System
.out
.println(" --to TO use TO as target encoding name");
31 System
.out
.println(" -i FILE read from FILE");
32 System
.out
.println(" -o FILE print output to FILE");
33 System
.out
.println(" --reverse swap FROM and TO encodings");
34 System
.out
.println(" --help print this help, then exit");
35 System
.out
.println(" --version print version number, then exit");
37 System
.out
.println("`-' as a file name argument can be used to refer to stdin or stdout.");
41 static void version ()
43 System
.out
.println("jv-convert ("
44 + System
.getProperty("java.vm.name")
46 + System
.getProperty("java.vm.version"));
48 System
.out
.println("Copyright (C) 2005 Free Software Foundation, Inc.");
49 System
.out
.println("This is free software; see the source for copying conditions. There is NO");
50 System
.out
.println("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
54 static void missing (String arg
)
56 error("missing arg after `" + arg
+ "' option");
59 public static void main (String
[] args
)
63 String inEncodingName
= null;
64 String outEncodingName
= "JavaSrc";
66 boolean reverse
= false;
68 for (int i
= 0; i
< args
.length
; i
++)
71 if (arg
.length() == 0)
72 error("zero-length argument");
73 if (arg
.charAt(0) == '-')
75 if (arg
.equals("-encoding") || arg
.equals("--encoding")
76 || args
.equals("-from") || arg
.equals("--from"))
78 if (++i
== args
.length
) missing(arg
);
79 inEncodingName
= args
[i
];
81 else if (arg
.equals("-to") || arg
.equals("--to"))
83 if (++i
== args
.length
) missing(arg
);
84 outEncodingName
= args
[i
];
86 else if (arg
.equals("-i"))
88 if (++i
== args
.length
) missing(arg
);
91 else if (arg
.equals("-o"))
93 if (++i
== args
.length
) missing(arg
);
96 else if (arg
.equals("-reverse") || arg
.equals("--reverse"))
100 else if (arg
.equals("-help") || arg
.equals("--help"))
104 else if (arg
.equals("-version") || arg
.equals("--version"))
108 else if (arg
.equals("-"))
121 error("too many `-' arguments");
125 error("unrecognized argument `" + arg
+ "'");
140 error("too many filename arguments");
147 String tmp
= inEncodingName
;
148 inEncodingName
= outEncodingName
;
149 outEncodingName
= tmp
;
154 BytesToUnicode inDecoder
155 = inEncodingName
== null ? BytesToUnicode
.getDefaultDecoder()
156 : BytesToUnicode
.getDecoder(inEncodingName
);
157 UnicodeToBytes outEncoder
158 = outEncodingName
== null ? UnicodeToBytes
.getDefaultEncoder()
159 : UnicodeToBytes
.getEncoder(outEncodingName
);
160 InputStream inStream
= inName
.equals("-") ? System
.in
161 : new FileInputStream(inName
);
162 OutputStream outStream
;
163 if (outName
.equals("-"))
164 outStream
= System
.out
;
166 outStream
= new FileOutputStream(outName
);
168 = new InputStreamReader(inStream
, inEncodingName
);
169 OutputStreamWriter out
170 = new OutputStreamWriter(outStream
, outEncodingName
);
171 char[] buffer
= new char[2048];
174 int count
= in
.read(buffer
);
177 out
.write(buffer
, 0, count
);
183 catch (java
.io
.IOException ex
)
185 System
.err
.print("jv-convert exception: ");
186 System
.err
.println(ex
);