PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/components/azureus/src/main/java/org/gudy/azureus2/core3/util/Constants.java

https://bitbucket.org/wrapman/frostwire.desktop.translations.pirate
Java | 453 lines | 281 code | 123 blank | 49 comment | 55 complexity | 2cd5fc0fe5a83ce02af05eea6a793b38 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception
  1. /*
  2. * Created on 16 juin 2003
  3. * Copyright (C) 2003, 2004, 2005, 2006 Aelitis, All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. * AELITIS, SAS au capital de 46,603.30 euros
  18. * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
  19. *
  20. */
  21. package org.gudy.azureus2.core3.util;
  22. import java.io.InputStreamReader;
  23. import java.io.LineNumberReader;
  24. import java.nio.charset.Charset;
  25. import java.util.Locale;
  26. import java.util.StringTokenizer;
  27. /**
  28. *
  29. * @author Olivier
  30. *
  31. */
  32. public class
  33. Constants
  34. {
  35. public static final String EMPTY_STRING = "";
  36. public static final String SF_WEB_SITE = "http://azureus.sourceforge.net/";
  37. public static final String AELITIS_TORRENTS = "http://torrent.vuze.com/torrents/";
  38. public static final String AELITIS_FILES = "http://torrent.vuze.com/files/";
  39. public static final String AZUREUS_WIKI = "http://wiki.vuze.com/w/";
  40. public static final String VERSION_SERVER_V4 = "version.vuze.com";
  41. public static final String VERSION_SERVER_V6 = "version6.vuze.com";
  42. public static final String DHT_SEED_ADDRESS_V4 = "dht.vuze.com";
  43. public static final String DHT_SEED_ADDRESS_V6 = "dht6.vuze.com";
  44. public static final String NAT_TEST_SERVER = "nettest.vuze.com";
  45. public static final String NAT_TEST_SERVER_HTTP = "http://nettest.vuze.com/";
  46. public static final String SPEED_TEST_SERVER = "speedtest.vuze.com";
  47. public static final String PAIRING_URL = "http://pair.vuze.com/pairing";
  48. public static final String[] AZUREUS_DOMAINS = { "azureusplatform.com", "azureus.com", "aelitis.com", "vuze.com" };
  49. public static final String DEFAULT_ENCODING = "UTF8";
  50. public static final String BYTE_ENCODING = "ISO-8859-1";
  51. public static Charset BYTE_CHARSET;
  52. public static Charset DEFAULT_CHARSET;
  53. static{
  54. try{
  55. BYTE_CHARSET = Charset.forName( Constants.BYTE_ENCODING );
  56. DEFAULT_CHARSET = Charset.forName( Constants.DEFAULT_ENCODING );
  57. }catch( Throwable e ){
  58. e.printStackTrace();
  59. }
  60. }
  61. public static final Locale LOCALE_ENGLISH = new Locale("en", "");
  62. public static final String INFINITY_STRING = "\u221E"; // "oo";pa
  63. public static final int CRAPPY_INFINITY_AS_INT = 365*24*3600; // seconds (365days)
  64. public static final long CRAPPY_INFINITE_AS_LONG = 10000*365*24*3600; // seconds (10k years)
  65. public static boolean DOWNLOAD_SOURCES_PRETEND_COMPLETE = false;
  66. // keep the CVS style constant coz version checkers depend on it!
  67. // e.g. 2.0.8.3
  68. // 2.0.8.3_CVS
  69. // 2.0.8.3_Bnn // incremental build
  70. public static String APP_NAME = "Vuze";
  71. public static final String AZUREUS_NAME = "Azureus";
  72. public static final String AZUREUS_VERSION = "4.5.0.4"; //4.5.0.5_CVS
  73. public static final String BUILD_VERSION = "@build.version@"; //Ant replace
  74. public static final String AZUREUS_SUBVER = "";
  75. public static final byte[] VERSION_ID = ("-" + "AZ" + "4504" + "-").getBytes(); //MUST be 8 chars long!
  76. private static final boolean FORCE_NON_CVS = System.getProperty( "az.force.noncvs", "0" ).equals( "1" );
  77. public static final boolean IS_CVS_VERSION = isCVSVersion( AZUREUS_VERSION ) && !FORCE_NON_CVS;
  78. public static final String OSName = System.getProperty("os.name");
  79. public static final boolean isOSX = OSName.toLowerCase().startsWith("mac os");
  80. public static final boolean isLinux = OSName.equalsIgnoreCase("Linux");
  81. public static final boolean isSolaris = OSName.equalsIgnoreCase("SunOS");
  82. public static final boolean isFreeBSD = OSName.equalsIgnoreCase("FreeBSD");
  83. public static final boolean isWindowsXP = OSName.equalsIgnoreCase("Windows XP");
  84. public static final boolean isWindows95 = OSName.equalsIgnoreCase("Windows 95");
  85. public static final boolean isWindows98 = OSName.equalsIgnoreCase("Windows 98");
  86. public static final boolean isWindows2000 = OSName.equalsIgnoreCase("Windows 2000");
  87. public static final boolean isWindowsME = OSName.equalsIgnoreCase("Windows ME");
  88. public static final boolean isWindows9598ME = isWindows95 || isWindows98 || isWindowsME;
  89. public static boolean isSafeMode = false;
  90. public static final boolean isWindows = OSName.toLowerCase().startsWith("windows");
  91. // If it isn't windows or osx, it's most likely an unix flavor
  92. public static final boolean isUnix = !isWindows && !isOSX;
  93. public static final boolean isWindowsVista;
  94. public static final boolean isWindowsVistaSP2OrHigher;
  95. public static final boolean isWindowsVistaOrHigher;
  96. public static final boolean isWindows7OrHigher;
  97. static{
  98. if ( isWindows ){
  99. Float ver = null;
  100. try{
  101. ver = new Float( System.getProperty( "os.version" ));
  102. }catch (Throwable e){
  103. }
  104. boolean vista_sp2_or_higher = false;
  105. if ( ver == null ){
  106. isWindowsVista = false;
  107. isWindowsVistaOrHigher = false;
  108. isWindows7OrHigher = false;
  109. }else{
  110. float f_ver = ver.floatValue();
  111. isWindowsVista = f_ver == 6;
  112. isWindowsVistaOrHigher = f_ver >= 6;
  113. isWindows7OrHigher = f_ver >= 6.1f;
  114. if ( isWindowsVista ){
  115. LineNumberReader lnr = null;
  116. try{
  117. Process p =
  118. Runtime.getRuntime().exec(
  119. new String[]{
  120. "reg",
  121. "query",
  122. "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion",
  123. "/v",
  124. "CSDVersion" });
  125. lnr = new LineNumberReader( new InputStreamReader( p.getInputStream()));
  126. while( true ){
  127. String line = lnr.readLine();
  128. if ( line == null ){
  129. break;
  130. }
  131. if ( line.matches( ".*CSDVersion.*" )){
  132. vista_sp2_or_higher = line.matches( ".*Service Pack [2-9]" );
  133. break;
  134. }
  135. }
  136. }catch( Throwable e ){
  137. }finally{
  138. if ( lnr != null ){
  139. try{
  140. lnr.close();
  141. }catch( Throwable e ){
  142. }
  143. }
  144. }
  145. }
  146. }
  147. isWindowsVistaSP2OrHigher = vista_sp2_or_higher;
  148. }else{
  149. isWindowsVista = false;
  150. isWindowsVistaSP2OrHigher = false;
  151. isWindowsVistaOrHigher = false;
  152. isWindows7OrHigher = false;
  153. }
  154. }
  155. public static final boolean isOSX_10_5_OrHigher;
  156. public static final boolean isOSX_10_6_OrHigher;
  157. static{
  158. if ( isOSX ){
  159. int first_digit = 0;
  160. int second_digit = 0;
  161. try{
  162. String os_version = System.getProperty( "os.version" );
  163. String[] bits = os_version.split( "\\." );
  164. first_digit = Integer.parseInt( bits[0] );
  165. if ( bits.length > 1 ){
  166. second_digit = Integer.parseInt( bits[1] );
  167. }
  168. }catch( Throwable e ){
  169. }
  170. isOSX_10_5_OrHigher = first_digit > 10 || ( first_digit == 10 && second_digit >= 5 );
  171. isOSX_10_6_OrHigher = first_digit > 10 || ( first_digit == 10 && second_digit >= 6 );
  172. }else{
  173. isOSX_10_5_OrHigher = false;
  174. isOSX_10_6_OrHigher = false;
  175. }
  176. }
  177. public static final String JAVA_VERSION = System.getProperty("java.version");
  178. public static final String FILE_WILDCARD = isWindows?"*.*":"*";
  179. /**
  180. * Gets the current version, or if a CVS version, the one on which it is based
  181. * @return
  182. */
  183. public static String
  184. getBaseVersion()
  185. {
  186. return( getBaseVersion( AZUREUS_VERSION ));
  187. }
  188. public static String
  189. getBaseVersion(
  190. String version )
  191. {
  192. int p1 = version.indexOf("_"); // _CVS or _Bnn
  193. if ( p1 == -1 ){
  194. return( version );
  195. }
  196. return( version.substring(0,p1));
  197. }
  198. /**
  199. * is this a formal build or CVS/incremental
  200. * @return
  201. */
  202. public static boolean
  203. isCVSVersion()
  204. {
  205. return IS_CVS_VERSION;
  206. }
  207. public static boolean
  208. isCVSVersion(
  209. String version )
  210. {
  211. return( version.indexOf("_") != -1 );
  212. }
  213. /**
  214. * For CVS builds this returns the incremental build number. For people running their own
  215. * builds this returns -1
  216. * @return
  217. */
  218. public static int
  219. getIncrementalBuild()
  220. {
  221. return( getIncrementalBuild( AZUREUS_VERSION ));
  222. }
  223. public static int
  224. getIncrementalBuild(
  225. String version )
  226. {
  227. if ( !isCVSVersion(version)){
  228. return( 0 );
  229. }
  230. int p1 = version.indexOf( "_B" );
  231. if ( p1 == -1 ){
  232. return( -1 );
  233. }
  234. try{
  235. return( Integer.parseInt( version.substring(p1+2)));
  236. }catch( Throwable e ){
  237. System.out.println("can't parse version");
  238. return( -1 );
  239. }
  240. }
  241. /**
  242. * compare two version strings of form n.n.n.n (e.g. 1.2.3.4)
  243. * @param version_1
  244. * @param version_2
  245. * @return -ve -> version_1 lower, 0 = same, +ve -> version_1 higher
  246. */
  247. public static int
  248. compareVersions(
  249. String version_1,
  250. String version_2 )
  251. {
  252. try{
  253. if ( version_1.startsWith("." )){
  254. version_1 = "0" + version_1;
  255. }
  256. if ( version_2.startsWith("." )){
  257. version_2 = "0" + version_2;
  258. }
  259. version_1 = version_1.replaceAll("[^0-9.]", ".");
  260. version_2 = version_2.replaceAll("[^0-9.]", ".");
  261. StringTokenizer tok1 = new StringTokenizer(version_1,".");
  262. StringTokenizer tok2 = new StringTokenizer(version_2,".");
  263. while( true ){
  264. if ( tok1.hasMoreTokens() && tok2.hasMoreTokens()){
  265. int i1 = Integer.parseInt(tok1.nextToken());
  266. int i2 = Integer.parseInt(tok2.nextToken());
  267. if ( i1 != i2 ){
  268. return( i1 - i2 );
  269. }
  270. }else if ( tok1.hasMoreTokens()){
  271. int i1 = Integer.parseInt(tok1.nextToken());
  272. if ( i1 != 0 ){
  273. return( 1 );
  274. }
  275. }else if ( tok2.hasMoreTokens()){
  276. int i2 = Integer.parseInt(tok2.nextToken());
  277. if ( i2 != 0 ){
  278. return( -1 );
  279. }
  280. }else{
  281. return( 0 );
  282. }
  283. }
  284. }catch( Throwable e ){
  285. Debug.printStackTrace(e);
  286. return( 0 );
  287. }
  288. }
  289. public static boolean
  290. isValidVersionFormat(
  291. String version )
  292. {
  293. if ( version == null || version.length() == 0 ){
  294. return( false );
  295. }
  296. for (int i=0;i<version.length();i++){
  297. char c = version.charAt(i);
  298. if ( !( Character.isDigit( c ) || c == '.' )){
  299. return( false) ;
  300. }
  301. }
  302. if ( version.startsWith( "." ) || version.endsWith( "." ) || version.indexOf( ".." ) != -1 ){
  303. return( false );
  304. }
  305. return( true );
  306. }
  307. public static boolean
  308. isAzureusDomain(
  309. String host )
  310. {
  311. host = host.toLowerCase();
  312. for (int i=0; i<AZUREUS_DOMAINS.length; i++) {
  313. String domain = (String) AZUREUS_DOMAINS[i];
  314. if ( domain.equals( host )){
  315. return( true );
  316. }
  317. if ( host.endsWith("." + domain)){
  318. return( true );
  319. }
  320. }
  321. return( false );
  322. }
  323. public static void main(String[] args) {
  324. System.out.println(compareVersions("3.0.0.1", "3.0.0.0"));
  325. System.out.println(compareVersions("3.0.0.0_B1", "3.0.0.0"));
  326. System.out.println(compareVersions("3.0.0.0", "3.0.0.0_B1"));
  327. System.out.println(compareVersions("3.0.0.0_B1", "3.0.0.0_B4"));
  328. System.out.println(compareVersions("3.0.0.0..B1", "3.0.0.0_B4"));
  329. }
  330. }