Java Code Examples for net.jpountz.lz4.LZ4Factory#fastDecompressor()

The following examples show how to use net.jpountz.lz4.LZ4Factory#fastDecompressor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Lz4Compress.java    From compress with MIT License 6 votes vote down vote up
@Override
public byte[] uncompress(byte[] data) throws IOException {
	LZ4Factory factory = LZ4Factory.fastestInstance();
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	LZ4FastDecompressor decompresser = factory.fastDecompressor();
	LZ4BlockInputStream lzis = new LZ4BlockInputStream(new ByteArrayInputStream(data), decompresser);
	
	int count;
	byte[] buffer = new byte[2048];
	while ((count = lzis.read(buffer)) != -1) {
		baos.write(buffer, 0, count);
	}
	lzis.close();
	
	return baos.toByteArray();
}
 
Example 2
Source File: LZ4CompressorTest.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        if(args.length == 0){
            System.out.println("args[0] must be data file path");
            return;
        }
        LZ4Factory factory = LZ4Factory.fastestInstance();

        byte[] data = Files.toByteArray(new File(args[0]));
        final int decompressedLength = data.length;

        // compress data
        LZ4Compressor compressor = factory.fastCompressor();
        long start = System.currentTimeMillis();
        int maxCompressedLength = compressor.maxCompressedLength(decompressedLength);
        byte[] compressed = new byte[maxCompressedLength];
        int compressedLength = compressor.compress(data, 0, decompressedLength, compressed, 0, maxCompressedLength);
        System.out.println("compress take:" + (System.currentTimeMillis() - start));
        System.out.println(compressedLength);

        // decompress data
        // - method 1: when the decompressed length is known
        LZ4FastDecompressor decompressor = factory.fastDecompressor();
        start = System.currentTimeMillis();
        byte[] restored = new byte[decompressedLength];
        int compressedLength2 = decompressor.decompress(compressed, 0, restored, 0, decompressedLength);
        System.out.println("decompress take:" + (System.currentTimeMillis() - start));
        System.out.println(decompressedLength);
        // compressedLength == compressedLength2

        // - method 2: when the compressed length is known (a little slower)
        // the destination buffer needs to be over-sized
        LZ4SafeDecompressor decompressor2 = factory.safeDecompressor();
        int decompressedLength2 = decompressor2.decompress(compressed, 0, compressedLength, restored, 0);
    }
 
Example 3
Source File: LZ4CompressorTest.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        if(args.length == 0){
            System.out.println("args[0] must be data file path");
            return;
        }
        LZ4Factory factory = LZ4Factory.fastestInstance();

        byte[] data = Files.toByteArray(new File(args[0]));
        final int decompressedLength = data.length;

        // compress data
        LZ4Compressor compressor = factory.fastCompressor();
        long start = System.currentTimeMillis();
        int maxCompressedLength = compressor.maxCompressedLength(decompressedLength);
        byte[] compressed = new byte[maxCompressedLength];
        int compressedLength = compressor.compress(data, 0, decompressedLength, compressed, 0, maxCompressedLength);
        System.out.println("compress take:" + (System.currentTimeMillis() - start));
        System.out.println(compressedLength);

        // decompress data
        // - method 1: when the decompressed length is known
        LZ4FastDecompressor decompressor = factory.fastDecompressor();
        start = System.currentTimeMillis();
        byte[] restored = new byte[decompressedLength];
        int compressedLength2 = decompressor.decompress(compressed, 0, restored, 0, decompressedLength);
        System.out.println("decompress take:" + (System.currentTimeMillis() - start));
        System.out.println(decompressedLength);
        // compressedLength == compressedLength2

        // - method 2: when the compressed length is known (a little slower)
        // the destination buffer needs to be over-sized
        LZ4SafeDecompressor decompressor2 = factory.safeDecompressor();
        int decompressedLength2 = decompressor2.decompress(compressed, 0, compressedLength, restored, 0);
    }
 
Example 4
Source File: SinParser.java    From Flashtool with GNU General Public License v3.0 5 votes vote down vote up
public String getDataTypePriv() throws IOException {
	RandomAccessFile fin = new RandomAccessFile(sinfile,"r");
	byte[] res=null;
	byte[] rescomp=null;
	if (dataHeader.mmcfLen>0) {
		Object block = dataBlocks.firstElement();
		if (block instanceof AddrBlock) {
			res = new byte[(int)((AddrBlock)block).dataLen];
			fin.seek(getDataOffset()+((AddrBlock)block).dataOffset);
			fin.read(res);
			fin.close();
		}
		else {
			rescomp = new byte[(int)((LZ4ABlock)block).compDataLen];
			fin.seek(getDataOffset()+((LZ4ABlock)block).dataOffset);
			fin.read(rescomp);
			fin.close();
			LZ4Factory factory = LZ4Factory.fastestInstance();
			LZ4FastDecompressor decomp = factory.fastDecompressor();
			res = decomp.decompress(rescomp, (int)((LZ4ABlock)block).uncompDataLen);
		}
	}
	else {
		res = new byte[blocks.blocks[0].length];
		fin.seek(getDataOffset());
		fin.read(res);
		fin.close();
	}
	return getDataTypePriv(res);
}
 
