Java的:BufferedReader中读取不是一条线吗?
-
11-09-2019 - |
题
我正在在Java中使用套接字的程序。我可以将命令发送到客户端,并从客户机到服务器。要读我使用BufferedReader
的命令。写他们,一个PrintWriter
但现在我想通过传输文件的是插座的(不仅仅是创建第二个连接)的。照片首先,我写的OutputStream如何许多字节的文件中包含。例如40000个字节。因此,我写穿过插座数量40000
,但连接的另一侧读取78
。
所以,我在想:该BufferedReader
读取不仅仅是以上的线路(通过调用readLine()
)的 的和这样,我失去了从文件数据的一些字节。因为他们是从BufferedReader
缓冲区。结果
所以数量78
是我想要传送的文件的字节。
搜索结果
在思考的权利,或不是这样。如果是这样,如何来解答这个问题。结果 我希望我已经很好地解释。
下面是我的代码,但我的默认语言是荷兰语。因此,一些变量名可以听起来STANGE。
public void flushStreamToStream(InputStream is, OutputStream os, boolean closeIn, boolean closeOut) throws IOException {
byte[] buffer = new byte[BUFFERSIZE];
int bytesRead;
if ((!closeOut) && closeIn) { // To Socket from File
action = "Upload";
os.write(is.available()); // Here I write 400000
max = is.available();
System.out.println("Bytes to send: " + max);
while ((bytesRead = is.read(buffer)) != -1) {
startTiming(); // Two lines to compute the speed
os.write(buffer, 0, bytesRead);
stopTiming(); // Speed compution
process += bytesRead;
}
os.flush();
is.close();
return;
}
if ((!closeIn) && closeOut) { // To File from Socket
action = "Download";
int bytesToRead = -1;
bytesToRead = is.read(); // Here he reads 78.
System.out.println("Bytes to read: " + bytesToRead);
max = bytesToRead;
int nextBufferSize;
while ((nextBufferSize = Math.min(BUFFERSIZE, bytesToRead)) > 0) {
startTiming();
bytesRead = is.read(buffer, 0, nextBufferSize);
bytesToRead -= bytesRead;
process += nextBufferSize;
os.write(buffer, 0, bytesRead);
stopTiming();
}
os.flush();
os.close();
return;
}
throw new IllegalArgumentException("The only two boolean combinations are: closeOut == false && closeIn == true AND closeOut == true && closeIn == false");
}
<强>以下是在溶液强>结果 感谢詹姆斯建议的点击 的我认为 laginimaineb anwser是一块的溶液。强>
读取命令。
DataInputStream in = new DataInputStream(is); // Originally a BufferedReader
// Read the request line
String str;
while ((str = in.readLine()) != null) {
if (str.trim().equals("")) {
continue;
}
handleSocketInput(str);
}
现在的flushStreamToStream:
public void flushStreamToStream(InputStream is, OutputStream os, boolean closeIn, boolean closeOut) throws IOException {
byte[] buffer = new byte[BUFFERSIZE];
int bytesRead;
if ((!closeOut) && closeIn) { // To Socket from File
action = "Upload";
DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(is.available());
max = is.available();
System.out.println("Bytes to send: " + max);
while ((bytesRead = is.read(buffer)) != -1) {
startTiming();
dos.write(buffer, 0, bytesRead);
stopTiming();
process += bytesRead;
}
os.flush();
is.close();
return;
}
if ((!closeIn) && closeOut) { // To File from Socket
action = "Download";
DataInputStream dis = new DataInputStream(is);
int bytesToRead = dis.readInt();
System.out.println("Bytes to read: " + bytesToRead);
max = bytesToRead;
int nextBufferSize;
while ((nextBufferSize = Math.min(BUFFERSIZE, bytesToRead)) > 0) {
startTiming();
bytesRead = is.read(buffer, 0, nextBufferSize);
bytesToRead -= bytesRead;
process += nextBufferSize;
os.write(buffer, 0, bytesRead);
stopTiming();
}
os.flush();
os.close();
return;
}
throw new IllegalArgumentException("The only two boolean combinations are: closeOut == false && closeIn == true AND closeOut == true && closeIn == false");
}
马亭。
解决方案
DataInputStream类是最有可能要使用什么。此外,因为它通常是无用的不使用可用的()方法。
其他提示
我不知道我已经按照你的解释。
不过,是的 - 你必须在一个BufferedReader多少实际读取没有真正的控制。这种读取器的要点是根据需要来补充其缓冲器,它乐观读取底层资源块。所以,当你第一次调用readLine
,它会看到它的内部缓冲区没有足够的服务youthe请求,将熄灭,从底层源读取然而,许多字节的感觉就像进入了缓冲 ,这通常比你要的只是那么多。一旦缓冲区被填充,它会从缓冲的内容返回您的线路。
因此,一旦你包在一个BufferedReader的输入流,你应该确保仅通过相同的缓冲读取器读取该流。如果你不这样做,你会最终失去的数据(如一些字节将被消耗,现在坐在BufferedReader中的缓存等候服务)。
一个的BufferedReader假定它是唯一一个从底层输入流中读取。
它的目的是最小化从底层流读取的次数(这是昂贵的,因为它们可以委派相当深)。为此目的,它保持一个缓冲器,它填充由读取的字节数尽可能到它在基础流的单个呼叫。
所以,是的,你的诊断是正确的。
这里只是一个野生刺 - 40000的二进制为10011100.01亿。现在,前7位这里有1001110这是78含义,你写2个字节的信息,但读七位。