Package com.fasterxml.util.membuf

Examples of com.fasterxml.util.membuf.ChunkyBytesMemBuffer


    private void _testChunkySkips(SegType aType) throws Exception
    {
        // will use segments of size 10 bytes; only one segment per-allocator reuse
        // and maximum allocation of 4 segments per-allocator
        // buffer will have similar limits
        final ChunkyBytesMemBuffer buffer = createBytesBuffers(aType, 10, 1, 4).createChunkyBuffer(1, 3);

        // append 5 segments
        for (int i = 5; i > 0; --i) {
            buffer.appendEntry(new byte[i]);
        }
        assertEquals(5, buffer.getEntryCount());
        assertEquals(15, buffer.getTotalPayloadLength());
        assertFalse(buffer.isEmpty());

        // then skip all of it
        assertEquals(5, buffer.skipNextEntry());
        assertEquals(4, buffer.skipNextEntry());
        assertEquals(3, buffer.skipNextEntry());
        assertEquals(2, buffer.skipNextEntry());

        assertEquals(1, buffer.getNextEntryLength());
        assertEquals(1, buffer.getNextEntryLength());
        assertEquals(1, buffer.getNextEntryLength());
        assertEquals(1, buffer.skipNextEntry());
        // and when empty, nothing more:
        assertEquals(-1, buffer.skipNextEntry());
        assertTrue(buffer.isEmpty());
    }
View Full Code Here


        assertTrue(buffer.isEmpty());
    }

    private void _testChunkySkipAndRead(SegType aType) throws Exception
    {
        final ChunkyBytesMemBuffer buffer = createBytesBuffers(aType, 10, 1, 4).createChunkyBuffer(1, 3);

        for (int i = 5; i > 0; --i) { // 5, 4, 3, 2, 1 segments
            buffer.appendEntry(new byte[i]);
        }
        assertEquals(5, buffer.getEntryCount());
        assertEquals(15, buffer.getTotalPayloadLength());
        assertFalse(buffer.isEmpty());

        // then skip all of it
        assertEquals(5, buffer.skipNextEntry());
        byte[] b = buffer.getNextEntry(10L);
        assertEquals(4, b.length);
        assertEquals(3, buffer.skipNextEntry());
        b = buffer.getNextEntry(10L);
        assertEquals(2, b.length);
        assertEquals(1, buffer.skipNextEntry());
        // and when empty, nothing more:
        assertEquals(-1, buffer.skipNextEntry());
        assertTrue(buffer.isEmpty());
    }
View Full Code Here

    }

    // Test to verify that skip works across buffer boundaries
    private void _testChunkyLongerSkip(SegType aType) throws Exception
    {
        final ChunkyBytesMemBuffer buffer = createBytesBuffers(aType, 10, 1, 4).createChunkyBuffer(1, 3);
        // maximum: 29 data bytes, 1 for length
        buffer.appendEntry(new byte[29]);
        assertEquals(29, buffer.skipNextEntry());
        assertEquals(-1, buffer.skipNextEntry());
    }
View Full Code Here

    */

   private void _testChunkyPeeks(SegType aType) throws Exception
   {
       // 10 byte segments, max 4
       final ChunkyBytesMemBuffer buffer = createBytesBuffers(aType, 10, 1, 4).createChunkyBuffer(1, 4);

       // append 6 segments
       for (int i = 1; i <= 6; ++i) {
           buffer.appendEntry(buildBytesChunk(i));
       }
       assertEquals(6, buffer.getEntryCount());
       assertEquals(21, buffer.getTotalPayloadLength());
       assertFalse(buffer.isEmpty());

       // then peek, read/skip
       byte[] chunk = buffer.peekNextEntry();
       Assert.assertArrayEquals(buildBytesChunk(1), chunk);
       assertEquals(1, buffer.skipNextEntry());

       assertEquals(2, buffer.getNextEntryLength());
       chunk = buffer.peekNextEntry();
       Assert.assertArrayEquals(buildBytesChunk(2), chunk);
       assertEquals(2, buffer.getNextEntryLength());
       byte[] gotten = buffer.getNextEntryIfAvailable();
       Assert.assertArrayEquals(chunk, gotten);

       chunk = buffer.getNextEntryIfAvailable();
       Assert.assertArrayEquals(buildBytesChunk(3), chunk);

       chunk = buffer.peekNextEntry();
       Assert.assertArrayEquals(buildBytesChunk(4), chunk);
       // should be idempotent
       byte[] chunk2 = buffer.peekNextEntry();
       Assert.assertArrayEquals(chunk, chunk2);
       assertEquals(4, buffer.skipNextEntry());
      
       assertEquals(5, buffer.skipNextEntry());

       chunk = buffer.peekNextEntry();
       Assert.assertArrayEquals(buildBytesChunk(6), chunk);
       assertEquals(6, buffer.getNextEntryLength());
       byte[] buf = new byte[6];
       assertEquals(6, buffer.readNextEntry(buf, 0));
       Assert.assertArrayEquals(chunk, buf);

       // and when empty, nothing more:
       assertEquals(-1, buffer.skipNextEntry());
       assertTrue(buffer.isEmpty());

       buffer.close();
   }
View Full Code Here

TOP

Related Classes of com.fasterxml.util.membuf.ChunkyBytesMemBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.