mirror of
https://github.com/oxen-io/session-android.git
synced 2025-04-30 09:20:53 +00:00
Switch to varin32 for contact input/output stream headers.
// FREEBIE
This commit is contained in:
parent
4731a34252
commit
d044a11bc0
@ -18,7 +18,7 @@ public class DeviceContactsInputStream {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public DeviceContact read() throws IOException {
|
public DeviceContact read() throws IOException {
|
||||||
long detailsLength = readRawVarint64();
|
long detailsLength = readRawVarint32();
|
||||||
byte[] detailsSerialized = new byte[(int)detailsLength];
|
byte[] detailsSerialized = new byte[(int)detailsLength];
|
||||||
Util.readFully(in, detailsSerialized);
|
Util.readFully(in, detailsSerialized);
|
||||||
|
|
||||||
@ -38,19 +38,40 @@ public class DeviceContactsInputStream {
|
|||||||
return new DeviceContact(number, name, avatar);
|
return new DeviceContact(number, name, avatar);
|
||||||
}
|
}
|
||||||
|
|
||||||
private long readRawVarint64() throws IOException {
|
public int readRawVarint32() throws IOException {
|
||||||
int shift = 0;
|
byte tmp = (byte)in.read();
|
||||||
long result = 0;
|
if (tmp >= 0) {
|
||||||
while (shift < 64) {
|
return tmp;
|
||||||
final byte b = (byte)in.read();
|
}
|
||||||
result |= (long)(b & 0x7F) << shift;
|
int result = tmp & 0x7f;
|
||||||
if ((b & 0x80) == 0) {
|
if ((tmp = (byte)in.read()) >= 0) {
|
||||||
return result;
|
result |= tmp << 7;
|
||||||
|
} else {
|
||||||
|
result |= (tmp & 0x7f) << 7;
|
||||||
|
if ((tmp = (byte)in.read()) >= 0) {
|
||||||
|
result |= tmp << 14;
|
||||||
|
} else {
|
||||||
|
result |= (tmp & 0x7f) << 14;
|
||||||
|
if ((tmp = (byte)in.read()) >= 0) {
|
||||||
|
result |= tmp << 21;
|
||||||
|
} else {
|
||||||
|
result |= (tmp & 0x7f) << 21;
|
||||||
|
result |= (tmp = (byte)in.read()) << 28;
|
||||||
|
if (tmp < 0) {
|
||||||
|
// Discard upper 32 bits.
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
if ((byte)in.read() >= 0) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new IOException("Malformed varint!");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
shift += 7;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new IOException("Malformed varint!");
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class LimitedInputStream extends FilterInputStream {
|
private static final class LimitedInputStream extends FilterInputStream {
|
||||||
|
@ -56,17 +56,17 @@ public class DeviceContactsOutputStream {
|
|||||||
|
|
||||||
byte[] serializedContactDetails = contactDetails.build().toByteArray();
|
byte[] serializedContactDetails = contactDetails.build().toByteArray();
|
||||||
|
|
||||||
writeVarint64(serializedContactDetails.length);
|
writeVarint32(serializedContactDetails.length);
|
||||||
out.write(serializedContactDetails);
|
out.write(serializedContactDetails);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeVarint64(long value) throws IOException {
|
private void writeVarint32(int value) throws IOException {
|
||||||
while (true) {
|
while (true) {
|
||||||
if ((value & ~0x7FL) == 0) {
|
if ((value & ~0x7F) == 0) {
|
||||||
out.write((int) value);
|
out.write(value);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
out.write(((int) value & 0x7F) | 0x80);
|
out.write((value & 0x7F) | 0x80);
|
||||||
value >>>= 7;
|
value >>>= 7;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user