Java:BufferedReaderを読み込み以上かかってしまいます。
-
11-09-2019 - |
質問
ったほうがいいと思うよJavaプログラムとソケットいで送信コマンドのクライアントとクライアントからサーバーです。のコマンドを使用してい BufferedReader
.を書いて、 PrintWriter
今回はこれを転送したいファイルを通して その ソケット (単に作成第二の接続).
第たoutputstreamに書き込むバイト数のファイルが入っています。●例えば40000バイトまでとなります。かに当てはまる番号を書く 40000
を通じて、ソケットがその他の接続を読み込み 78
.
そ:の BufferedReader
を読み込み以外にもライン 呼び出さ readLine()
) れることが、その一部のバイトからのファイルデータです。このバッファからの BufferedReader
.
その番号 78
はバイトのファイルにしたいを伝達する
この考え方、又はいません。の答えを見出さなければならないためsovleこの問題です。
思いを説明します。
こちらは自分のコードが、私のデフォルトの言語はオランダ語一部変数の名サウンドを得ることができ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のキャッシュを待っている).
A BufferedReaderは、この一からの読み出しを配下の入力ストリームです。
この目的での回数を最小限に抑えるために読み込みから基礎ストリームを高くしていを委譲も深く).そのために、バッファで読み込み でできるだけ多くのバイト数 では単一の呼び出し元のストリームです。
であり、診断ができるようになりました。
ただの野生刺しがここに-40000は1001110001000000バイナリ.現在のビットは1001110は78.意味を、2バイト書き込みの情報が読みセブンビット.