1

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?

Juno Kim
  • 11
  • 1
  • Well, the data can either remain on disc (i.e. you transfer the data in chunks) or in memory. Since you'd transfer the data from the file to some target in chunks anyways (not necessarily buffer sized) there's no need to read it all into memory first. – Thomas Nov 19 '18 at 13:31
  • Buffer inside PosterOutputStream stores all data i wrote. I think it will be problem if i trying to transfer huge file. – Juno Kim Nov 19 '18 at 13:41
  • https://stackoverflow.com/questions/2082057/outputstream-outofmemoryerror-when-sending-http helps me – Juno Kim Nov 19 '18 at 13:45

0 Answers0