【发布时间】:2015-02-18 13:45:13
【问题描述】:
我正在尝试使用 BufferedInputStream 中的 BufferedReader 从套接字流中读取第一行,它读取第一行 (1),这是此内容中某些内容的大小 (2) 我有另一个内容的大小 (3)
- 读取正确...
( with BufferedReader, _bin.readLine() ) - 也能正确读取...
( with _in.read(byte[] b) ) - 不会阅读,似乎内容比我在 (2) 中阅读的大小要多
我认为问题是我尝试使用BufferedReader 阅读,然后使用BufferedInputStream... 谁能帮助我?
public HashMap<String, byte[]> readHead() throws IOException {
JSONObject json;
try {
HashMap<String, byte[]> map = new HashMap<>();
System.out.println("reading header");
int headersize = Integer.parseInt(_bin.readLine());
byte[] parsable = new byte[headersize];
_in.read(parsable);
json = new JSONObject(new String(parsable));
map.put("id", lTob(json.getLong(SagConstants.KEY_ID)));
map.put("length", iTob(json.getInt(SagConstants.KEY_SIZE)));
map.put("type", new byte[]{(byte)json.getInt(SagConstants.KEY_TYPE)});
return map;
} catch(SocketException | JSONException e) {
_exception = e.getMessage();
_error_code = SagConstants.ERROR_OCCOURED_EXCEPTION;
return null;
}
}
抱歉英语不好,解释不好,我试图解释我的问题,希望你能理解
文件格式是这样的:
size1
{json, length is given size1, there is size2 given}
{second json, length is size2}
_in 是 BufferedInputStream();
_bin 是 BufferedReader(_in);
使用 _bin,我读取第一行 (size1) 并转换为整数
使用 _in,我读取下一个数据,其中 size2 和此数据的长度是 size1
然后我试图读取最后一个数据,它的大小是 size2
像这样:
byte[] b = new byte[secondSize];
_in.read(b);
这里什么也没有发生,程序暂停...
【问题讨论】:
-
@George您能通过输入文件示例解释您的情况吗?就像你有一个输入文件,因为它对于 Java 套接字、文件或其他源是相同的,你想要什么样的输出?如果你这样解释,我们会更清楚。
-
请使用此信息编辑您的问题,从 cmets 很难读取。
-
我已经编辑了问题
标签: java sockets bufferedreader bufferedinputstream