Package java.nio

Examples of java.nio.MappedByteBuffer


     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
     */
    public void test_map_ReadOnly_CloseChannel() throws IOException {
        // close channel has no effect on map if mapped
        assertEquals(0, readWriteFileChannel.size());
        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.READ_ONLY,
                0, CONTENT_LENGTH);
        assertEquals(CONTENT_LENGTH, readWriteFileChannel.size());
        readOnlyFileChannel.close();
        assertEquals(CONTENT_LENGTH, mapped.limit());
    }
View Full Code Here


    /**
     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
     */
    public void test_map_Private_CloseChannel() throws IOException {
        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.PRIVATE, 0,
                CONTENT_LENGTH);
        readWriteFileChannel.close();
        mapped.put(TEST_BYTES);
        assertEquals(CONTENT_LENGTH, mapped.limit());
        assertEquals("test".length(), mapped.position());
    }
View Full Code Here

    /**
     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
     */
    public void test_map_ReadOnly() throws IOException {
        MappedByteBuffer mapped = null;
        // try put something to readonly map
        writeDataToFile(fileOfReadOnlyFileChannel);
        mapped = readOnlyFileChannel.map(MapMode.READ_ONLY, 0, CONTENT_LENGTH);
        try {
            mapped.put(TEST_BYTES);
            fail("should throw ReadOnlyBufferException.");
        } catch (ReadOnlyBufferException ex) {
            // expected;
        }
        assertEquals(CONTENT_LENGTH, mapped.limit());
        assertEquals(CONTENT_LENGTH, mapped.capacity());
        assertEquals(0, mapped.position());

        // try to get a readonly map from read/write channel
        writeDataToFile(fileOfReadWriteFileChannel);
        mapped = readWriteFileChannel.map(MapMode.READ_ONLY, 0, CONTENT
                .length());
        assertEquals(CONTENT_LENGTH, mapped.limit());
        assertEquals(CONTENT_LENGTH, mapped.capacity());
        assertEquals(0, mapped.position());

        // map not change channel's position
        assertEquals(0, readOnlyFileChannel.position());
        assertEquals(0, readWriteFileChannel.position());
    }
View Full Code Here

    /**
     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
     */
    public void test_map_ReadOnly_NonZeroPosition() throws IOException {
        this.writeDataToFile(fileOfReadOnlyFileChannel);
        MappedByteBuffer mapped = readOnlyFileChannel.map(MapMode.READ_ONLY,
                10, CONTENT_LENGTH - 10);
        assertEquals(CONTENT_LENGTH - 10, mapped.limit());
        assertEquals(CONTENT_LENGTH - 10, mapped.capacity());
        assertEquals(0, mapped.position());
    }
View Full Code Here

    /**
     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
     */
    public void test_map_Private() throws IOException {
        this.writeDataToFile(fileOfReadWriteFileChannel);
        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.PRIVATE, 0,
                CONTENT_LENGTH);
        assertEquals(CONTENT_LENGTH, mapped.limit());
        // test copy on write if private
        ByteBuffer returnByPut = mapped.put(TEST_BYTES);
        assertSame(returnByPut, mapped);
        ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
        mapped.force();
        readWriteFileChannel.read(checkBuffer);
        assertEquals(CONTENT, new String(checkBuffer.array(), "iso8859-1"));

        // test overflow
        try {
            mapped.put(("test" + CONTENT).getBytes("iso8859-1"));
            fail("should throw BufferOverflowException.");
        } catch (BufferOverflowException ex) {
            // expected;
        }
    }
View Full Code Here

        }
        long alignment = position - position % ALLOC_GRANULARITY;
        int offset = (int) (position - alignment);
        PlatformAddress address = PlatformAddressFactory.allocMap(handle,
                alignment, size + offset, mapMode);
        MappedByteBuffer buffer = null;
        try {
            buffer = MappedByteBufferFactory.getBuffer(address, mapMode, size,
                    offset);
        } catch (Exception e) {
            throw new IOException(e.getMessage());
View Full Code Here

     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
     */
    public void test_map_ReadOnly_CloseChannel() throws IOException {
        // close channel has no effect on map if mapped
        assertEquals(0, readWriteFileChannel.size());
        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.READ_ONLY,
                0, CONTENT_LENGTH);
        assertEquals(CONTENT_LENGTH, readWriteFileChannel.size());
        readOnlyFileChannel.close();
        assertEquals(CONTENT_LENGTH, mapped.limit());
    }
View Full Code Here

    /**
     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
     */
    public void test_map_Private_CloseChannel() throws IOException {
        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.PRIVATE, 0,
                CONTENT_LENGTH);
        readWriteFileChannel.close();
        mapped.put(TEST_BYTES);
        assertEquals(CONTENT_LENGTH, mapped.limit());
        assertEquals("test".length(), mapped.position());
    }
View Full Code Here

    /**
     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
     */
    public void test_map_ReadOnly() throws IOException {
        MappedByteBuffer mapped = null;
        // try put something to readonly map
        writeDataToFile(fileOfReadOnlyFileChannel);
        mapped = readOnlyFileChannel.map(MapMode.READ_ONLY, 0, CONTENT_LENGTH);
        try {
            mapped.put(TEST_BYTES);
            fail("should throw ReadOnlyBufferException.");
        } catch (ReadOnlyBufferException ex) {
            // expected;
        }
        assertEquals(CONTENT_LENGTH, mapped.limit());
        assertEquals(CONTENT_LENGTH, mapped.capacity());
        assertEquals(0, mapped.position());

        // try to get a readonly map from read/write channel
        writeDataToFile(fileOfReadWriteFileChannel);
        mapped = readWriteFileChannel.map(MapMode.READ_ONLY, 0, CONTENT
                .length());
        assertEquals(CONTENT_LENGTH, mapped.limit());
        assertEquals(CONTENT_LENGTH, mapped.capacity());
        assertEquals(0, mapped.position());

        // map not change channel's position
        assertEquals(0, readOnlyFileChannel.position());
        assertEquals(0, readWriteFileChannel.position());
    }
View Full Code Here

    /**
     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
     */
    public void test_map_ReadOnly_NonZeroPosition() throws IOException {
        this.writeDataToFile(fileOfReadOnlyFileChannel);
        MappedByteBuffer mapped = readOnlyFileChannel.map(MapMode.READ_ONLY,
                10, CONTENT_LENGTH - 10);
        assertEquals(CONTENT_LENGTH - 10, mapped.limit());
        assertEquals(CONTENT_LENGTH - 10, mapped.capacity());
        assertEquals(0, mapped.position());
    }
View Full Code Here

TOP

Related Classes of java.nio.MappedByteBuffer

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.