mirror of
https://github.com/oxen-io/session-android.git
synced 2025-04-30 04:20:45 +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 {
|
||||
long detailsLength = readRawVarint64();
|
||||
long detailsLength = readRawVarint32();
|
||||
byte[] detailsSerialized = new byte[(int)detailsLength];
|
||||
Util.readFully(in, detailsSerialized);
|
||||
|
||||
@ -38,20 +38,41 @@ public class DeviceContactsInputStream {
|
||||
return new DeviceContact(number, name, avatar);
|
||||
}
|
||||
|
||||
private long readRawVarint64() throws IOException {
|
||||
int shift = 0;
|
||||
long result = 0;
|
||||
while (shift < 64) {
|
||||
final byte b = (byte)in.read();
|
||||
result |= (long)(b & 0x7F) << shift;
|
||||
if ((b & 0x80) == 0) {
|
||||
public int readRawVarint32() throws IOException {
|
||||
byte tmp = (byte)in.read();
|
||||
if (tmp >= 0) {
|
||||
return tmp;
|
||||
}
|
||||
int result = tmp & 0x7f;
|
||||
if ((tmp = (byte)in.read()) >= 0) {
|
||||
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;
|
||||
}
|
||||
shift += 7;
|
||||
}
|
||||
|
||||
throw new IOException("Malformed varint!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static final class LimitedInputStream extends FilterInputStream {
|
||||
|
||||
|
@ -56,17 +56,17 @@ public class DeviceContactsOutputStream {
|
||||
|
||||
byte[] serializedContactDetails = contactDetails.build().toByteArray();
|
||||
|
||||
writeVarint64(serializedContactDetails.length);
|
||||
writeVarint32(serializedContactDetails.length);
|
||||
out.write(serializedContactDetails);
|
||||
}
|
||||
|
||||
public void writeVarint64(long value) throws IOException {
|
||||
private void writeVarint32(int value) throws IOException {
|
||||
while (true) {
|
||||
if ((value & ~0x7FL) == 0) {
|
||||
out.write((int) value);
|
||||
if ((value & ~0x7F) == 0) {
|
||||
out.write(value);
|
||||
return;
|
||||
} else {
|
||||
out.write(((int) value & 0x7F) | 0x80);
|
||||
out.write((value & 0x7F) | 0x80);
|
||||
value >>>= 7;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user