私はこれを実行すると、私は
public class DownloadMain {
public static void main(String[] args) throws IOException {
HttpURLConnection uri = (HttpURLConnection) new URL("http://speedtest.tele2.net/3MB.zip").openConnection();
uri.setRequestMethod("GET");
uri.setConnectTimeout(5000);
InputStream ent = uri.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes = new byte[8192];
long start = System.currentTimeMillis();
for (int len; (len = ent.read(bytes)) > 0;)
baos.write(bytes, 0, len);
long time = System.currentTimeMillis() - start;
System.out.printf("Took %.3f seconds to read %.3f MB of data%n",
time/1e3, baos.toByteArray().length/1e6);
}
}
プリントます場合は約6 MB/sの
ある
Took 0.541 seconds to read 3.146 MB of data
を取得し、バイナリファイル用のバイナリダウンロードを使用します性能比較のためにファイルがバイナリであるという事実を無視してください。
public class DownloadMain {
public static void main(String[] args) throws IOException {
HttpURLConnection uri = (HttpURLConnection) new URL("http://speedtest.tele2.net/3MB.zip").openConnection();
uri.setRequestMethod("GET");
uri.setConnectTimeout(5000);
InputStream ent =uri.getInputStream();
Reader reader = new InputStreamReader(ent, StandardCharsets.ISO_8859_1);
StringWriter sw = new StringWriter();
char[] chars = new char[8192];
long start = System.currentTimeMillis();
for (int len; (len = reader.read(chars)) > 0;)
sw.write(chars, 0, len);
long time = System.currentTimeMillis() - start;
System.out.printf("Took %.3f seconds to read %.3f MB of data%n",
time/1e3, sw.toString().length()/1e6);
}
}
プリント
Took 0.548 seconds to read 3.146 MB of data
だから、それは少し遅くなるか、単にランダムなバリエーションがあります。あなたはBufferedReaderの約100メガバイト/秒でファイルを読み込むことができます劇的
Took 0.555 seconds to read 3.146 MB of data
、ではなく、私は読書についてもっと心配ですそれは動作しませんテキストとしてバイナリファイル。バイナリとして読む必要があります。 –
@PeterLawreyそれは高速テストのためだけでなく、実行するためではないので、それを行うには問題ないと思いますか? –
@Alperözdemir実際には、BufferedReaderは読み込みデータ内の改行を探す作業をしなければなりません。バイナリ転送を最も確実に行うことは、Operaがフードの中で何をしているかをよりよく表しています。さらに、一度に4kまたは8kの読み込みを行うだけで、より多くのデータを簡単に読み込むことができます。 – Gimby