1/** 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.android.inputmethod.latin.dicttool; 18 19import java.io.BufferedInputStream; 20import java.io.BufferedOutputStream; 21import java.io.File; 22import java.io.FileInputStream; 23import java.io.FileNotFoundException; 24import java.io.IOException; 25import java.io.InputStream; 26import java.io.OutputStream; 27 28public class Package { 29 private Package() { 30 // This container class is not publicly instantiable. 31 } 32 33 static public class Packager extends Dicttool.Command { 34 public static final String COMMAND = "package"; 35 private final static String PREFIX = "dicttool"; 36 private final static String SUFFIX = ".tmp"; 37 38 public Packager() { 39 } 40 41 @Override 42 public String getHelp() { 43 return COMMAND + " <src_filename> <dst_filename>: Package a file for distribution"; 44 } 45 46 @Override 47 public void run() throws IOException { 48 if (mArgs.length != 2) { 49 throw new RuntimeException("Too many/too few arguments for command " + COMMAND); 50 } 51 final File intermediateFile = File.createTempFile(PREFIX, SUFFIX); 52 try { 53 final Compress.Compressor compressCommand = new Compress.Compressor(); 54 compressCommand.setArgs(new String[] { mArgs[0], intermediateFile.getPath() }); 55 compressCommand.run(); 56 final Crypt.Encrypter cryptCommand = new Crypt.Encrypter(); 57 cryptCommand.setArgs(new String[] { intermediateFile.getPath(), mArgs[1] }); 58 cryptCommand.run(); 59 } finally { 60 intermediateFile.delete(); 61 } 62 } 63 } 64 65 static public class Unpackager extends Dicttool.Command { 66 public static final String COMMAND = "unpackage"; 67 68 public Unpackager() { 69 } 70 71 @Override 72 public String getHelp() { 73 return COMMAND + " <src_filename> <dst_filename>: Detects how a file is packaged and\n" 74 + "decrypts/uncompresses as necessary to produce a raw binary file."; 75 } 76 77 @Override 78 public void run() throws FileNotFoundException, IOException { 79 if (mArgs.length != 2) { 80 throw new RuntimeException("Too many/too few arguments for command " + COMMAND); 81 } 82 final BinaryDictOffdeviceUtils.DecoderChainSpec decodedSpec = 83 BinaryDictOffdeviceUtils.getRawDictionaryOrNull(new File(mArgs[0])); 84 if (null == decodedSpec) { 85 System.out.println(mArgs[0] + " does not seem to be a dictionary"); 86 return; 87 } 88 System.out.println("Packaging : " + decodedSpec.describeChain()); 89 System.out.println("Uncompressed size : " + decodedSpec.mFile.length()); 90 try ( 91 final InputStream input = getFileInputStream(decodedSpec.mFile); 92 final OutputStream output = new BufferedOutputStream( 93 getFileOutputStreamOrStdOut(mArgs[1])) 94 ) { 95 BinaryDictOffdeviceUtils.copy(input, output); 96 } 97 } 98 } 99} 100