I'm trying to download readable gtfs real time data (protocol buffer format) using Java so I can view it in a text file.
I tried a couple of approaches:
Approach #1:
URL url = new URL(uri);
byte[] buffer = new byte[4096];
InputStream is = url.openStream();
byte[] buffer = new byte[4096];
InputStream is = url.openStream();
File file = new File("c:/protobuf_data.txt");
OutputStream output = new FileOutputStream(file);
int numOfBytesReadIntoBuffer = -1;
while((numOfBytesReadIntoBuffer = is.read(buffer)) != -1){
output.write(buffer, 0, numOfBytesReadIntoBuffer);
}
results (snippet):
099700_L..S20150102*LÊ>0L 1637 8AV/RPY!¯¬œ¥¾¬œ¥"L22S(
Approach #2 (same results as approach #1): import org.apache.commons.io.IOUtils;
URL url = new URL(uri);
InputStream is = url.openStream();
File file = new File("c:/protobuf_data.txt");
OutputStream output = new FileOutputStream(file);
byte[] bytes = IOUtils.toByteArray(is);
output.write(bytes);
I guess because they both write to the OutputStream the same way, the results are the same.
I also tried the suggestion found here but I just ended up getting errors: When using google protocol buffers to transfer String character,got messy code
I read through protocol buffer docs but I got more confused. https://developers.google.com/protocol-buffers/docs/encoding
I used com.sun.org.apache.xml.internal.security.utils.Base64 but I get an error. Approach #3
URL url = new URL(uri);
InputStream is = url.openStream();
File file = new File("c:/users/Workstation/protobuf_data_bytes.txt");
OutputStream output = new FileOutputStream(file);
byte[] bytes = IOUtils.toByteArray(is);
Init.init();
byte[] decoded_bytes = Base64.decode(bytes);
error:
Exception in thread "main" com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException: Error while decoding
I also tried using java.util.Base64's wrap method to create an InputStream for decoding Base64 encoded byte stream but the data just got even more mangled.