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 package org.apache.hc.client5.http.impl.cache;
28
29 import java.io.File;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.RandomAccessFile;
34 import java.nio.channels.FileChannel;
35
36 import org.apache.hc.client5.http.cache.Resource;
37 import org.apache.hc.client5.http.cache.ResourceFactory;
38 import org.apache.hc.client5.http.cache.ResourceIOException;
39 import org.apache.hc.core5.annotation.Contract;
40 import org.apache.hc.core5.annotation.ThreadingBehavior;
41 import org.apache.hc.core5.util.Args;
42
43
44
45
46
47
48 @Contract(threading = ThreadingBehavior.STATELESS)
49 public class FileResourceFactory implements ResourceFactory {
50
51 private final File cacheDir;
52 private final BasicIdGenerator idgen;
53
54 public FileResourceFactory(final File cacheDir) {
55 super();
56 this.cacheDir = cacheDir;
57 this.idgen = new BasicIdGenerator();
58 }
59
60 private File generateUniqueCacheFile(final String requestId) {
61 final StringBuilder buffer = new StringBuilder();
62 this.idgen.generate(buffer);
63 buffer.append('.');
64 final int len = Math.min(requestId.length(), 100);
65 for (int i = 0; i < len; i++) {
66 final char ch = requestId.charAt(i);
67 if (Character.isLetterOrDigit(ch) || ch == '.') {
68 buffer.append(ch);
69 } else {
70 buffer.append('-');
71 }
72 }
73 return new File(this.cacheDir, buffer.toString());
74 }
75
76 @Override
77 public Resource generate(
78 final String requestId,
79 final byte[] content, final int off, final int len) throws ResourceIOException {
80 Args.notNull(requestId, "Request id");
81 final File file = generateUniqueCacheFile(requestId);
82 try (FileOutputStream outStream = new FileOutputStream(file)) {
83 if (content != null) {
84 outStream.write(content, off, len);
85 }
86 } catch (final IOException ex) {
87 throw new ResourceIOException(ex.getMessage(), ex);
88 }
89 return new FileResource(file);
90 }
91
92 @Override
93 public Resource generate(final String requestId, final byte[] content) throws ResourceIOException {
94 Args.notNull(content, "Content");
95 return generate(requestId, content, 0, content.length);
96 }
97
98 @Override
99 public Resource copy(
100 final String requestId,
101 final Resource resource) throws ResourceIOException {
102 final File file = generateUniqueCacheFile(requestId);
103 try {
104 if (resource instanceof FileResource) {
105 try (final RandomAccessFile srcFile = new RandomAccessFile(((FileResource) resource).getFile(), "r");
106 final RandomAccessFile dstFile = new RandomAccessFile(file, "rw");
107 final FileChannel src = srcFile.getChannel();
108 final FileChannel dst = dstFile.getChannel()) {
109 src.transferTo(0, srcFile.length(), dst);
110 }
111 } else {
112 try (final FileOutputStream out = new FileOutputStream(file);
113 final InputStream in = resource.getInputStream()) {
114 final byte[] buf = new byte[2048];
115 int len;
116 while ((len = in.read(buf)) != -1) {
117 out.write(buf, 0, len);
118 }
119 }
120 }
121 } catch (final IOException ex) {
122 throw new ResourceIOException(ex.getMessage(), ex);
123 }
124 return new FileResource(file);
125 }
126
127 }