Search in sources :

Example 46 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project buck by facebook.

the class ElfTest method le32.

@Test
public void le32() throws IOException {
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "samples", tmp);
    workspace.setUp();
    Path elfPath = workspace.resolve(Paths.get("le32.o"));
    try (FileChannel channel = FileChannel.open(elfPath)) {
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        Elf elf = new Elf(buffer);
        assertEquals(ElfHeader.EIClass.ELFCLASS32, elf.header.ei_class);
        assertEquals(ElfHeader.EIData.ELFDATA2LSB, elf.header.ei_data);
        assertEquals(9, elf.getNumberOfSections());
        assertTrue(elf.getSectionByName(".text").isPresent());
    }
}
Also used : Path(java.nio.file.Path) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Example 47 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project buck by facebook.

the class ElfTest method sectionTypes.

@Test
public void sectionTypes() throws IOException {
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "samples", tmp);
    workspace.setUp();
    Path elfPath = workspace.resolve(Paths.get("section_types.o"));
    try (FileChannel channel = FileChannel.open(elfPath)) {
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        Elf elf = new Elf(buffer);
        Optional<ElfSection> section;
        section = elf.getSectionByName(".text").map(Pair::getSecond);
        assertTrue(section.isPresent());
        assertEquals(ElfSectionHeader.SHType.SHT_PROGBITS, section.get().header.sh_type);
        section = elf.getSectionByName(".bss").map(Pair::getSecond);
        assertTrue(section.isPresent());
        assertEquals(ElfSectionHeader.SHType.SHT_NOBITS, section.get().header.sh_type);
        section = elf.getSectionByName(".strtab").map(Pair::getSecond);
        assertTrue(section.isPresent());
        assertEquals(ElfSectionHeader.SHType.SHT_STRTAB, section.get().header.sh_type);
        section = elf.getSectionByName(".symtab").map(Pair::getSecond);
        assertTrue(section.isPresent());
        assertEquals(ElfSectionHeader.SHType.SHT_SYMTAB, section.get().header.sh_type);
    }
}
Also used : Path(java.nio.file.Path) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Example 48 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project buck by facebook.

the class ElfTest method lotsOfSectionHeaders.

@Test
public void lotsOfSectionHeaders() throws IOException {
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "samples", tmp);
    workspace.setUp();
    Path elfPath = workspace.resolve(Paths.get("has43664sections.o"));
    try (FileChannel channel = FileChannel.open(elfPath)) {
        MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        Elf elf = new Elf(buffer);
        assertThat(elf.getNumberOfSections(), Matchers.equalTo(43664));
    }
}
Also used : Path(java.nio.file.Path) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Example 49 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project hadoop by apache.

the class MappableBlock method load.

/**
   * Load the block.
   *
   * mmap and mlock the block, and then verify its checksum.
   *
   * @param length         The current length of the block.
   * @param blockIn        The block input stream.  Should be positioned at the
   *                       start.  The caller must close this.
   * @param metaIn         The meta file input stream.  Should be positioned at
   *                       the start.  The caller must close this.
   * @param blockFileName  The block file name, for logging purposes.
   *
   * @return               The Mappable block.
   */
public static MappableBlock load(long length, FileInputStream blockIn, FileInputStream metaIn, String blockFileName) throws IOException {
    MappableBlock mappableBlock = null;
    MappedByteBuffer mmap = null;
    FileChannel blockChannel = null;
    try {
        blockChannel = blockIn.getChannel();
        if (blockChannel == null) {
            throw new IOException("Block InputStream has no FileChannel.");
        }
        mmap = blockChannel.map(MapMode.READ_ONLY, 0, length);
        NativeIO.POSIX.getCacheManipulator().mlock(blockFileName, mmap, length);
        verifyChecksum(length, metaIn, blockChannel, blockFileName);
        mappableBlock = new MappableBlock(mmap, length);
    } finally {
        IOUtils.closeQuietly(blockChannel);
        if (mappableBlock == null) {
            if (mmap != null) {
                // unmapping also unlocks
                NativeIO.POSIX.munmap(mmap);
            }
        }
    }
    return mappableBlock;
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException)

Example 50 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project druid by druid-io.

the class HadoopConverterJobTest method corrupt.

private static void corrupt(DataSegment segment) throws IOException {
    final Map<String, Object> localLoadSpec = segment.getLoadSpec();
    final Path segmentPath = Paths.get(localLoadSpec.get("path").toString());
    final MappedByteBuffer buffer = Files.map(segmentPath.toFile(), FileChannel.MapMode.READ_WRITE);
    while (buffer.hasRemaining()) {
        buffer.put((byte) 0xFF);
    }
}
Also used : Path(java.nio.file.Path) MappedByteBuffer(java.nio.MappedByteBuffer)

Aggregations

MappedByteBuffer (java.nio.MappedByteBuffer)154 FileChannel (java.nio.channels.FileChannel)75 IOException (java.io.IOException)44 File (java.io.File)38 RandomAccessFile (java.io.RandomAccessFile)36 FileInputStream (java.io.FileInputStream)29 ByteBuffer (java.nio.ByteBuffer)24 Test (org.junit.Test)18 Path (java.nio.file.Path)11 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)9 Elf (com.facebook.buck.cxx.elf.Elf)8 FileOutputStream (java.io.FileOutputStream)8 ElfSection (com.facebook.buck.cxx.elf.ElfSection)6 FileNotFoundException (java.io.FileNotFoundException)5 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)4 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)4 Pair (com.facebook.buck.model.Pair)3 Date (java.util.Date)3 NulTerminatedCharsetDecoder (com.facebook.buck.charset.NulTerminatedCharsetDecoder)2 ElfDynamicSection (com.facebook.buck.cxx.elf.ElfDynamicSection)2