ioutil.java

来自「动画素材图像语义标注系统:该系统实现对图片的语义标注」· Java 代码 · 共 82 行

JAVA
82
字号
package cn.dxm.client;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

/**
 * I/O操作工具类
 * @author hdf
 */
public class IOUtil {

	/**
	 * 对字节流进行压缩
	 * @param input:要压缩的字节流
	 * @param level:级别 9~1
	 * @return
	 */
	public static byte[] compress(byte[] input, int level) {
        // Create the compressor with highest level of compression
        Deflater compressor = new Deflater();
        compressor.setLevel(level);

        // Give the compressor the data to compress
        compressor.setInput(input);
        compressor.finish();

        // Create an expandable byte array to hold the compressed data.
        ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

        // Compress the data
        byte[] buf = new byte[1024];
        while (!compressor.finished()) {
            int count = compressor.deflate(buf);
            bos.write(buf, 0, count);
        }
        try {
            bos.flush();
            bos.close();
        } catch (IOException e) { 
            e.getStackTrace();
        }
        // Get the compressed data
        return bos.toByteArray();        
    }    
    
	/**
	 * 解压缩字节流
	 * @param compressedData
	 * @return
	 */
    public static byte[] decompress (byte[] compressedData) {
        // Create the decompressor and give it the data to compress
        Inflater decompressor = new Inflater();
        decompressor.setInput(compressedData);

        // Create an expandable byte array to hold the decompressed data
        ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);

        // Decompress the data
        byte[] buf = new byte[1024];
        while (!decompressor.finished()) {
            try {
                int count = decompressor.inflate(buf);
                bos.write(buf, 0, count);
            } catch (DataFormatException e) {
                e.getStackTrace();
            }
        } 
        try {
            bos.flush();
            bos.close();       
        } catch (IOException e) {
            e.getStackTrace();
        }
        // Get the decompressed data
        return bos.toByteArray();         
    }       
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?