Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added streaming protobufs using COBS
- Updated to Android Studio 2.0
- Loading branch information
1 parent
4eed052
commit 3ca815a
Showing
10 changed files
with
695 additions
and
372 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
app/src/main/java/com/sureshjoshi/android/streamingprotobufexample/utils/CobsUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.sureshjoshi.android.streamingprotobufexample.utils; | ||
|
||
public class CobsUtils { | ||
|
||
// Expected to be the entire packet to encode | ||
public static byte[] encode(byte[] packet) { | ||
if (packet == null | ||
|| packet.length == 0) { | ||
return new byte[]{}; | ||
} | ||
|
||
byte[] output = new byte[packet.length + 2]; | ||
byte blockStartValue = 1; | ||
int lastZeroIndex = 0; | ||
int srcIndex = 0; | ||
int destIndex = 1; | ||
|
||
while (srcIndex < packet.length) { | ||
if (packet[srcIndex] == 0) { | ||
output[lastZeroIndex] = blockStartValue; | ||
lastZeroIndex = destIndex++; | ||
blockStartValue = 1; | ||
} else { | ||
output[destIndex++] = packet[srcIndex]; | ||
if (++blockStartValue == 255) { | ||
output[lastZeroIndex] = blockStartValue; | ||
lastZeroIndex = destIndex++; | ||
blockStartValue = 1; | ||
} | ||
} | ||
|
||
++srcIndex; | ||
} | ||
|
||
output[lastZeroIndex] = blockStartValue; | ||
return output; | ||
} | ||
|
||
// Expected to be the entire packet to decode (must end with a 0) | ||
public static byte[] decode(byte[] packet) { | ||
if (packet == null | ||
|| packet.length == 0 | ||
|| packet[packet.length - 1] != 0) { | ||
return new byte[]{}; | ||
} | ||
|
||
byte[] output = new byte[packet.length - 2]; | ||
int srcPacketLength = packet.length - 1; | ||
int srcIndex = 0; | ||
int destIndex = 0; | ||
|
||
while (srcIndex < srcPacketLength) { | ||
int code = packet[srcIndex++] & 0xff; | ||
for (int i = 1; srcIndex < srcPacketLength && i < code; ++i) { | ||
output[destIndex++] = packet[srcIndex++]; | ||
} | ||
if (code != 255 && srcIndex != srcPacketLength) { | ||
output[destIndex++] = 0; | ||
} | ||
} | ||
|
||
return output; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#Wed Oct 21 11:34:03 PDT 2015 | ||
#Mon Apr 11 22:23:35 EDT 2016 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip |