Datasets:

ArXiv:
License:
File size: 1,052 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package httpcli.adapter;

import httpcli.ResponseBody;
import httpcli.io.IOUtils;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class RespBodyFile implements RespBodyAdapter<File> {

  private final String localPath;

  public RespBodyFile(String localPath) {
    this.localPath = localPath;
  }
  
  public RespBodyFile() {
    this(null);
  }

  public String localPath() {
    return localPath;
  }
  
  public File localFile() throws IOException {
    String path = localPath();
    if (path == null || path.isEmpty()) {
      return File.createTempFile("download", ".temp");
    }
    return new File(path);
  }
  
  @Override
  public File parse(ResponseBody respBody) throws Exception {
    BufferedOutputStream bos = null;
    try {
      File file = localFile();
      bos = new BufferedOutputStream(new FileOutputStream(file));
      respBody.writeTo(bos);
      return file;
    } finally {
      IOUtils.closeQuietly(bos);
      respBody.close();
    }
  }
}