base64 - 将 gtfs 实时数据流式传输为人类可读的格式

标签 base64 inputstream protocol-buffers gtfs

我正在尝试使用 Java 下载可读的 gtfs 实时数据( Protocol Buffer 格式),以便我可以在文本文件中查看它。

我尝试了几种方法:

方法#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(

方法 2(与方法 1 的结果相同): 导入 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);

我猜因为它们都以相同的方式写入OutputStream,所以结果是相同的。

我也尝试了这里的建议,但最终出现了错误: When using google protocol buffers to transfer String character,got messy code

我通读了 Protocol Buffer 文档,但我变得更加困惑。 https://developers.google.com/protocol-buffers/docs/encoding

我使用了 com.sun.org.apache.xml.internal.security.utils.Base64 但出现错误。 方法#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

我还尝试使用java.util.Base64的wrap方法创建一个InputStream来解码Base64编码的字节流,但数据变得更加困惑。

最佳答案

GTFS 实时规范现在包含用于解析各种语言的 GTFS 实时数据的代码示例:

https://developers.google.com/transit/gtfs-realtime/code-samples

当需要用您最喜欢的语言解析 GTFS 实时数据时,这是一个很好的起点。

关于base64 - 将 gtfs 实时数据流式传输为人类可读的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27749975/

相关文章:

ssl - 密码学中有哪些不同的证书类型和格式

javascript - Node.js 中的安全随机 token

Java文件加密,无法让它接受文件输入

java - 如何使用 UTF-8 读取 InputStream?

python - 查找自定义 protobuf 插件时出错

c# - Protobuf-Net NotSupportedException : Type cannot be represented as a default value for closed immutable type (UnityEngine. 矢量3)

python - 在 Python 3.4 中使用 Odoo API 导入图像

python - Gzip 到 base64 编码将字符添加到 JSON 字符串

android - 如何将来自 Okhttp 的响应转换为输入流

python - 查找 ProtoBuf 的 Python DESCRIPTOR 中列出的枚举