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 17 package com.android.inputmethod.latin.dicttool; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 import java.io.OutputStream; 22 import java.util.zip.GZIPInputStream; 23 import java.util.zip.GZIPOutputStream; 24 25 public class Compress { Compress()26 private Compress() { 27 // This container class is not publicly instantiable. 28 } 29 getCompressedStream(final OutputStream out)30 public static OutputStream getCompressedStream(final OutputStream out) throws IOException { 31 return new GZIPOutputStream(out); 32 } 33 getUncompressedStream(final InputStream in)34 public static InputStream getUncompressedStream(final InputStream in) throws IOException { 35 return new GZIPInputStream(in); 36 } 37 38 static public class Compressor extends Dicttool.Command { 39 public static final String COMMAND = "compress"; 40 Compressor()41 public Compressor() { 42 } 43 44 @Override getHelp()45 public String getHelp() { 46 return COMMAND + " <src_filename> <dst_filename>: " 47 + "Compresses a file using gzip compression"; 48 } 49 50 @Override run()51 public void run() throws IOException { 52 if (mArgs.length > 2) { 53 throw new RuntimeException("Too many arguments for command " + COMMAND); 54 } 55 final String inFilename = mArgs.length >= 1 ? mArgs[0] : STDIN_OR_STDOUT; 56 final String outFilename = mArgs.length >= 2 ? mArgs[1] : STDIN_OR_STDOUT; 57 try ( 58 final InputStream input = getFileInputStreamOrStdIn(inFilename); 59 final OutputStream compressedOutput = getCompressedStream( 60 getFileOutputStreamOrStdOut(outFilename)) 61 ) { 62 BinaryDictOffdeviceUtils.copy(input, compressedOutput); 63 } 64 } 65 } 66 67 static public class Uncompressor extends Dicttool.Command { 68 public static final String COMMAND = "uncompress"; 69 Uncompressor()70 public Uncompressor() { 71 } 72 73 @Override getHelp()74 public String getHelp() { 75 return COMMAND + " <src_filename> <dst_filename>: " 76 + "Uncompresses a file compressed with gzip compression"; 77 } 78 79 @Override run()80 public void run() throws IOException { 81 if (mArgs.length > 2) { 82 throw new RuntimeException("Too many arguments for command " + COMMAND); 83 } 84 final String inFilename = mArgs.length >= 1 ? mArgs[0] : STDIN_OR_STDOUT; 85 final String outFilename = mArgs.length >= 2 ? mArgs[1] : STDIN_OR_STDOUT; 86 try ( 87 final InputStream uncompressedInput = getUncompressedStream( 88 getFileInputStreamOrStdIn(inFilename)); 89 final OutputStream output = getFileOutputStreamOrStdOut(outFilename) 90 ) { 91 BinaryDictOffdeviceUtils.copy(uncompressedInput, output); 92 } 93 } 94 } 95 } 96