Skip to content

pure java compressed output cannot be decompressed by native #295

@bokken

Description

@bokken

Content compressed with the pure java implementation fails to decompress by the native code with error
FAILED_TO_UNCOMPRESS(5).

Here is a unit test which causes the failure. It is the testSnappyCompressNativeDecompress which fails.

package org.xerial.snappy;

import static org.junit.Assert.assertEquals;

import java.nio.ByteBuffer;
import java.util.Random;

import org.junit.Test;
import org.xerial.snappy.pure.PureJavaSnappy;

public class PureSnappyCompatTest
{
    private static final byte[] DATA = new byte[34873];

    static
    {
        int i = 0;
        final Random random = new Random(87);
        for ( ; i<DATA.length - 6; i+=7)
        {
            if (i % 8 == 1)
            {
                DATA[i] = (byte) random.nextInt(256);
                DATA[i + 1] = (byte) random.nextInt(256);
                DATA[i + 2] = (byte) random.nextInt(256);
                DATA[i + 3] = (byte) random.nextInt(256);
                DATA[i + 4] = (byte) random.nextInt(256);
                DATA[i + 5] = (byte) random.nextInt(256);
                DATA[i + 6] = (byte) random.nextInt(256);
            }
            else
            {
                DATA[i] = 1;
                DATA[i + 1] = 1;
                DATA[i + 2] = 1;
                DATA[i + 3] = 2;
                DATA[i + 4] = 2;
                DATA[i + 5] = 2;
                DATA[i + 6] = 2;
            }
        }
        while(i<DATA.length)
        {
            DATA[i++] = (byte) random.nextInt(256);
        }
    }
    
    private final SnappyApi snappyNative = SnappyLoader.loadSnappyApi();
    private final SnappyApi snappyJava = new PureJavaSnappy();

    @Test
    public void testNativeCompressJavaDecompress() throws Exception
    {
        ByteBuffer raw = ByteBuffer.allocateDirect(DATA.length);
        ByteBuffer compressed = ByteBuffer.allocateDirect(Snappy.maxCompressedLength(DATA.length));
        
        raw.put(DATA);
        raw.flip();

        final int length = snappyNative.rawCompress(raw, 0, raw.remaining(), compressed, 0);
        compressed.limit(length);

        ByteBuffer actual = ByteBuffer.allocateDirect(DATA.length);
        snappyJava.rawUncompress(compressed, 0, compressed.remaining(), actual, 0);

        actual.flip();
        assertRawData(0, DATA.length, actual);
    }

    @Test
    public void testSnappyCompressNativeDecompress() throws Exception
    {
        ByteBuffer raw = ByteBuffer.allocateDirect(DATA.length);
        ByteBuffer compressed = ByteBuffer.allocateDirect(Snappy.maxCompressedLength(DATA.length));
        
        raw.put(DATA);
        raw.flip();

        final int length = snappyJava.rawCompress(raw, 0, raw.remaining(), compressed, 0);
        compressed.limit(length);

        ByteBuffer actual = ByteBuffer.allocateDirect(DATA.length);
        snappyNative.rawUncompress(compressed, 0, compressed.remaining(), actual, 0);

        actual.flip();
        assertRawData(0, DATA.length, actual);
    }

    private static void assertRawData(int offset, int length, ByteBuffer actual)
    {
        assertEquals(length, actual.remaining());
        for (int i = 0; i<length; ++i)
        {
            assertEquals("value at idx: " + i, DATA[i + offset], actual.get());
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions