Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Created on 7 Jun 2007
 * Created by Allan Crooks
 * Copyright (C) 2007 Aelitis, All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * AELITIS, SAS au capital de 46,603.30 euros
 * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
 */

public class Main {
    /**
     * Checking whether a peer ID is Shadow style or not is a bit tricky.
     * 
     * The BitTornado peer ID convention code is explained here:
     *   http://forums.degreez.net/viewtopic.php?t=7070
     *   
     * The main thing we are interested in is the first six characters.
     * Although the other characters are base64 characters, there's no
     * guarantee that other clients which follow that style will follow
     * that convention (though the fact that some of these clients use
     * BitTornado in the core does blur the lines a bit between what is
     * "style" and what is just common across clients).
     * 
     * So if we base it on the version number information, there's another
     * problem - there isn't the use of absolute delimiters (no fixed dash
     * character, for example).
     * 
     * There are various things we can do to determine how likely the peer
     * ID is to be of that style, but for now, I'll keep it to a relatively
     * simple check.
     * 
     * We'll assume that no client uses the fifth version digit, so we'll
     * expect a dash. We'll also assume that no client has reached version 10
     * yet, so we expect the first two characters to be "letter,digit".
     * 
     * We've seen some clients which don't appear to contain any version
     * information, so we need to allow for that.
     */
    public static boolean isShadowStyle(String peer_id) {
        if (peer_id.charAt(5) != '-') {
            return false;
        }
        if (!Character.isLetter(peer_id.charAt(0))) {
            return false;
        }
        if (!(Character.isDigit(peer_id.charAt(1)) || peer_id.charAt(1) == '-')) {
            return false;
        }

        // Find where the version number string ends.
        int last_ver_num_index = 4;
        for (; last_ver_num_index > 0; last_ver_num_index--) {
            if (peer_id.charAt(last_ver_num_index) != '-') {
                break;
            }
        }

        // For each digit in the version string, check it is a valid version identifier.
        char c;
        for (int i = 1; i <= last_ver_num_index; i++) {
            c = peer_id.charAt(i);
            if (c == '-') {
                return false;
            }
            if (decodeAlphaNumericChar(c) == null) {
                return false;
            }
        }

        return true;
    }

    public static String decodeAlphaNumericChar(char c) {
        if ('0' <= c && c <= '9') {
            return String.valueOf(c);
        } else if ('A' <= c && c <= 'Z') {
            return String.valueOf(10 + (c - 'A'));
        } else if ('a' <= c && c <= 'z') {
            return String.valueOf(36 + (c - 'A'));
        } else if (c == '.') {
            return "62";
        }
        return null;
    }
}