web服务正在提供二进制chunked application/octet-stream
。我必须用java构建一个读取客户端。我从一些文档中了解该结构,并希望将该流用作ObjectInputStream,但在我调用任何自制的readObject方法之前,由于java.io.StreamCorruptedException: invalid stream header
,这种方法失败了。
从这样一个字节流填充java对象的最佳方法是什么?
顺便说一句:数据以LittleEndian的形式出现,16位字符的文本被4字节大小的信息所取代。
03 00 00 00 41 00 62 00 63 00 (六角)
会导致"Abc“
发布于 2016-08-30 07:08:41
有时候,从头开始(或者使用您已经知道的工具)比寻找更复杂的解决方案更快。我只是创建了自己的专用DataInputStream:
import java.io.IOException;
import java.io.InputStream;
public class LittleEndianInputStream {
private InputStream s;
public LittleEndianInputStream (InputStream in) { s = in; }
boolean readBoolean() throws IOException {return (s.read() == 1);}
int readInt() throws IOException {return s.read() | s.read() << 8 | s.read() << 16 | s.read() << 24; }
char readChar() throws IOException {return (char)(s.read() | s.read() << 8); }
String readString() throws IOException {
int len = readInt();
char [] tarray = new char[len];
for (int i = 0; i < len; i++ ) tarray[i] = readChar();
return new String(tarray);
}
}
为了根据需要扩展更多的数据类型..。
(仍欢迎其他解决办法;)
https://stackoverflow.com/questions/39228745
复制相似问题