Search in sources :

Example 16 with FileChannel

use of java.nio.channels.FileChannel in project kafka by apache.

the class FileRecordsTest method testTruncateNotCalledIfSizeIsSameAsTargetSize.

/**
     * Test that truncateTo only calls truncate on the FileChannel if the size of the
     * FileChannel is bigger than the target size. This is important because some JVMs
     * change the mtime of the file, even if truncate should do nothing.
     */
@Test
public void testTruncateNotCalledIfSizeIsSameAsTargetSize() throws IOException {
    FileChannel channelMock = EasyMock.createMock(FileChannel.class);
    EasyMock.expect(channelMock.size()).andReturn(42L).atLeastOnce();
    EasyMock.expect(channelMock.position(42L)).andReturn(null);
    EasyMock.replay(channelMock);
    FileRecords fileRecords = new FileRecords(tempFile(), channelMock, 0, Integer.MAX_VALUE, false);
    fileRecords.truncateTo(42);
    EasyMock.verify(channelMock);
}
Also used : FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Example 17 with FileChannel

use of java.nio.channels.FileChannel in project kafka by apache.

the class FileRecordsTest method testTruncateIfSizeIsDifferentToTargetSize.

/**
     * see #testTruncateNotCalledIfSizeIsSameAsTargetSize
     */
@Test
public void testTruncateIfSizeIsDifferentToTargetSize() throws IOException {
    FileChannel channelMock = EasyMock.createMock(FileChannel.class);
    EasyMock.expect(channelMock.size()).andReturn(42L).atLeastOnce();
    EasyMock.expect(channelMock.position(42L)).andReturn(null).once();
    EasyMock.expect(channelMock.truncate(23L)).andReturn(null).once();
    EasyMock.expect(channelMock.position(23L)).andReturn(null).once();
    EasyMock.replay(channelMock);
    FileRecords fileRecords = new FileRecords(tempFile(), channelMock, 0, Integer.MAX_VALUE, false);
    fileRecords.truncateTo(23);
    EasyMock.verify(channelMock);
}
Also used : FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Example 18 with FileChannel

use of java.nio.channels.FileChannel in project kafka by apache.

the class FileRecordsTest method testTruncateNotCalledIfSizeIsBiggerThanTargetSize.

/**
     * Expect a KafkaException if targetSize is bigger than the size of
     * the FileRecords.
     */
@Test
public void testTruncateNotCalledIfSizeIsBiggerThanTargetSize() throws IOException {
    FileChannel channelMock = EasyMock.createMock(FileChannel.class);
    EasyMock.expect(channelMock.size()).andReturn(42L).atLeastOnce();
    EasyMock.expect(channelMock.position(42L)).andReturn(null);
    EasyMock.replay(channelMock);
    FileRecords fileRecords = new FileRecords(tempFile(), channelMock, 0, Integer.MAX_VALUE, false);
    try {
        fileRecords.truncateTo(43);
        fail("Should throw KafkaException");
    } catch (KafkaException e) {
    // expected
    }
    EasyMock.verify(channelMock);
}
Also used : FileChannel(java.nio.channels.FileChannel) KafkaException(org.apache.kafka.common.KafkaException) Test(org.junit.Test)

Example 19 with FileChannel

use of java.nio.channels.FileChannel in project kafka by apache.

the class UtilsTest method testReadFullyWithPartialFileChannelReads.

/**
     * Tests that `readFullyOrFail` behaves correctly if multiple `FileChannel.read` operations are required to fill
     * the destination buffer.
     */
@Test
public void testReadFullyWithPartialFileChannelReads() throws IOException {
    FileChannel channelMock = EasyMock.createMock(FileChannel.class);
    final int bufferSize = 100;
    StringBuilder expectedBufferContent = new StringBuilder();
    fileChannelMockExpectReadWithRandomBytes(channelMock, expectedBufferContent, bufferSize);
    EasyMock.replay(channelMock);
    ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
    Utils.readFully(channelMock, buffer, 0L);
    assertEquals("The buffer should be populated correctly.", expectedBufferContent.toString(), new String(buffer.array()));
    assertFalse("The buffer should be filled", buffer.hasRemaining());
    EasyMock.verify(channelMock);
}
Also used : FileChannel(java.nio.channels.FileChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 20 with FileChannel

use of java.nio.channels.FileChannel in project kafka by apache.

the class UtilsTest method testReadFullyOrFailWithRealFile.

@Test
public void testReadFullyOrFailWithRealFile() throws IOException {
    try (FileChannel channel = FileChannel.open(TestUtils.tempFile().toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE)) {
        // prepare channel
        String msg = "hello, world";
        channel.write(ByteBuffer.wrap(msg.getBytes()), 0);
        channel.force(true);
        assertEquals("Message should be written to the file channel", channel.size(), msg.length());
        ByteBuffer perfectBuffer = ByteBuffer.allocate(msg.length());
        ByteBuffer smallBuffer = ByteBuffer.allocate(5);
        ByteBuffer largeBuffer = ByteBuffer.allocate(msg.length() + 1);
        // Scenario 1: test reading into a perfectly-sized buffer
        Utils.readFullyOrFail(channel, perfectBuffer, 0, "perfect");
        assertFalse("Buffer should be filled up", perfectBuffer.hasRemaining());
        assertEquals("Buffer should be populated correctly", msg, new String(perfectBuffer.array()));
        // Scenario 2: test reading into a smaller buffer
        Utils.readFullyOrFail(channel, smallBuffer, 0, "small");
        assertFalse("Buffer should be filled", smallBuffer.hasRemaining());
        assertEquals("Buffer should be populated correctly", "hello", new String(smallBuffer.array()));
        // Scenario 3: test reading starting from a non-zero position
        smallBuffer.clear();
        Utils.readFullyOrFail(channel, smallBuffer, 7, "small");
        assertFalse("Buffer should be filled", smallBuffer.hasRemaining());
        assertEquals("Buffer should be populated correctly", "world", new String(smallBuffer.array()));
        // Scenario 4: test end of stream is reached before buffer is filled up
        try {
            Utils.readFullyOrFail(channel, largeBuffer, 0, "large");
            fail("Expected EOFException to be raised");
        } catch (EOFException e) {
        // expected
        }
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) EOFException(java.io.EOFException) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

FileChannel (java.nio.channels.FileChannel)629 IOException (java.io.IOException)227 ByteBuffer (java.nio.ByteBuffer)205 File (java.io.File)185 FileInputStream (java.io.FileInputStream)164 FileOutputStream (java.io.FileOutputStream)147 RandomAccessFile (java.io.RandomAccessFile)144 Test (org.junit.Test)95 MappedByteBuffer (java.nio.MappedByteBuffer)78 Path (java.nio.file.Path)37 FileLock (java.nio.channels.FileLock)32 FileNotFoundException (java.io.FileNotFoundException)29 Random (java.util.Random)12 OutputStream (java.io.OutputStream)11 ArrayList (java.util.ArrayList)11 AsynchronousFileChannel (java.nio.channels.AsynchronousFileChannel)10 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)10 LinkedList (java.util.LinkedList)10 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)9 BufferedReader (java.io.BufferedReader)9