package apdu4j;

import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class ATRList {
    private final Map<String, String> map;


    public static ATRList from(InputStream in) throws IOException {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {

            Map<String, String> entries = new HashMap<>();

            List<String> lines = reader.lines().collect(Collectors.toList());
            String name = null;
            String desc = null;
            for (String line : lines) {

                if (line.length() == 0) {
                    if (name != null && desc != null) {
                        entries.put(name.replace(" ", ""), desc.trim().replace('\t', '\n'));
                    }
                    desc = "";
                } else if (line.charAt(0) == '\t') {
                    desc += line;
                } else if (line.charAt(0) == '#') {
                    continue;
                } else {
                    name = line;
                }
            }

            System.out.println("Parsed " + entries.size() + " ATR-s");
            return new ATRList(entries);
        }
    }

    public static ATRList from(String path) throws IOException {
        final InputStream in;
        if (path.startsWith("http")) {
            in = new URL(path).openStream();
        } else {
            in = new FileInputStream(path);
        }
        return from(in);
    }

    private ATRList(Map<String, String> map) {
        this.map = map;
    }

    public Optional<Map.Entry<String, String>> match(byte[] atr) {
        String q = HexUtils.bin2hex(atr).toUpperCase();
        return map.entrySet().stream().filter(e -> q.matches(e.getKey())).findFirst();
    }

    // Does the same as https://github.com/LudovicRousseau/pcsc-tools/blob/master/ATR_analysis#L58
    public static Optional<String> locate() {
        String home = System.getProperty("user.home", "");
        Path[] paths = new Path[]{
                Paths.get(home, ".cache", ".smartcard_list.txt"),
                Paths.get(home, ".smartcard_list.txt"),
                Paths.get("/usr/local/pcsc/smartcard_list.txt"),
                Paths.get("/usr/share/pcsc/smartcard_list.txt"),
                Paths.get("/usr/local/share/pcsc/smartcard_list.txt")};

        for (Path p : paths) {
            if (Files.exists(p))
                return Optional.of(p.toString());
        }

        return Optional.empty();
    }
}

@@ -1,8 +1,1131 @@
+/*%0A * Copyright (c) 2014-2019 Martin Paljak%0A *%0A * Permission is hereby granted, free of charge, to any person obtaining a copy%0A * of this software and associated documentation files (the %22Software%22), to deal%0A * in the Software without restriction, including without limitation the rights%0A * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell%0A * copies of the Software, and to permit persons to whom the Software is%0A * furnished to do so, subject to the following conditions:%0A *%0A * The above copyright notice and this permission notice shall be included in%0A * all copies or substantial portions of the Software.%0A *%0A * THE SOFTWARE IS PROVIDED %22AS IS%22, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR%0A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,%0A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE%0A * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER%0A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,%0A * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN%0A * THE SOFTWARE.%0A */%0A
 package 
@@ -2428,80 +2428,8 @@
    %7D
-%0A%0A            System.out.println(%22Parsed %22 + entries.size() + %22 ATR-s%22);
 %0A   

Remove printf and add license