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 OpenGrok by OpenGrok.
the class ELFAnalyzer method parseELF.
public String parseELF(FileChannel fch) throws IOException {
MappedByteBuffer fmap = fch.map(FileChannel.MapMode.READ_ONLY, 0, fch.size());
ELFHeader eh = new ELFHeader(fmap);
if (eh.e_shnum <= 0) {
LOGGER.log(Level.FINE, "Skipping file, no section headers");
return null;
}
fmap.position(eh.e_shoff + (eh.e_shstrndx * eh.e_shentsize));
ELFSection stringSection = new ELFSection(fmap);
if (stringSection.sh_size == 0) {
LOGGER.log(Level.FINE, "Skipping file, no section name string table");
return null;
}
HashMap<String, Integer> sectionMap = new HashMap<String, Integer>();
ELFSection[] sections = new ELFSection[eh.e_shnum];
int[] readables = new int[eh.e_shnum];
int ri = 0;
for (int i = 0; i < eh.e_shnum; i++) {
fmap.position(eh.e_shoff + (i * eh.e_shentsize));
sections[i] = new ELFSection(fmap);
String sectionName = getName(stringSection.sh_offset, sections[i].sh_name, fmap);
if (sectionName != null) {
sectionMap.put(sectionName, sections[i].sh_offset);
}
if (sections[i].sh_type == ELFSection.SHT_STRTAB) {
readables[ri++] = i;
} else if (READABLE_SECTIONS.contains(sectionName)) {
readables[ri++] = i;
}
}
boolean lastPrintable = false;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ri; i++) {
fmap.position(sections[readables[i]].sh_offset);
int size = sections[readables[i]].sh_size;
byte c;
while (size-- > 0) {
c = fmap.get();
if (isReadable(c)) {
lastPrintable = true;
sb.append((char) c);
} else if (lastPrintable) {
lastPrintable = false;
sb.append(' ');
}
}
sb.append('\n');
}
return sb.toString();
}
use of java.nio.MappedByteBuffer in project LuaViewSDK by alibaba.
the class ChannelTools method toBytes.
/**
* file path to
*
* @param filepath
* @param sizes
* @return
*/
public static List<byte[]> toBytes(String filepath, int[] sizes) {
List<byte[]> result = new ArrayList<byte[]>();
try {
RandomAccessFile randomAccessFile = new RandomAccessFile(filepath, "r");
MappedByteBuffer buffer = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, randomAccessFile.length());
if (sizes != null && sizes.length > 0) {
for (int size : sizes) {
byte[] r = new byte[size];
//fill buffer
buffer.get(r);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
use of java.nio.MappedByteBuffer in project http-kit by http-kit.
the class NBlockingSSL method doHandshake.
private static void doHandshake() throws IOException {
SSLEngineResult.HandshakeStatus hs = engine.getHandshakeStatus();
isHandshakeDone = hs == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING || hs == SSLEngineResult.HandshakeStatus.FINISHED;
loop: while (!isHandshakeDone) {
switch(hs) {
case NEED_TASK:
Runnable runnable;
while ((runnable = engine.getDelegatedTask()) != null) {
logger.info("get task " + runnable);
runnable.run();
}
break;
case NEED_UNWRAP:
int read = socketChannel.read(peerNetData);
logger.info("read {} bytes", read);
if (read < 0) {
logger.info("closed");
// TODO closed
} else {
peerNetData.flip();
SSLEngineResult res = engine.unwrap(peerNetData, peerAppData);
logger.info("hs unwrap, " + res);
if (res.getStatus() != Status.OK) {
System.out.println("--------------------------");
}
peerNetData.compact();
switch(res.getStatus()) {
case OK:
break;
case BUFFER_UNDERFLOW:
// need more data from peer
logger.info("waiting for more info");
break loop;
case BUFFER_OVERFLOW:
// need larger peerAppData buffer
break;
case CLOSED:
break;
}
if (engine.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NEED_WRAP) {
key.interestOps(SelectionKey.OP_WRITE);
logger.info("for write");
break loop;
}
}
break;
case NEED_WRAP:
// myNetData.compact();
SSLEngineResult result = engine.wrap(ByteBuffer.allocate(0), myNetData);
logger.info("wrap: " + result);
myNetData.flip();
socketChannel.write(myNetData);
if (engine.getHandshakeStatus() == SSLEngineResult.HandshakeStatus.NEED_UNWRAP) {
}
if (!myNetData.hasRemaining()) {
// write done, so just for read
if ((key.interestOps() & SelectionKey.OP_READ) == 0) {
key.interestOps(SelectionKey.OP_READ);
logger.info("for read");
}
myNetData.clear();
// break loop;
} else {
myNetData.compact();
}
break;
}
hs = engine.getHandshakeStatus();
isHandshakeDone = hs == SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING || hs == SSLEngineResult.HandshakeStatus.FINISHED;
if (isHandshakeDone) {
logger.info("handshake done");
peerNetData.clear();
ByteBuffer buffer = ByteBuffer.wrap(("GET / HTTP/1.1\r\nHost: " + HOST + "\r\n\r\n").getBytes());
SSLEngineResult res = engine.wrap(buffer, myNetData);
RandomAccessFile r = new RandomAccessFile("/home/feng/workspace/http-kit/blog.access.log", "r");
MappedByteBuffer b = r.getChannel().map(MapMode.READ_ONLY, 0, r.getChannel().size());
ByteBuffer bf = ByteBuffer.allocate(256 * 1024);
// even though b is big, bf is small, the two buffer just move
// forward
SSLEngineResult t = engine.wrap(b, bf);
System.out.println(t);
if (res.getStatus() == SSLEngineResult.Status.OK) {
myNetData.flip();
socketChannel.write(myNetData);
if (myNetData.hasRemaining()) {
key.interestOps(SelectionKey.OP_WRITE);
}
}
}
}
}
use of java.nio.MappedByteBuffer in project mapdb by jankotek.
the class ByteBufferVol method unmap.
/**
* Hack to unmap MappedByteBuffer.
* Unmap is necessary on Windows, otherwise file is locked until JVM exits or BB is GCed.
* There is no public JVM API to unmap buffer, so this tries to use SUN proprietary API for unmap.
* Any error is silently ignored (for example SUN API does not exist on Android).
*/
protected static boolean unmap(MappedByteBuffer b) {
if (!unmapHackSupported) {
return false;
}
if (!(b instanceof DirectBuffer))
return false;
// need to dispose old direct buffer, see bug
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038
DirectBuffer bb = (DirectBuffer) b;
Cleaner c = bb.cleaner();
if (c != null) {
c.clean();
return true;
}
Object attachment = bb.attachment();
return attachment != null && attachment instanceof DirectBuffer && attachment != b && unmap((MappedByteBuffer) attachment);
}
Aggregations