2020-10-14 01:38:44 +02:00
|
|
|
package it.tdlight.tdlibsession.td.middle;
|
|
|
|
|
|
|
|
import io.vertx.core.buffer.Buffer;
|
|
|
|
import io.vertx.core.eventbus.MessageCodec;
|
|
|
|
import it.tdlight.jni.TdApi;
|
|
|
|
import it.tdlight.jni.TdApi.Error;
|
|
|
|
import it.tdlight.tdlibsession.td.TdResult;
|
2021-01-23 01:41:42 +01:00
|
|
|
import it.tdlight.utils.VertxBufferInputStream;
|
|
|
|
import it.tdlight.utils.VertxBufferOutputStream;
|
2020-10-14 01:38:44 +02:00
|
|
|
import java.io.DataInputStream;
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
|
2021-01-13 17:22:14 +01:00
|
|
|
public class TdResultListMessageCodec implements MessageCodec<TdResultList, TdResultList> {
|
2020-10-14 01:38:44 +02:00
|
|
|
|
2021-01-13 17:22:14 +01:00
|
|
|
public TdResultListMessageCodec() {
|
2020-10-14 01:38:44 +02:00
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-13 17:22:14 +01:00
|
|
|
public void encodeToWire(Buffer buffer, TdResultList ts) {
|
2021-01-23 01:41:42 +01:00
|
|
|
try (var bos = new VertxBufferOutputStream(buffer)) {
|
2020-10-14 01:38:44 +02:00
|
|
|
try (var dos = new DataOutputStream(bos)) {
|
2021-01-13 17:22:14 +01:00
|
|
|
var t = ts.getValues();
|
|
|
|
dos.writeInt(t.size());
|
|
|
|
for (TdResult<TdApi.Object> t1 : t) {
|
|
|
|
if (t1.succeeded()) {
|
|
|
|
dos.writeBoolean(true);
|
|
|
|
t1.result().serialize(dos);
|
|
|
|
} else {
|
|
|
|
dos.writeBoolean(false);
|
|
|
|
t1.cause().serialize(dos);
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-13 17:22:14 +01:00
|
|
|
public TdResultList decodeFromWire(int pos, Buffer buffer) {
|
2021-01-23 01:41:42 +01:00
|
|
|
try (var fis = new VertxBufferInputStream(buffer, pos, buffer.length())) {
|
2020-10-14 01:38:44 +02:00
|
|
|
try (var dis = new DataInputStream(fis)) {
|
|
|
|
var size = dis.readInt();
|
2021-01-23 01:41:42 +01:00
|
|
|
ArrayList<TdResult<TdApi.Object>> list = new ArrayList<>(size);
|
2021-01-13 17:22:14 +01:00
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
if (dis.readBoolean()) {
|
|
|
|
list.add(TdResult.succeeded((TdApi.Object) TdApi.Deserializer.deserialize(dis)));
|
|
|
|
} else {
|
|
|
|
list.add(TdResult.failed((Error) TdApi.Deserializer.deserialize(dis)));
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-13 17:22:14 +01:00
|
|
|
return new TdResultList(list);
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
} catch (IOException | UnsupportedOperationException ex) {
|
|
|
|
ex.printStackTrace();
|
2021-01-13 17:22:14 +01:00
|
|
|
return new TdResultList(Collections.emptyList());
|
2020-10-14 01:38:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-01-13 17:22:14 +01:00
|
|
|
public TdResultList transform(TdResultList ts) {
|
2020-10-14 01:38:44 +02:00
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String name() {
|
|
|
|
return "TdOptListCodec";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public byte systemCodecID() {
|
|
|
|
// Always -1
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|