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  package org.apache.commons.compress.archivers.memory;
20  
21  import java.io.IOException;
22  
23  import org.apache.commons.compress.archivers.ArchiveEntry;
24  import org.apache.commons.compress.archivers.ArchiveInputStream;
25  
26  public final class MemoryArchiveInputStream extends ArchiveInputStream {
27  
28      private final String[] filenames;
29      private final String[] content;
30      private int p;
31  
32      public MemoryArchiveInputStream( final String[][] pFiles ) {
33          filenames = new String[pFiles.length];
34          content = new String[pFiles.length];
35  
36          for (int i = 0; i < pFiles.length; i++) {
37              String[] nameAndContent = pFiles[i];
38              filenames[i] = nameAndContent[0];
39              content[i] = nameAndContent[1];
40          }
41          p = 0;
42      }
43  
44      @Override
45      public ArchiveEntry getNextEntry() throws IOException {
46          if (p >= filenames.length) {
47              return null;
48          }
49  
50          return new MemoryArchiveEntry(filenames[p]);
51      }
52  
53      public String readString() {
54          return content[p++];
55      }
56  
57      @Override
58      public int read() throws IOException {
59          return 0;
60      }
61  
62  }