org.springframework.util.StreamUtil Maven / Gradle / Ivy
package org.springframework.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
public class StreamUtil {
public static long stream(InputStream inputStream, OutputStream outputStream) throws IOException {
try (ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream); WritableByteChannel writableByteChannel = Channels.newChannel(outputStream);) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(10240);
long size = 0;
while (readableByteChannel.read(byteBuffer) != -1) {
byteBuffer.flip();
size += writableByteChannel.write(byteBuffer);
byteBuffer.clear();
}
return size;
}
}
}