View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */
19  
20  package org.apache.rat.document.impl;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.InputStream;
24  import java.io.InputStreamReader;
25  import java.io.Reader;
26  import java.nio.charset.StandardCharsets;
27  import java.util.Collections;
28  import java.util.SortedSet;
29  
30  import org.apache.rat.api.Document;
31  
32  /**
33   * A Document that wraps an Archive entry.
34   */
35  public class ArchiveEntryDocument extends Document {
36  
37      /** The contents of the entry */
38      private final byte[] contents;
39  
40      /**
41       * Creates an Archive entry.
42       * @param outerName the name of this entry from outside the archive.
43       * @param contents the contents of the entry.
44       * @param nameMatcher the name matcher to filter contents with.
45       */
46      public ArchiveEntryDocument(final DocumentName outerName, final byte[] contents, final DocumentNameMatcher nameMatcher) {
47          super(outerName, nameMatcher);
48          this.contents = contents;
49      }
50  
51      @Override
52      public InputStream inputStream() {
53          return new ByteArrayInputStream(contents);
54      }
55  
56      @Override
57      public boolean isDirectory() {
58          return false;
59      }
60  
61      @Override
62      public SortedSet<Document> listChildren() {
63          return Collections.emptySortedSet();
64      }
65  
66      @Override
67      public Reader reader() {
68          return new InputStreamReader(new ByteArrayInputStream(contents), StandardCharsets.UTF_8);
69      }
70  }