lz4 / lz4-java

LZ4 compression for Java
Apache License 2.0
1.09k stars 248 forks source link

the value don't equal xxhsum,the value length is 15 #199

Closed lurenjia528 closed 1 year ago

lurenjia528 commented 1 year ago
   <dependency>
            <groupId>org.lz4</groupId>
            <artifactId>lz4-java</artifactId>
            <version>1.8.0</version>
        </dependency> 
 private static String xxhFile(String filePath) {
        FileInputStream fileInputStream = null;
        XXHashFactory factory = XXHashFactory.fastestInstance();
        long seed = 0L;
        StreamingXXHash64 streamingXXHash64 = factory.newStreamingHash64(seed);
        try {
            fileInputStream = new FileInputStream(filePath);
            byte[] buffer = new byte[8192];
            int length;
            while ((length = fileInputStream.read(buffer)) != -1) {
                streamingXXHash64.update(buffer, 0, length);
            }
        } catch (Exception e) {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        }
        long value = streamingXXHash64.getValue();
        return Long.toHexString(value).toLowerCase();
    }

the result : 6e7b836becf8ea2 The calculation result of some files is 15 bits

go calc the same file, the result is 06e7b836becf8ea2

lurenjia528 commented 1 year ago

I know

this is java long type precision lost, The following two methods are ok.

import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
...
        long value = streamingXXHash64.getValue();
        BigInteger bigInteger = BigInteger.valueOf(value);
        return HexBin.encode(bigInteger.toByteArray()).toLowerCase();

    public static String printHexString(byte[] b) {
        String res = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            res += hex;
        }
        return res;
    }
lurenjia528 commented 1 year ago

long l = 497568829116747426L;

this long value to hex should be 06e7b836becf8ea2

but Long.toHexString(value).toLowerCase(); is 6e7b836becf8ea2