Source for gnu.gcj.convert.Convert

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