- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
伙计们,我一直在尝试编写一个使用 TCP 传输 JSON 字符串的程序。问题是,当我调用 BufferedInputStream.read()
时,我无法收到任何 -1
来指示 JSON 字符串的结尾。
我的服务器代码是:
public void listen() {
try {
if (getClient() != null) {
String jst = null;
Message msg = null;
InputStream in = getClient().getInputStream();
BufferedInputStream inBuf = new BufferedInputStream(in);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (getClient() != null) {
System.out.println("Receiving Data...");
try {
int i;
while ((i = inBuf.read()) != -1) {
baos.write(i);
}
jst = baos.toString();
} catch (IOException e) {
System.err.println("Bad Connection.");
stopStream();
break;
}
System.out.println(jst);
msg = MessageFactory.fromJSON(jst);
if (msg.getType().equals(StartMessage.defaultType)) {
startStream((StartMessage) msg);
} else if (msg.getType().equals(StreamMessage.defaultType)) {
streaming((StreamMessage) msg);
} else if (msg.getType().equals(
StopStreamMessage.defaultType)) {
stopStream();
}
}
}
} catch (IOException e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
}
我的客户端代码是:
public void sendStart(OutputStream out) throws IOException {
String jst = makeStart();
out.write(jst.getBytes());
System.out.println("data sent: " + jst);
}
当我调试它时,我看到 baos.write(i);
写入了 i
的数据。然而,数据中似乎没有 -1
所以我只能看到 jst = baos.toString();
我终止客户端或等待时间出。
这是我得到的:
{"format":"raw","width":320,"type":"startstream","height":240}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}{"data":"[B@41635617","type":"image"}
Unexpected token LEFT BRACE({) at position 62.
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at au.edu.benjamin.message.MessageFactory.fromJSON(MessageFactory.java:14)
at au.edu.benjamin.transmission.TransListenerThread.listen(TransListenerThread.java:78)
at au.edu.benjamin.transmission.TransListenerThread.run(TransListenerThread.java:53)
at java.lang.Thread.run(Thread.java:745)
null
Exception in thread "Thread-2" java.lang.NullPointerException
at au.edu.benjamin.transmission.TransListenerThread.listen(TransListenerThread.java:79)
at au.edu.benjamin.transmission.TransListenerThread.run(TransListenerThread.java:53)
at java.lang.Thread.run(Thread.java:745)
异常是因为意外的'{',它应该与以前的JSON字符串分开但没有。
谁能告诉我问题出在哪里?非常感谢!
最佳答案
一旦消息结束,对 TCP 流的 ImputStream.read() 调用不会返回 -1,而是等待新字节(事实上,这样的流仅在关闭时返回 -1)。
为了实现您想要的功能,如果您不在消息的其他地方使用它,您可以在末尾使用字符串终止字符 (\0),或者您可以在之前发送 JSON 消息的大小您自己发送并以该尺寸阅读。
关于java - 为什么我的 BufferedInputStream.read() 不能接收 -1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26516045/
1. 概述 在这个例子中,我们将使用一个BufferedInputStream类来读取一个文件。BufferedInputStream类是用来从流中读取信息的。它在内部使用了一个缓冲机制来使性能快速提
如果文件大小 > 8k,为什么读取的最后一个字节 = 0? private static final int GAP_SIZE = 8 * 1024; public static void main(
使用缓冲输入流读取批量文件时,如何确定缓冲区的大小?它基于文件大小吗?我正在使用, byte[] buf = new byte[4096]; 如果我增加缓冲区大小,它会读取得很快吗? 最佳答案 默认值
我正在尝试用java编写服务器端和客户端。因此,客户端发送请求,如 GET/HTTP/1.0,服务器端响应(如果文件存在),如 HTTP/1.0 200 OK,放入 header 内容类型和内容长度,
我的文件写入过程如下(我称之为非集群模式) Write an object to the current position of the file. Note the position of writ
我的 BufferedInputStream 没有正确标记。这是我的代码: public static void main(String[] args) throws Exception {
对于我的 jvm,BufferedinputStream 默认缓冲区大小是 8k。这是硬编码值,还是可以通过更改某些系统参数来更改? 我希望它在不修改 java 代码的情况下使用 128k。这可能吗?
我正在使用 BufferedInputStream 从套接字中读取数据。 BufferedInputStream 内容如下: socketInput.read(replyBuffer, 0, 7);
我知道将 BufferedInpurStream 包装在 FileInputStream 周围可以使读取操作更快但尝试 弄清楚如何。我查看了 BufferedInpurStream 的源代码并得到了一
让我在这篇文章的开头谨慎一点。当谈到 Java 时,我是一个完全的初学者。我一直在断断续续地编写 PHP,但我已经准备好做一个桌面应用程序,所以出于各种原因我决定使用 Java。 我正在处理的应用程序
这个问题在这里已经有了答案: 关闭11年前。 Possible Duplicate: In Java how do a read/convert an InputStream in to a stri
BufferedInputStream 介绍 BufferedInputStream 是缓冲输入流。它继承于FilterInputStream。 BufferedInputStream 的作用是为
是 available()在 Java 中的套接字编程中使用可靠吗? 我只关心它告诉我什么时候有可用的字节读取,这样当我调用读取方法时它们不会阻塞。 BufferedInputStream.avail
这是我的代码。 while ((count = in.read(data, 0, 16384)) != -1) // while there is still data from link to be
我通过 javax.sound.sampled.AudioSystem.getAudioInputStream() 从 URLConnection 获取 AudioInputStream。当将 URL
我需要使 java BufferedInputStream 可序列化。是否有任何替代方案或任何其他方式来实现它? 您是否发现此实现中存在任何问题 import java.io.BufferedInpu
我有 Java SSL/TLS 服务器和客户端套接字。我的客户端只需将文件发送到服务器,服务器就会接收它。这是我的代码: 我的客户端方法: static boolean writeData(Buffe
我正在从 Oracle 数据库读取 BLOB 列,然后将其写入文件,如下所示: public static int execute(String filename, BLOB blob)
有人可以向我解释为什么这在 in.available()>0 注释掉的情况下工作得很好,但是当我把它放回去时它就崩溃了? mySocket = new Socket("blahblah", 12345
有一个常见的模式,当应用程序的每一层处理来自流的数据时,倾向于将其包装到 BufferedInputStream 中,因此总的来说,有很多缓冲区,从缓冲区填充,从缓冲区填充等等。 我认为这是不好的做法
我是一名优秀的程序员,十分优秀!