Example 5
Source File: NSO0Adapter.java    From Ghidra-Switch-Loader with ISC License 4 votes vote down vote up
private void read() throws IOException
{
    this.nso0 = new NSO0Header(this.fileReader, 0x0);
    
    LZ4Factory factory = LZ4Factory.fastestInstance();
    LZ4FastDecompressor decompressor = factory.fastDecompressor();
    
    NSO0SectionHeader textHeader = this.nso0.getSectionHeader(NXOSectionType.TEXT);
    NSO0SectionHeader rodataHeader = this.nso0.getSectionHeader(NXOSectionType.RODATA);
    NSO0SectionHeader dataHeader = this.nso0.getSectionHeader(NXOSectionType.DATA);
    
    int textOffset = textHeader.getMemoryOffset();
    int rodataOffset = rodataHeader.getMemoryOffset();
    int dataOffset = dataHeader.getMemoryOffset();
    int textSize = textHeader.getDecompressedSize();
    int rodataSize = rodataHeader.getDecompressedSize();
    int dataSize = dataHeader.getDecompressedSize();
    
    // The data section is last, so we use its offset + decompressed size
    byte[] full = new byte[dataOffset + dataSize];
    byte[] decompressedText;
    byte[] decompressedRodata;
    byte[] decompressedData;
    
    if (this.nso0.isSectionCompressed(NXOSectionType.TEXT))
    {
        byte[] compressedText = this.fileProvider.readBytes(this.nso0.getSectionFileOffset(NXOSectionType.TEXT), this.nso0.getCompressedSectionSize(NXOSectionType.TEXT));
        decompressedText = new byte[textSize];
        decompressor.decompress(compressedText, decompressedText);
    }
    else
    {
        decompressedText = this.fileProvider.readBytes(this.nso0.getSectionFileOffset(NXOSectionType.TEXT), textSize);
    }
    
    System.arraycopy(decompressedText, 0, full, textOffset, textSize);
    
    if (this.nso0.isSectionCompressed(NXOSectionType.RODATA))
    {
        byte[] compressedRodata = this.fileProvider.readBytes(this.nso0.getSectionFileOffset(NXOSectionType.RODATA), this.nso0.getCompressedSectionSize(NXOSectionType.RODATA));
        decompressedRodata = new byte[rodataSize];
        decompressor.decompress(compressedRodata, decompressedRodata);
    }
    else
    {
        decompressedRodata = this.fileProvider.readBytes(this.nso0.getSectionFileOffset(NXOSectionType.RODATA), rodataSize);
    }
    
    System.arraycopy(decompressedRodata, 0, full, rodataOffset, rodataSize);
    
    if (this.nso0.isSectionCompressed(NXOSectionType.DATA))
    {
        byte[] compressedData = this.fileProvider.readBytes(this.nso0.getSectionFileOffset(NXOSectionType.DATA), this.nso0.getCompressedSectionSize(NXOSectionType.DATA));
        decompressedData = new byte[dataSize];
        decompressor.decompress(compressedData, decompressedData);
    }
    else
    {
        decompressedData = this.fileProvider.readBytes(this.nso0.getSectionFileOffset(NXOSectionType.DATA), dataSize);
    }
    
    System.arraycopy(decompressedData, 0, full, dataOffset, dataSize);
    this.memoryProvider = new ByteArrayProvider(full);
    
    this.sections = new NXOSection[3];
    this.sections[NXOSectionType.TEXT.ordinal()] = new NXOSection(NXOSectionType.TEXT, textOffset, textSize);
    this.sections[NXOSectionType.RODATA.ordinal()] = new NXOSection(NXOSectionType.RODATA, rodataOffset, rodataSize);
    this.sections[NXOSectionType.DATA.ordinal()] = new NXOSection(NXOSectionType.DATA, dataOffset, dataSize);
}
 
Example 6
Source File: Lz4FrameDecoder.java    From netty-4.1.22 with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new customizable LZ4 decoder.创建一个新的可定制的LZ4解码器。
 *
 * @param factory   user customizable {@link LZ4Factory} instance
 *                  which may be JNI bindings to the original C implementation, a pure Java implementation
 *                  or a Java implementation that uses the {@link sun.misc.Unsafe}
 * @param checksum  the {@link Checksum} instance to use to check data for integrity.
 *                  You may set {@code null} if you do not want to validate checksum of each block
 */
public Lz4FrameDecoder(LZ4Factory factory, Checksum checksum) {
    if (factory == null) {
        throw new NullPointerException("factory");
    }
    decompressor = factory.fastDecompressor();
    this.checksum = checksum == null ? null : ByteBufChecksum.wrapChecksum(checksum);
}