1/*
2 * Copyright (C) 2013 The Android Open Source Project
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 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.multidex;
18
19import java.io.File;
20import java.io.FileInputStream;
21import java.io.FileNotFoundException;
22import java.io.InputStream;
23import java.util.ArrayList;
24
25/**
26 * A folder element.
27 */
28class FolderPathElement implements ClassPathElement {
29
30    private File baseFolder;
31
32    public FolderPathElement(File baseFolder) {
33        this.baseFolder = baseFolder;
34    }
35
36    @Override
37    public InputStream open(String path) throws FileNotFoundException {
38        return new FileInputStream(new File(baseFolder,
39                path.replace(SEPARATOR_CHAR, File.separatorChar)));
40    }
41
42    @Override
43    public void close() {
44    }
45
46    @Override
47    public Iterable<String> list() {
48        ArrayList<String> result = new ArrayList<String>();
49        collect(baseFolder, "", result);
50        return result;
51    }
52
53    private void collect(File folder, String prefix, ArrayList<String> result) {
54        for (File file : folder.listFiles()) {
55            if (file.isDirectory()) {
56                collect(file, prefix + SEPARATOR_CHAR + file.getName(), result);
57            } else {
58                result.add(prefix + SEPARATOR_CHAR + file.getName());
59            }
60        }
61    }
62
63}
64