Java NIO通道-数据流向何处?

时间:2018-11-19 13:28:35

标签: java buffer nio

我正在尝试通过HttpUrlConnection传输大数​​据。

FileInputStream inputStream = new FileInputStream(localFile);
ReadableByteChannel inputChannel = Channels.newChannel(inputStream);
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);

connection = (HttpURLConnection)(new URL("somewhere").openConnection());

connection.addRequestProperty("Range", "bytes=" + 0 + "-" + (localFile.length() - 1));
connection.setRequestMethod(HttpMethod.PUT.name());
connection.setDoOutput(true);

connection.connect();

WritableByteChannel outputChannel = Channels.newChannel(connection.getOutputStream());
while (inputChannel.read(buffer) > -1) {
    buffer.flip();
    int totalWritten = outputChannel.write(buffer);
    buffer.compact();
}

log.info("total written : {}, {}", accumulate
            , connection.getResponseCode());

connection.disconnect();

假定本地文件大小为1GB,字节缓冲区为1MB。

如果我将所有本地文件数据放到Channel中,我的JVM堆会没事吗?

如果可以,通道中的数据存储在哪里?

如果没有,我必须在每个缓冲区大小的块上进行传输吗?

0 个答案:

没有答案