net.jpountz.util.SafeUtils Java Examples

The following examples show how to use net.jpountz.util.SafeUtils. 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: LZ4BlockInputStream.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public int read(byte[] b, int off, int len) throws IOException {
    SafeUtils.checkRange(b, off, len);
    if (finished) {
        return -1;
    }
    if (o == originalLen) {
        refill();
    }
    if (finished) {
        return -1;
    }
    len = Math.min(len, originalLen - o);
    System.arraycopy(buffer, o, b, off, len);
    o += len;
    return len;
}
 
Example #2
Source File: LZ4BlockOutputStream.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void write(byte[] b, int off, int len) throws IOException {
    SafeUtils.checkRange(b, off, len);
    ensureNotFinished();

    while (o + len > blockSize) {
        final int l = blockSize - o;
        System.arraycopy(b, off, buffer, o, blockSize - o);
        o = blockSize;
        flushBufferedData();
        off += l;
        len -= l;
    }
    System.arraycopy(b, off, buffer, o, len);
    o += len;
}
 
Example #3
Source File: CloverDataStream.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public int read(byte[] b, int off, int len) throws IOException {
	SafeUtils.checkRange(b, off, len);
	int count = 0;
	while (buffer.remaining() < len) {
		final int l = buffer.remaining();
		buffer.get(b, off, l);
		count += l;
		off += l;
		len -= l;
		if (!readDataBlock())
			return -1; //end of stream;

	}
	buffer.get(b, off, len);
	count += len;
	return count;
}
 
Example #4
Source File: Lz4BlockDecompressor.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public int decompress(byte[] src, int srcOff, int srcLen, byte[] dst, int dstOff)
		throws InsufficientBufferException, DataCorruptionException {
	final int compressedLen = SafeUtils.readIntLE(src, srcOff);
	final int originalLen = SafeUtils.readIntLE(src, srcOff + 4);
	validateLength(compressedLen, originalLen);

	if (dst.length - dstOff < originalLen) {
		throw new InsufficientBufferException("Buffer length too small");
	}

	if (src.length - srcOff - HEADER_LENGTH < compressedLen) {
		throw new DataCorruptionException("Source data is not integral for decompression.");
	}

	try {
		final int compressedLen2 = decompressor.decompress(
				src,
				srcOff + HEADER_LENGTH,
				dst,
				dstOff,
				originalLen
		);
		if (compressedLen != compressedLen2) {
			throw new DataCorruptionException("Input is corrupted");
		}
	}
	catch (LZ4Exception e) {
		throw new DataCorruptionException("Input is corrupted", e);
	}

	return originalLen;
}
 
Example #5
Source File: LZ4JNISafeDecompressor.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public final int decompress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen) {
    SafeUtils.checkRange(src, srcOff, srcLen);
    SafeUtils.checkRange(dest, destOff, maxDestLen);
    final int result = LZ4JNI.LZ4_decompress_safe(src, null, srcOff, srcLen, dest, null, destOff, maxDestLen);
    if (result < 0) {
        throw new LZ4Exception("Error decoding offset " + (srcOff - result) + " of input buffer");
    }
    return result;
}
 
Example #6
Source File: LZ4JNIFastDecompressor.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public final int decompress(byte[] src, int srcOff, byte[] dest, int destOff, int destLen) {
    SafeUtils.checkRange(src, srcOff);
    SafeUtils.checkRange(dest, destOff, destLen);
    final int result = LZ4JNI.LZ4_decompress_fast(src, null, srcOff, dest, null, destOff, destLen);
    if (result < 0) {
        throw new LZ4Exception("Error decoding offset " + (srcOff - result) + " of input buffer");
    }
    return result;
}
 
Example #7
Source File: LZ4HCJNICompressor.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int compress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen) {
    SafeUtils.checkRange(src, srcOff, srcLen);
    SafeUtils.checkRange(dest, destOff, maxDestLen);
    final int result = LZ4JNI.LZ4_compressHC(src, null, srcOff, srcLen, dest, null, destOff, maxDestLen, compressionLevel);
    if (result <= 0) {
        throw new LZ4Exception();
    }
    return result;
}
 
