Copies data from the input channel to the output file channel. @param input the input channel to copy. @param output the output channel to copy. @throws IOException if there is an I/O error.
(@NonNull ReadableByteChannel input, @NonNull FileChannel output)
| 45 | * @throws IOException if there is an I/O error. |
| 46 | */ |
| 47 | @SuppressLint("LambdaLast") |
| 48 | public static void copy(@NonNull ReadableByteChannel input, @NonNull FileChannel output) |
| 49 | throws IOException { |
| 50 | try { |
| 51 | if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { |
| 52 | output.transferFrom(input, 0, Long.MAX_VALUE); |
| 53 | } else { |
| 54 | InputStream inputStream = Channels.newInputStream(input); |
| 55 | OutputStream outputStream = Channels.newOutputStream(output); |
| 56 | int length; |
| 57 | byte[] buffer = new byte[1024 * 4]; |
| 58 | while ((length = inputStream.read(buffer)) > 0) { |
| 59 | outputStream.write(buffer, 0, length); |
| 60 | } |
| 61 | } |
| 62 | output.force(false); |
| 63 | } finally { |
| 64 | input.close(); |
| 65 | output.close(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | private FileUtil() { |
| 70 | } |
no test coverage detected