use of java.nio.ByteBuffer in project AndroidAsync by koush.
the class DnsResponse method parse.
public static DnsResponse parse(ByteBufferList bb) {
ByteBuffer b = bb.getAll();
bb.add(b.duplicate());
// naive parsing...
bb.order(ByteOrder.BIG_ENDIAN);
// id
bb.getShort();
// flags
bb.getShort();
// number questions
int questions = bb.getShort();
// number answer rr
int answers = bb.getShort();
// number authority rr
int authorities = bb.getShort();
// number additional rr
int additionals = bb.getShort();
for (int i = 0; i < questions; i++) {
parseName(bb, b);
// type
bb.getShort();
// class
bb.getShort();
}
DnsResponse response = new DnsResponse();
for (int i = 0; i < answers; i++) {
String name = parseName(bb, b);
// type
int type = bb.getShort();
// class
int clazz = bb.getShort();
// ttl
int ttl = bb.getInt();
// length of address
int length = bb.getShort();
try {
if (type == 1) {
// data
byte[] data = new byte[length];
bb.get(data);
response.addresses.add(InetAddress.getByAddress(data));
} else if (type == 0x000c) {
response.names.add(parseName(bb, b));
} else if (type == 16) {
ByteBufferList txt = new ByteBufferList();
bb.get(txt, length);
response.parseTxt(txt);
} else {
bb.get(new byte[length]);
}
} catch (Exception e) {
// e.printStackTrace();
}
}
// authorities
for (int i = 0; i < authorities; i++) {
String name = parseName(bb, b);
// type
int type = bb.getShort();
// class
int clazz = bb.getShort();
// ttl
int ttl = bb.getInt();
// length of address
int length = bb.getShort();
try {
bb.get(new byte[length]);
} catch (Exception e) {
// e.printStackTrace();
}
}
// additionals
for (int i = 0; i < additionals; i++) {
String name = parseName(bb, b);
// type
int type = bb.getShort();
// class
int clazz = bb.getShort();
// ttl
int ttl = bb.getInt();
// length of address
int length = bb.getShort();
try {
if (type == 16) {
ByteBufferList txt = new ByteBufferList();
bb.get(txt, length);
response.parseTxt(txt);
} else {
bb.get(new byte[length]);
}
} catch (Exception e) {
// e.printStackTrace();
}
}
return response;
}
use of java.nio.ByteBuffer in project AndroidAsync by koush.
the class DnsResponse method parseName.
private static String parseName(ByteBufferList bb, ByteBuffer backReference) {
bb.order(ByteOrder.BIG_ENDIAN);
String ret = "";
int len;
while (0 != (len = bb.get() & 0x00FF)) {
// compressed
if ((len & 0x00c0) == 0x00c0) {
int offset = ((len & ~0xFFFFFFc0) << 8) | (bb.get() & 0x00FF);
if (ret.length() > 0)
ret += ".";
ByteBufferList sub = new ByteBufferList();
ByteBuffer duplicate = backReference.duplicate();
duplicate.get(new byte[offset]);
sub.add(duplicate);
return ret + parseName(sub, backReference);
}
byte[] bytes = new byte[len];
bb.get(bytes);
if (ret.length() > 0)
ret += ".";
ret += new String(bytes);
}
return ret;
}
use of java.nio.ByteBuffer in project AndroidAsync by koush.
the class AsyncNetworkSocket method onReadable.
int onReadable() {
spitPending();
// already in the selector's ready queue.
if (mPaused)
return 0;
int total = 0;
try {
boolean closed = false;
// ByteBufferList.obtainArray(buffers, Math.min(Math.max(mToAlloc, 2 << 11), maxAlloc));
ByteBuffer b = allocator.allocate();
// keep track of the max mount read during this read cycle
// so we can be quicker about allocations during the next
// time this socket reads.
long read = mChannel.read(b);
if (read < 0) {
closeInternal();
closed = true;
} else {
total += read;
}
if (read > 0) {
allocator.track(read);
b.flip();
// for (int i = 0; i < buffers.length; i++) {
// ByteBuffer b = buffers[i];
// buffers[i] = null;
// b.flip();
// pending.add(b);
// }
pending.add(b);
Util.emitAllData(this, pending);
} else {
ByteBufferList.reclaim(b);
}
if (closed) {
reportEndPending(null);
reportClose(null);
}
} catch (Exception e) {
closeInternal();
reportEndPending(e);
reportClose(e);
}
return total;
}
use of java.nio.ByteBuffer in project libgdx by libgdx.
the class BufferUtils method newUnsafeByteBuffer.
/** Allocates a new direct ByteBuffer from native heap memory using the native byte order. Needs to be disposed with
* {@link #freeMemory(ByteBuffer)}.
* @param numBytes */
public static ByteBuffer newUnsafeByteBuffer(int numBytes) {
ByteBuffer buffer = newDisposableByteBuffer(numBytes);
buffer.order(ByteOrder.nativeOrder());
allocatedUnsafe += numBytes;
synchronized (unsafeBuffers) {
unsafeBuffers.add(buffer);
}
return buffer;
}
use of java.nio.ByteBuffer in project hs4j by killme2008.
the class AbstractNioSession method doRealWrite.
protected final long doRealWrite(SelectableChannel channel, IoBuffer buffer) throws IOException {
if (log.isDebugEnabled()) {
StringBuffer bufMsg = new StringBuffer("send buffers:\n[\n");
final ByteBuffer buff = buffer.buf();
bufMsg.append(" buffer:position=").append(buff.position()).append(",limit=").append(buff.limit()).append(",capacity=").append(buff.capacity()).append("\n");
bufMsg.append("]");
bufMsg.append("\n");
for (byte b : buff.array()) bufMsg.append("0x").append(Integer.toHexString(b)).append(",");
bufMsg.append("\n");
bufMsg.append(new String(buff.array()));
log.debug(bufMsg.toString());
}
return ((WritableByteChannel) channel).write(buffer.buf());
}
Aggregations