View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.impl.cache;
28  
29  import java.io.File;
30  import java.io.FileInputStream;
31  import java.io.FileNotFoundException;
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.util.concurrent.atomic.AtomicReference;
35  
36  import org.apache.hc.client5.http.cache.Resource;
37  import org.apache.hc.client5.http.cache.ResourceIOException;
38  import org.apache.hc.core5.annotation.Contract;
39  import org.apache.hc.core5.annotation.ThreadingBehavior;
40  import org.apache.hc.core5.util.Args;
41  import org.apache.hc.core5.util.ByteArrayBuffer;
42  
43  /**
44   * Cache resource backed by a file.
45   *
46   * @since 4.1
47   */
48  @Contract(threading = ThreadingBehavior.SAFE)
49  public class FileResource extends Resource {
50  
51      private static final long serialVersionUID = 4132244415919043397L;
52  
53      private final AtomicReference<File> fileRef;
54      private final long len;
55  
56      public FileResource(final File file) {
57          super();
58          Args.notNull(file, "File");
59          this.fileRef = new AtomicReference<>(file);
60          this.len = file.length();
61      }
62  
63      File getFile() {
64          return this.fileRef.get();
65      }
66  
67      @Override
68      public byte[] get() throws ResourceIOException {
69          final File file = this.fileRef.get();
70          if (file == null) {
71              throw new ResourceIOException("Resource already disposed");
72          }
73          try (final InputStream in = new FileInputStream(file)) {
74              final ByteArrayBuffer buf = new ByteArrayBuffer(1024);
75              final byte[] tmp = new byte[2048];
76              int len;
77              while ((len = in.read(tmp)) != -1) {
78                  buf.append(tmp, 0, len);
79              }
80              return buf.toByteArray();
81          } catch (final IOException ex) {
82              throw new ResourceIOException(ex.getMessage(), ex);
83          }
84      }
85  
86      @Override
87      public InputStream getInputStream() throws ResourceIOException {
88          final File file = this.fileRef.get();
89          if (file != null) {
90              try {
91                  return new FileInputStream(file);
92              } catch (final FileNotFoundException ex) {
93                  throw new ResourceIOException(ex.getMessage(), ex);
94              }
95          }
96          throw new ResourceIOException("Resource already disposed");
97      }
98  
99      @Override
100     public long length() {
101         return len;
102     }
103 
104     @Override
105     public void dispose() {
106         final File file = this.fileRef.getAndSet(null);
107         if (file != null) {
108             file.delete();
109         }
110     }
111 
112 }