1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.utils;
19
20 import org.apache.hc.client5.http.impl.classic.AbstractHttpClientResponseHandler;
21 import org.apache.hc.core5.http.HttpEntity;
22
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.nio.ByteBuffer;
28 import java.nio.channels.Channels;
29 import java.nio.channels.FileChannel;
30 import java.nio.channels.ReadableByteChannel;
31
32 class SaveToFileResponseHandler extends AbstractHttpClientResponseHandler<Void> {
33
34
35
36
37 private final File outputPath;
38
39 SaveToFileResponseHandler(File outputPath) {
40 this.outputPath = outputPath;
41 }
42
43 @Override
44 public Void handleEntity(HttpEntity responseEntity) throws IOException {
45 try (InputStream in = responseEntity.getContent();
46 ReadableByteChannel sourceChannel = Channels.newChannel(in);
47 FileOutputStream fos = new FileOutputStream(outputPath);
48 FileChannel destChannel = fos.getChannel()) {
49 final ByteBuffer buffer = ByteBuffer.allocateDirect(8192);
50 while (sourceChannel.read(buffer) != -1) {
51 buffer.flip();
52 destChannel.write(buffer);
53 buffer.compact();
54 }
55 }
56 return null;
57 }
58
59 }