I'm trying to transfer large data through 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();
Presume that local file size is 1GB and ByteBuffer is 1MB.
If i put all local file data to Channel, MY JVM Heap will be alright?
If do, Where data in channels stores?
If don't, I have to transfer on each buffer-sized chunk?