View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit;
16  
17  import java.io.ByteArrayInputStream;
18  import java.io.File;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.Serializable;
22  import java.lang.ref.Cleaner;
23  import java.nio.file.Files;
24  
25  import org.apache.commons.io.FileUtils;
26  import org.htmlunit.util.ArrayUtils;
27  
28  /**
29   * Wrapper for content downloaded from a remote server.
30   *
31   * @author Marc Guillemot
32   * @author Ronald Brill
33   */
34  public interface DownloadedContent extends Serializable {
35  
36      /**
37       * Implementation keeping content in memory.
38       */
39      class InMemory implements DownloadedContent {
40          private final byte[] bytes_;
41  
42          InMemory(final byte[] byteArray) {
43              if (byteArray == null) {
44                  bytes_ = ArrayUtils.EMPTY_BYTE_ARRAY;
45              }
46              else {
47                  bytes_ = byteArray;
48              }
49          }
50  
51          @Override
52          public InputStream getInputStream() {
53              return new ByteArrayInputStream(bytes_);
54          }
55  
56          @Override
57          public void cleanUp() {
58              // nothing to do
59          }
60  
61          @Override
62          public boolean isEmpty() {
63              return length() == 0;
64          }
65  
66          @Override
67          public long length() {
68              return bytes_.length;
69          }
70      }
71  
72      /**
73       * Implementation keeping content on the file system.
74       */
75      class OnFile implements DownloadedContent {
76  
77          private final File file_;
78          private final Cleaner.Cleanable cleanable_;
79  
80          // no synchronization required, a Cleanable runs the action at most once
81          private static final class OnFileCleaningAction implements Runnable {
82              private final File file_;
83  
84              OnFileCleaningAction(final File file) {
85                  file_ = file;
86              }
87  
88              @Override
89              public void run() {
90                  FileUtils.deleteQuietly(file_);
91              }
92          }
93  
94          /**
95           * Ctor.
96           *
97           * @param file the file
98           * @param temporary if true, the file will be deleted when cleanUp() is called.
99           */
100         OnFile(final File file, final boolean temporary) {
101             file_ = file;
102             if (temporary) {
103                 cleanable_ = WebClient.registerCleanerAction(this, new OnFileCleaningAction(file));
104             }
105             else {
106                 cleanable_ = null;
107             }
108         }
109 
110         @Override
111         public InputStream getInputStream() throws IOException {
112             return Files.newInputStream(file_.toPath());
113         }
114 
115         @Override
116         public void cleanUp() {
117             if (cleanable_ != null) {
118                 cleanable_.clean();
119             }
120         }
121 
122         @Override
123         public boolean isEmpty() {
124             return false;
125         }
126 
127         @Override
128         public long length() {
129             if (file_ == null) {
130                 return 0;
131             }
132 
133             return file_.length();
134         }
135     }
136 
137     /**
138      * Returns a new {@link InputStream} allowing to read the downloaded content.
139      * @return the InputStream
140      * @throws IOException in case of problem accessing the content
141      */
142     InputStream getInputStream() throws IOException;
143 
144     /**
145      * Clean up resources associated to this content.
146      */
147     void cleanUp();
148 
149     /**
150      * Returns true if the content is empty.
151      * @return true or false
152      */
153     boolean isEmpty();
154 
155     /**
156      * Returns the number of bytes.
157      * @return the length
158      */
159     long length();
160 }