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());
}
}
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);
}
}
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));
}
}
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;
}
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);
}
}
Aggregations