1 /* Copyright (C) 1999, 2002, 2005, 2006, 2007, 2008, 2009
2 Free Software Foundation
4 This file is part of libgcj.
6 This software is copyrighted work licensed under the terms of the
7 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
10 package gnu
.gcj
.convert
;
15 static void error (String message
)
17 System
.err
.print("jv-convert: ");
18 System
.err
.println(message
);
19 System
.err
.println("Try `jv-convert --help' for more information.");
25 System
.out
.println("Usage: jv-convert [OPTIONS] [INPUTFILE [OUTPUTFILE]]");
27 System
.out
.println("Convert from one encoding to another.");
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");
38 System
.out
.println("`-' as a file name argument can be used to refer to stdin or stdout.");
42 static void version ()
44 System
.out
.println("jv-convert ("
45 + System
.getProperty("java.vm.name")
47 + System
.getProperty("java.vm.version"));
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.");
55 static void missing (String arg
)
57 error("missing arg after `" + arg
+ "' option");
60 public static void main (String
[] args
)
64 String inEncodingName
= null;
65 String outEncodingName
= "JavaSrc";
67 boolean reverse
= false;
69 for (int i
= 0; i
< args
.length
; i
++)
72 if (arg
.length() == 0)
73 error("zero-length argument");
74 if (arg
.charAt(0) == '-')
76 if (arg
.equals("-encoding") || arg
.equals("--encoding")
77 || args
.equals("-from") || arg
.equals("--from"))
79 if (++i
== args
.length
) missing(arg
);
80 inEncodingName
= args
[i
];
82 else if (arg
.equals("-to") || arg
.equals("--to"))
84 if (++i
== args
.length
) missing(arg
);
85 outEncodingName
= args
[i
];
87 else if (arg
.equals("-i"))
89 if (++i
== args
.length
) missing(arg
);
92 else if (arg
.equals("-o"))
94 if (++i
== args
.length
) missing(arg
);
97 else if (arg
.equals("-reverse") || arg
.equals("--reverse"))
101 else if (arg
.equals("-help") || arg
.equals("--help"))
105 else if (arg
.equals("-version") || arg
.equals("--version"))
109 else if (arg
.equals("-"))
122 error("too many `-' arguments");
126 error("unrecognized argument `" + arg
+ "'");
141 error("too many filename arguments");
148 String tmp
= inEncodingName
;
149 inEncodingName
= outEncodingName
;
150 outEncodingName
= tmp
;
155 InputStream inStream
= inName
.equals("-") ? System
.in
156 : new FileInputStream(inName
);
157 OutputStream outStream
;
158 if (outName
.equals("-"))
159 outStream
= System
.out
;
161 outStream
= new FileOutputStream(outName
);
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];
173 int count
= in
.read(buffer
);
176 out
.write(buffer
, 0, count
);
182 catch (java
.io
.IOException ex
)
184 System
.err
.print("jv-convert exception: ");
185 System
.err
.println(ex
);