From ba98b639836953ee3be8f7b58000123822fd2865 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Tue, 30 Oct 2012 18:04:23 +0900 Subject: [PATCH] Add the unpackage command Bug: 7388852 Change-Id: I7e5376f5d646e669884ef78e4e0dee62c99d4d6b --- .../latin/dicttool/CommandList.java | 2 + .../inputmethod/latin/dicttool/Compress.java | 3 + .../inputmethod/latin/dicttool/Crypt.java | 4 + .../inputmethod/latin/dicttool/Package.java | 78 +++++++++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 tools/dicttool/src/com/android/inputmethod/latin/dicttool/Package.java diff --git a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/CommandList.java b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/CommandList.java index 66f871c10..0e0095bd6 100644 --- a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/CommandList.java +++ b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/CommandList.java @@ -24,6 +24,8 @@ public class CommandList { Dicttool.addCommand("uncompress", Compress.Uncompressor.class); Dicttool.addCommand("encrypt", Crypt.Encrypter.class); Dicttool.addCommand("decrypt", Crypt.Decrypter.class); + Dicttool.addCommand("package", Package.Packager.class); + Dicttool.addCommand("unpackage", Package.Unpackager.class); Dicttool.addCommand("makedict", Makedict.class); } } diff --git a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Compress.java b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Compress.java index 8c159201a..b7f48b522 100644 --- a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Compress.java +++ b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Compress.java @@ -28,6 +28,9 @@ import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; public class Compress { + private Compress() { + // This container class is not publicly instantiable. + } public static OutputStream getCompressedStream(final OutputStream out) throws java.io.IOException { diff --git a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Crypt.java b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Crypt.java index 036b617ef..f8990231e 100644 --- a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Crypt.java +++ b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Crypt.java @@ -20,6 +20,10 @@ import java.io.InputStream; import java.io.OutputStream; public class Crypt { + private Crypt() { + // This container class is not publicly instantiable. + } + public static OutputStream getCryptedStream(final OutputStream out) { // Encryption is not supported return out; diff --git a/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Package.java b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Package.java new file mode 100644 index 000000000..c35d0f312 --- /dev/null +++ b/tools/dicttool/src/com/android/inputmethod/latin/dicttool/Package.java @@ -0,0 +1,78 @@ +/** + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.android.inputmethod.latin.dicttool; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; + +public class Package { + private Package() { + // This container class is not publicly instantiable. + } + + static public class Packager extends Dicttool.Command { + public static final String COMMAND = "package"; + + public Packager() { + } + + public String getHelp() { + return COMMAND + " : Package a file for distribution"; + } + + public void run() { + // Not implemented yet + throw new UnsupportedOperationException(); + } + } + + static public class Unpackager extends Dicttool.Command { + public static final String COMMAND = "unpackage"; + + public Unpackager() { + } + + public String getHelp() { + return COMMAND + " : Detects how a file is packaged and\n" + + "decrypts/uncompresses as necessary to produce a raw binary file."; + } + + public void run() throws FileNotFoundException, IOException { + if (mArgs.length != 2) { + throw new RuntimeException("Too many/too few arguments for command " + COMMAND); + } + final BinaryDictOffdeviceUtils.DecoderChainSpec decodedSpec = + BinaryDictOffdeviceUtils.getRawBinaryDictionaryOrNull(new File(mArgs[0])); + if (null == decodedSpec) { + System.out.println(mArgs[0] + " does not seem to be a dictionary"); + return; + } + System.out.println("Packaging : " + decodedSpec.describeChain()); + System.out.println("Uncompressed size : " + decodedSpec.mFile.length()); + final FileOutputStream dstStream = new FileOutputStream(new File(mArgs[1])); + BinaryDictOffdeviceUtils.copy(new BufferedInputStream( + new FileInputStream(decodedSpec.mFile)), new BufferedOutputStream(dstStream)); + } + } +}