use of com.koushikdutta.async.ByteBufferList 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 com.koushikdutta.async.ByteBufferList 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 com.koushikdutta.async.ByteBufferList in project ion by koush.
the class GsonTests method testParserCastingCallbackError.
public void testParserCastingCallbackError() throws Exception {
ByteBufferList b = new ByteBufferList(ByteBuffer.wrap("[]".getBytes()));
FilteredDataEmitter emitter = new FilteredDataEmitter() {
@Override
public boolean isPaused() {
return false;
}
};
GsonObjectParser g = new GsonObjectParser();
Future<JsonObject> ret = g.parse(emitter);
emitter.onDataAvailable(emitter, b);
emitter.getEndCallback().onCompleted(null);
final Semaphore s = new Semaphore(0);
ret.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
assertNull(result);
assertNotNull(e);
assertTrue(e instanceof ClassCastException);
s.release();
}
});
s.acquire();
}
use of com.koushikdutta.async.ByteBufferList in project ion by koush.
the class GsonTests method testParserCastingError.
public void testParserCastingError() throws Exception {
ByteBufferList b = new ByteBufferList(ByteBuffer.wrap("[]".getBytes()));
FilteredDataEmitter emitter = new FilteredDataEmitter() {
@Override
public boolean isPaused() {
return false;
}
};
GsonObjectParser g = new GsonObjectParser();
Future<JsonObject> ret = g.parse(emitter);
emitter.onDataAvailable(emitter, b);
emitter.getEndCallback().onCompleted(null);
try {
JsonObject j = ret.get();
fail(j.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.koushikdutta.async.ByteBufferList in project ion by koush.
the class RequestBodyUploadObserver method write.
@Override
public void write(AsyncHttpRequest request, final DataSink sink, final CompletedCallback completed) {
final int length = body.length();
body.write(request, new DataSink() {
int totalWritten;
@Override
public void write(ByteBufferList bb) {
int start = bb.remaining();
sink.write(bb);
int wrote = start - bb.remaining();
totalWritten += wrote;
callback.onProgress(totalWritten, length);
}
@Override
public void setWriteableCallback(WritableCallback handler) {
sink.setWriteableCallback(handler);
}
@Override
public WritableCallback getWriteableCallback() {
return sink.getWriteableCallback();
}
@Override
public boolean isOpen() {
return sink.isOpen();
}
@Override
public void end() {
sink.end();
}
@Override
public void setClosedCallback(CompletedCallback handler) {
sink.setClosedCallback(handler);
}
@Override
public CompletedCallback getClosedCallback() {
return sink.getClosedCallback();
}
@Override
public AsyncServer getServer() {
return sink.getServer();
}
}, completed);
}
Aggregations