Example #8
Source File: ChannelLZ4Decompressor.java    From datakernel with Apache License 2.0 5 votes vote down vote up
private static void readHeader(Header header, byte[] buf, int off) throws ParseException {
	for (int i = 0; i < MAGIC_LENGTH; ++i) {
		if (buf[off + i] != MAGIC[i]) {
			throw STREAM_IS_CORRUPTED;
		}
	}
	int token = buf[off + MAGIC_LENGTH] & 0xFF;
	header.compressionMethod = token & 0xF0;
	int compressionLevel = COMPRESSION_LEVEL_BASE + (token & 0x0F);
	if (header.compressionMethod != COMPRESSION_METHOD_RAW && header.compressionMethod != COMPRESSION_METHOD_LZ4) {
		throw STREAM_IS_CORRUPTED;
	}
	header.compressedLen = SafeUtils.readIntLE(buf, off + MAGIC_LENGTH + 1);
	header.originalLen = SafeUtils.readIntLE(buf, off + MAGIC_LENGTH + 5);
	header.check = SafeUtils.readIntLE(buf, off + MAGIC_LENGTH + 9);
	if (header.originalLen > 1 << compressionLevel
			|| (header.originalLen < 0 || header.compressedLen < 0)
			|| (header.originalLen == 0 && header.compressedLen != 0)
			|| (header.originalLen != 0 && header.compressedLen == 0)
			|| (header.compressionMethod == COMPRESSION_METHOD_RAW && header.originalLen != header.compressedLen)) {
		throw STREAM_IS_CORRUPTED;
	}
	if (header.originalLen == 0) {
		if (header.check != 0) {
			throw STREAM_IS_CORRUPTED;
		}
		header.finished = true;
	}
}
 
Example #9
Source File: Lz4BlockDecompressor.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public int decompress(byte[] src, int srcOff, int srcLen, byte[] dst, int dstOff)
		throws InsufficientBufferException, DataCorruptionException {
	final int compressedLen = SafeUtils.readIntLE(src, srcOff);
	final int originalLen = SafeUtils.readIntLE(src, srcOff + 4);
	validateLength(compressedLen, originalLen);

	if (dst.length - dstOff < originalLen) {
		throw new InsufficientBufferException("Buffer length too small");
	}

	if (src.length - srcOff - HEADER_LENGTH < compressedLen) {
		throw new DataCorruptionException("Source data is not integral for decompression.");
	}

	try {
		final int compressedLen2 = decompressor.decompress(
				src,
				srcOff + HEADER_LENGTH,
				dst,
				dstOff,
				originalLen
		);
		if (compressedLen != compressedLen2) {
			throw new DataCorruptionException("Input is corrupted");
		}
	}
	catch (LZ4Exception e) {
		throw new DataCorruptionException("Input is corrupted", e);
	}

	return originalLen;
}
 
Example #10
Source File: LZ4HCJavaSafeCompressor.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
private int hashPointer(byte[] bytes, int off) {
    final int v = SafeUtils.readInt(bytes, off);
    return hashPointer(v);
}
 
Example #11
Source File: LZ4HCJavaSafeCompressor.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
private void addHash(byte[] bytes, int off) {
    final int v = SafeUtils.readInt(bytes, off);
    addHash(v, off);
}
 
Example #12
Source File: LZ4HCJavaSafeCompressor.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
boolean insertAndFindBestMatch(byte[] buf, int off, int matchLimit, Match match) {
    match.start = off;
    match.len = 0;
    int delta = 0;
    int repl = 0;

    insert(off, buf);

    int ref = hashPointer(buf, off);

    if (ref >= off - 4 && ref <= off && ref >= base) { // potential repetition
        if (LZ4SafeUtils.readIntEquals(buf, ref, off)) { // confirmed
            delta = off - ref;
            repl = match.len = MIN_MATCH + LZ4SafeUtils.commonBytes(buf, ref + MIN_MATCH, off + MIN_MATCH, matchLimit);
            match.ref = ref;
        }
        ref = next(ref);
    }

    for (int i = 0; i < maxAttempts; ++i) {
        if (ref < Math.max(base, off - MAX_DISTANCE + 1) || ref > off) {
            break;
        }
        if (LZ4SafeUtils.readIntEquals(buf, ref, off)) {
            final int matchLen = MIN_MATCH + LZ4SafeUtils.commonBytes(buf, ref + MIN_MATCH, off + MIN_MATCH, matchLimit);
            if (matchLen > match.len) {
                match.ref = ref;
                match.len = matchLen;
            }
        }
        ref = next(ref);
    }

    if (repl != 0) {
        int ptr = off;
        final int end = off + repl - (MIN_MATCH - 1);
        while (ptr < end - delta) {
            chainTable[ptr & MASK] = (short) delta; // pre load
            ++ptr;
        }
        do {
            chainTable[ptr & MASK] = (short) delta;
            hashTable[hashHC(SafeUtils.readInt(buf, ptr))] = ptr;
            ++ptr;
        } while (ptr < end);
        nextToUpdate = end;
    }

    return match.len != 0;
}