Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.InputStream;
import java.io.FileInputStream;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.util.TreeMap;

public class Main {
    private static TreeMap<String, String> pathMap;

    public static InputStream U7openStream2(String nm1, String nm2) {
        String nm = U7exists(nm1);
        if (nm != null)
            try {
                return new BufferedInputStream(new FileInputStream(nm), 0x8000);
            } catch (IOException e) {
            }
        nm = U7exists(nm2);
        if (nm != null)
            try {
                return new BufferedInputStream(new FileInputStream(nm), 0x8000);
            } catch (IOException e) {
            }
        return null;
    }

    public static final String U7exists(String nm) {
        String name = getSystemPath(nm);
        int uppercasecount = 0;
        do {
            if (new File(name).exists())
                return name; // found it!
        } while ((name = baseToUppercase(name, ++uppercasecount)) != null);
        return null;
    }

    public static String getSystemPath(String path) {
        String newPath;
        int pos, pos2;
        pos = path.indexOf('>');
        pos2 = path.indexOf('<');
        // If there is no separator, return the path as is
        if (pos == -1 || pos2 != 0) {
            newPath = path;
        } else {
            pos += 1;
            // See if we can translate this prefix
            String syspath = path.substring(0, pos);
            if (isSystemPathDefined(syspath)) {
                String newPrefix = pathMap.get(syspath);
                newPath = newPrefix + path.substring(pos);
            } else {
                newPath = path;
            }
        }
        return newPath;
    }

    private static String baseToUppercase(String str, int count) {
        if (count <= 0)
            return str;
        int todo = count, i;
        // Go backwards.
        for (i = str.length() - 1; i >= 0; --i) {
            int c = str.charAt(i);
            if (c == '/') {
                todo--;
                if (todo <= 0)
                    break;
            }
        }
        if (todo > 0)
            return null; // Didn't reach 'count' parts.
        String res = str.substring(0, i) + str.substring(i, str.length()).toUpperCase();
        return res;
    }

    public static boolean isSystemPathDefined(String path) {
        return pathMap != null && pathMap.containsKey(path);
    }
}