chiark / gitweb /
base91-0.6.0.tar.gz downloaed just now
[base91.git] / Java / b91cli.java
1 /*
2  * basE91 command line front-end
3  *
4  * Copyright (c) 2000-2006 Joachim Henke
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  *  - Redistributions of source code must retain the above copyright notice,
11  *    this list of conditions and the following disclaimer.
12  *  - Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *  - Neither the name of Joachim Henke nor the names of his contributors may
16  *    be used to endorse or promote products derived from this software without
17  *    specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 import java.io.*;
33
34 public class b91cli
35 {
36         private static void encode(InputStream is, OutputStream os)
37         {
38                 int s;
39                 byte[] ibuf = new byte[53248];
40                 byte[] obuf = new byte[65536];
41                 basE91 b91 = new basE91();
42
43                 try {
44                         while ((s = is.read(ibuf)) > 0) {
45                                 s = b91.encode(ibuf, s, obuf);
46                                 os.write(obuf, 0, s);
47                         }
48                         s = b91.encEnd(obuf);
49                         os.write(obuf, 0, s);
50                 } catch (Exception e) {
51                         System.err.println(e);
52                 }
53         }
54
55         private static void encodeWrap(InputStream is, OutputStream os)
56         {
57                 int i, s;
58                 int n = 0;
59                 byte[] ibuf = new byte[53248];
60                 byte[] obuf = new byte[65536];
61                 char[] line = new char[76];
62                 basE91 b91 = new basE91();
63
64                 try {
65                         PrintStream ps = new PrintStream(os, false, "US-ASCII");
66
67                         while ((s = is.read(ibuf)) > 0) {
68                                 s = b91.encode(ibuf, s, obuf);
69                                 for (i = 0; i < s; ++i) {
70                                         line[n++] = (char) obuf[i];
71                                         if (n == 76) {
72                                                 ps.println(line);
73                                                 n = 0;
74                                         }
75                                 }
76                         }
77                         s = b91.encEnd(obuf);
78                         for (i = 0; i < s; ++i) {
79                                 line[n++] = (char) obuf[i];
80                                 if (n == 76) {
81                                         ps.println(line);
82                                         n = 0;
83                                 }
84                         }
85                         if (n > 0)
86                                 ps.println(new String(line, 0, n));
87                 } catch (Exception e) {
88                         System.err.println(e);
89                 }
90         }
91
92         private static void decode(InputStream is, OutputStream os)
93         {
94                 int s;
95                 byte[] ibuf = new byte[65536];
96                 byte[] obuf = new byte[57344];
97                 basE91 b91 = new basE91();
98
99                 try {
100                         while ((s = is.read(ibuf)) > 0) {
101                                 s = b91.decode(ibuf, s, obuf);
102                                 os.write(obuf, 0, s);
103                         }
104                         s = b91.decEnd(obuf);
105                         os.write(obuf, 0, s);
106                 } catch (Exception e) {
107                         System.err.println(e);
108                 }
109         }
110
111         private static void errExit(String msg)
112         {
113                 System.err.println("syntax error - " + msg + "\nTry `-h' option for more information.");
114                 System.exit(3);
115         }
116
117         public static void main(String[] args)
118         {
119                 int i;
120                 boolean enc = true;
121                 boolean lbr = true;
122                 String ifn = null;
123                 String ofn = null;
124
125                 for (i = 0; i < args.length; ++i)
126                         if (args[i].length() == 2 && args[i].charAt(0) == '-')
127                                 switch (args[i].charAt(1)) {
128                                 case 'd':
129                                         enc = false;
130                                         break;
131                                 case 'u':
132                                         lbr = false;
133                                         break;
134                                 case 'h':
135                                         System.out.println("Usage: base91 [OPTION] infile [outfile]\n\n  -d\tdecode a basE91 encoded file\n  -u\tleave encoder output unformatted (disable line wrapping)\n  -h\tdisplay this help and exit\n  -V\toutput version information and exit");
136                                         return;
137                                 case 'V':
138                                         System.out.println("base91 0.6.0\nCopyright (c) 2000-2006 Joachim Henke");
139                                         return;
140                                 default:
141                                         errExit("invalid option: " + args[i]);
142                                 }
143                         else if (ifn == null)
144                                 ifn = args[i];
145                         else if (ofn == null)
146                                 ofn = args[i];
147                         else
148                                 errExit("too many arguments: " + args[i]);
149                 if (ifn == null)
150                         errExit("file name missing");
151                 if (ofn == null)
152                         if (enc)
153                                 ofn = ifn + (lbr ? "_b91.txt" : ".b91");
154                         else {
155                                 String lifn = ifn.toLowerCase();
156                                 if (ifn.length() > 4 && lifn.endsWith(".b91"))
157                                         ofn = ifn.substring(0, ifn.length() - 4);
158                                 else if (ifn.length() > 8 && lifn.endsWith("_b91.txt"))
159                                         ofn = ifn.substring(0, ifn.length() - 8);
160                                 else
161                                         ofn = ifn + ".bin";
162                         }
163
164                 try {
165                         FileInputStream ifs = new FileInputStream(ifn);
166                         FileOutputStream ofs = new FileOutputStream(ofn);
167
168                         if (enc)
169                                 if (lbr)
170                                         encodeWrap(ifs, ofs);
171                                 else
172                                         encode(ifs, ofs);
173                         else
174                                 decode(ifs, ofs);
175                         ifs.close();
176                         ofs.close();
177                 } catch (Exception e) {
178                         System.err.println(e);
179                 }
180         }
181 }