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.IOException;
24  import java.io.InputStream;
25  import java.io.InputStreamReader;
26  import java.io.Reader;
27  import java.nio.charset.StandardCharsets;
28  import java.nio.file.Path;
29  import java.util.Collections;
30  import java.util.SortedSet;
31  
32  import org.apache.rat.api.Document;
33  
34  /**
35   * A Document that wraps an Archive entry.
36   */
37  public class ArchiveEntryDocument extends Document {
38  
39      /** The contents of the entry */
40      private final byte[] contents;
41  
42      /** The path for the entry */
43      private final Path path;
44  
45      /**
46       * Creates an Archive entry.
47       * @param path The path for the entry.
48       * @param contents the contents of the entry.
49       */
50      public ArchiveEntryDocument(Path path, byte[] contents) {
51          super(FileDocument.normalizeFileName(path.toFile()));
52          this.path = path;
53          this.contents = contents;
54      }
55  
56      @Override
57      public InputStream inputStream() throws IOException {
58          return new ByteArrayInputStream(contents);
59      }
60  
61      @Override
62      public boolean isDirectory() {
63          return false;
64      }
65  
66      @Override
67      public SortedSet<Document> listChildren() {
68          return Collections.emptySortedSet();
69      }
70  
71      @Override
72      public Reader reader() throws IOException {
73          return new InputStreamReader(new ByteArrayInputStream(contents), StandardCharsets.UTF_8);
74      }
75  }