如何有效地优化将.osgb文件流式传输到ByteString?

时间:2016-04-11 17:54:39

标签: java protocol-buffers

我在Google Protobuf消息中发送.osgb文件,该消息需要字节字符串。它编码为" ISO-5589-1"。在python中,我可以简单地open(file_name, "r").read()。在Java中,我创建了noob-ishly:

    String model;

    ByteString modelBytes = null;

    try {
        FileInputStream fis = new FileInputStream( filename );
        DataInputStream dis = new DataInputStream(fis);

        byte[] bytes = new byte[dis.available()];

        if ( dis.available() != 0 ) {
            dis.readFully(bytes);
        }
        model = new String(bytes, "ISO-8859-1"); 

        modelBytes = ByteString.copyFrom(model, "ISO-8859-1");
    }

我不打算对这段摘录进行编码高尔夫,但我觉得我可能有多余的或额外的代码,而这些代码确实是不需要的。感觉好像我应该能够立即将数据流转换为ByteString并且不用担心编码,但我对它不够熟悉。

我对Java非常缺乏经验,所以我感谢任何帮助。感谢。

0 个答案:

没有答案
相关问题