All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.czietsman.lz4.LZ4ByteBufferUtils Maven / Gradle / Ivy

The newest version!
package com.github.czietsman.lz4;

/*
 * 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.
 */

import com.github.czietsman.util.ByteBufferUtils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

enum LZ4ByteBufferUtils {
    ;

    static int hash(ByteBuffer buf, int i) {
        return LZ4Utils.hash(ByteBufferUtils.readInt(buf, i));
    }

    static int hash64k(ByteBuffer buf, int i) {
        return LZ4Utils.hash64k(ByteBufferUtils.readInt(buf, i));
    }

    static boolean readIntEquals(ByteBuffer buf, int i, int j) {
        return buf.getInt(i) == buf.getInt(j);
    }

    static void safeIncrementalCopy(ByteBuffer dest, int matchOff, int dOff, int matchLen) {
        for (int i = 0; i < matchLen; ++i) {
            dest.put(dOff + i, dest.get(matchOff + i));
        }
    }

    static void wildIncrementalCopy(ByteBuffer dest, int matchOff, int dOff, int matchCopyEnd) {
        if (dOff - matchOff < 4) {
            for (int i = 0; i < 4; ++i) {
                ByteBufferUtils.writeByte(dest, dOff + i, ByteBufferUtils.readByte(dest, matchOff + i));
            }
            dOff += 4;
            matchOff += 4;
            int dec = 0;
            assert dOff >= matchOff && dOff - matchOff < 8;
            switch (dOff - matchOff) {
                case 1:
                    matchOff -= 3;
                    break;
                case 2:
                    matchOff -= 2;
                    break;
                case 3:
                    matchOff -= 3;
                    dec = -1;
                    break;
                case 5:
                    dec = 1;
                    break;
                case 6:
                    dec = 2;
                    break;
                case 7:
                    dec = 3;
                    break;
                default:
                    break;
            }
            ByteBufferUtils.writeInt(dest, dOff, ByteBufferUtils.readInt(dest, matchOff));
            dOff += 4;
            matchOff -= dec;
        } else if (dOff - matchOff < LZ4Constants.COPY_LENGTH) {
            ByteBufferUtils.writeLong(dest, dOff, ByteBufferUtils.readLong(dest, matchOff));
            dOff += dOff - matchOff;
        }
        while (dOff < matchCopyEnd) {
            ByteBufferUtils.writeLong(dest, dOff, ByteBufferUtils.readLong(dest, matchOff));
            dOff += 8;
            matchOff += 8;
        }
    }

    static int commonBytes(ByteBuffer src, int ref, int sOff, int srcLimit) {
        int matchLen = 0;
        while (sOff <= srcLimit - 8) {
            if (ByteBufferUtils.readLong(src, sOff) == ByteBufferUtils.readLong(src, ref)) {
                matchLen += 8;
                ref += 8;
                sOff += 8;
            } else {
                final int zeroBits;
                if (src.order() == ByteOrder.BIG_ENDIAN) {
                    zeroBits = Long.numberOfLeadingZeros(ByteBufferUtils.readLong(src, sOff) ^ ByteBufferUtils.readLong(src, ref));
                } else {
                    zeroBits = Long.numberOfTrailingZeros(ByteBufferUtils.readLong(src, sOff) ^ ByteBufferUtils.readLong(src, ref));
                }
                return matchLen + (zeroBits >>> 3);
            }
        }
        while (sOff < srcLimit && ByteBufferUtils.readByte(src, ref++) == ByteBufferUtils.readByte(src, sOff++)) {
            ++matchLen;
        }
        return matchLen;
    }

    static int commonBytesBackward(ByteBuffer b, int o1, int o2, int l1, int l2) {
        int count = 0;
        while (o1 > l1 && o2 > l2 && b.get(--o1) == b.get(--o2)) {
            ++count;
        }
        return count;
    }

    static void safeArraycopy(ByteBuffer src, int sOff, ByteBuffer dest, int dOff, int len) {
        for (int i = 0; i < len; ++i) {
            dest.put(dOff + i, src.get(sOff + i));
        }
    }

    static void wildArraycopy(ByteBuffer src, int sOff, ByteBuffer dest, int dOff, int len) {
        assert src.order().equals(dest.order());
        try {
            for (int i = 0; i < len; i += 8) {
                dest.putLong(dOff + i, src.getLong(sOff + i));
            }
        } catch (IndexOutOfBoundsException e) {
            throw new LZ4Exception("Malformed input at offset " + sOff);
        }
    }

    static int encodeSequence(ByteBuffer src, int anchor, int matchOff, int matchRef, int matchLen, ByteBuffer dest, int dOff, int destEnd) {
        final int runLen = matchOff - anchor;
        final int tokenOff = dOff++;

        if (dOff + runLen + (2 + 1 + LZ4Constants.LAST_LITERALS) + (runLen >>> 8) > destEnd) {
            throw new LZ4Exception("maxDestLen is too small");
        }

        int token;
        if (runLen >= LZ4Constants.RUN_MASK) {
            token = (byte) (LZ4Constants.RUN_MASK << LZ4Constants.ML_BITS);
            dOff = writeLen(runLen - LZ4Constants.RUN_MASK, dest, dOff);
        } else {
            token = runLen << LZ4Constants.ML_BITS;
        }

        // copy literals
        wildArraycopy(src, anchor, dest, dOff, runLen);
        dOff += runLen;

        // encode offset
        final int matchDec = matchOff - matchRef;
        dest.put(dOff++, (byte) matchDec);
        dest.put(dOff++, (byte) (matchDec >>> 8));

        // encode match len
        matchLen -= 4;
        if (dOff + (1 + LZ4Constants.LAST_LITERALS) + (matchLen >>> 8) > destEnd) {
            throw new LZ4Exception("maxDestLen is too small");
        }
        if (matchLen >= LZ4Constants.ML_MASK) {
            token |= LZ4Constants.ML_MASK;
            dOff = writeLen(matchLen - LZ4Constants.RUN_MASK, dest, dOff);
        } else {
            token |= matchLen;
        }

        dest.put(tokenOff, (byte) token);

        return dOff;
    }

    static int lastLiterals(ByteBuffer src, int sOff, int srcLen, ByteBuffer dest, int dOff, int destEnd) {
        final int runLen = srcLen;

        if (dOff + runLen + 1 + (runLen + 255 - LZ4Constants.RUN_MASK) / 255 > destEnd) {
            throw new LZ4Exception();
        }

        if (runLen >= LZ4Constants.RUN_MASK) {
            dest.put(dOff++, (byte) (LZ4Constants.RUN_MASK << LZ4Constants.ML_BITS));
            dOff = writeLen(runLen - LZ4Constants.RUN_MASK, dest, dOff);
        } else {
            dest.put(dOff++, (byte) (runLen << LZ4Constants.ML_BITS));
        }
        // copy literals
        safeArraycopy(src, sOff, dest, dOff, runLen);
        dOff += runLen;

        return dOff;
    }

    static int writeLen(int len, ByteBuffer dest, int dOff) {
        while (len >= 0xFF) {
            dest.put(dOff++, (byte) 0xFF);
            len -= 0xFF;
        }
        dest.put(dOff++, (byte) len);
        return dOff;
    }

    static void copyTo(Match m1, Match m2) {
        m2.len = m1.len;
        m2.start = m1.start;
        m2.ref = m1.ref;
    }

    static class Match {
        int start, ref, len;

        void fix(int correction) {
            start += correction;
            ref += correction;
            len -= correction;
        }

        int end() {
            return start + len;
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy