<commit_before>package net.morimekta.util.io;

import org.junit.Test;

import java.io.IOException;
import java.nio.ByteBuffer;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

/**
 * Testing for the ByteBufferInputStream and ByteBufferOutputStream.
 */
public class ByteBufferIOTest {
    @Test
    public void testHeapBuffer() throws IOException {
        byte[] data = new byte[]{9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4};

        ByteBuffer buffer = ByteBuffer.allocate(20);
        buffer.limit(20);
        ByteBufferOutputStream bbos = new ByteBufferOutputStream(buffer);

        bbos.write(0);
        bbos.write(data);
        bbos.write(data, 6, 2);

        buffer.flip();
        ByteBufferInputStream bbis = new ByteBufferInputStream(buffer);

        assertEquals(0, bbis.read());
        byte[] indata = new byte[data.length];
        assertEquals(16, bbis.read(indata));
        assertArrayEquals(data, indata);
        byte[] indata2 = new byte[data.length];
        assertEquals(2, bbis.read(indata2, 6, 2));
        assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0},
                          indata2);
    }
}<commit_msg>Add test for direct buffers.<commit_after>package net.morimekta.util.io;

import org.junit.Test;

import java.io.IOException;
import java.nio.ByteBuffer;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

/**
 * Testing for the ByteBufferInputStream and ByteBufferOutputStream.
 */
public class ByteBufferIOTest {
    @Test
    public void testHeapBuffer() throws IOException {
        byte[] data = new byte[]{9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4};

        ByteBuffer buffer = ByteBuffer.allocate(20);
        buffer.limit(20);
        ByteBufferOutputStream bbos = new ByteBufferOutputStream(buffer);

        bbos.write(0);
        bbos.write(data);
        bbos.write(data, 6, 2);

        buffer.flip();
        ByteBufferInputStream bbis = new ByteBufferInputStream(buffer);

        assertEquals(0, bbis.read());
        byte[] indata = new byte[data.length];
        assertEquals(16, bbis.read(indata));
        assertArrayEquals(data, indata);
        byte[] indata2 = new byte[data.length];
        assertEquals(2, bbis.read(indata2, 6, 2));
        assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0},
                          indata2);
    }

    @Test
    public void testDirectBuffer() throws IOException {
        byte[] data = new byte[]{9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4};

        ByteBuffer buffer = ByteBuffer.allocateDirect(20);
        buffer.limit(20);
        ByteBufferOutputStream bbos = new ByteBufferOutputStream(buffer);

        bbos.write(0);
        bbos.write(data);
        bbos.write(data, 6, 2);

        buffer.flip();
        ByteBufferInputStream bbis = new ByteBufferInputStream(buffer);

        assertEquals(0, bbis.read());
        byte[] indata = new byte[data.length];
        assertEquals(16, bbis.read(indata));
        assertArrayEquals(data, indata);
        byte[] indata2 = new byte[data.length];
        assertEquals(2, bbis.read(indata2, 6, 2));
        assertArrayEquals(new byte[]{0, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0},
                          indata2);
    }
}
{'fraction_non_alphanumeric': 0.13385121003884076, 'fraction_numerical': 0.04302360322677024, 'mean_word_length': 2.6670317634173055, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6987316', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1045, 'n_words': 287}