2 * basE91 command line front-end
4 * Copyright (c) 2000-2006 Joachim Henke
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
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.
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.
36 private static void encode(InputStream is, OutputStream os)
39 byte[] ibuf = new byte[53248];
40 byte[] obuf = new byte[65536];
41 basE91 b91 = new basE91();
44 while ((s = is.read(ibuf)) > 0) {
45 s = b91.encode(ibuf, s, obuf);
50 } catch (Exception e) {
51 System.err.println(e);
55 private static void encodeWrap(InputStream is, OutputStream os)
59 byte[] ibuf = new byte[53248];
60 byte[] obuf = new byte[65536];
61 char[] line = new char[76];
62 basE91 b91 = new basE91();
65 PrintStream ps = new PrintStream(os, false, "US-ASCII");
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];
78 for (i = 0; i < s; ++i) {
79 line[n++] = (char) obuf[i];
86 ps.println(new String(line, 0, n));
87 } catch (Exception e) {
88 System.err.println(e);
92 private static void decode(InputStream is, OutputStream os)
95 byte[] ibuf = new byte[65536];
96 byte[] obuf = new byte[57344];
97 basE91 b91 = new basE91();
100 while ((s = is.read(ibuf)) > 0) {
101 s = b91.decode(ibuf, s, obuf);
102 os.write(obuf, 0, s);
104 s = b91.decEnd(obuf);
105 os.write(obuf, 0, s);
106 } catch (Exception e) {
107 System.err.println(e);
111 private static void errExit(String msg)
113 System.err.println("syntax error - " + msg + "\nTry `-h' option for more information.");
117 public static void main(String[] args)
125 for (i = 0; i < args.length; ++i)
126 if (args[i].length() == 2 && args[i].charAt(0) == '-')
127 switch (args[i].charAt(1)) {
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");
138 System.out.println("base91 0.6.0\nCopyright (c) 2000-2006 Joachim Henke");
141 errExit("invalid option: " + args[i]);
143 else if (ifn == null)
145 else if (ofn == null)
148 errExit("too many arguments: " + args[i]);
150 errExit("file name missing");
153 ofn = ifn + (lbr ? "_b91.txt" : ".b91");
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);
165 FileInputStream ifs = new FileInputStream(ifn);
166 FileOutputStream ofs = new FileOutputStream(ofn);
170 encodeWrap(ifs, ofs);
177 } catch (Exception e) {
178 System.err.println(e);