source: main/trunk/greenstone3/src/java/org/greenstone/util/Misc.java@ 31573

Last change on this file since 31573 was 31573, checked in by ak19, 7 years ago

Commit ahead of larger changes: Misc.NEWLINE now works sets the NEWLINE constant to be appropriate for the OS and uses the more convenient System property when we're working with Java 7+ or otherwise manually decides based on OS.

  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1 /*
2 * Misc.java
3 * Copyright (C) 2002 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20package org.greenstone.util;
21
22import java.util.HashMap;
23import java.util.Set;
24import java.util.Map;
25import java.util.Iterator;
26import java.util.Properties;
27import java.io.InputStream;
28import java.io.BufferedReader;
29import java.io.InputStreamReader;
30import java.io.IOException;
31import java.net.HttpURLConnection;
32import java.net.URL;
33import java.net.URLConnection;
34
35/** contains miscellaneous functions */
36public class Misc {
37
38 // Can initialise static final vars on declaration or in static initialisation code block
39 // http://stackoverflow.com/questions/2339932/java-can-final-variables-be-initialized-in-static-initialization-block
40 // Initialise object member final vars on declaration or in constructors
41 public static final String NEWLINE;
42
43 // Before Java 7, no System.lineSeparator() or System.getProperty("line.separator")
44 // And on local linux, am compiling with JDK 6, so need this.
45 // http://stackoverflow.com/questions/207947/how-do-i-get-a-platform-dependent-new-line-character
46
47 static {
48 // http://stackoverflow.com/questions/2591083/getting-java-version-at-runtime
49 // https://www.tutorialspoint.com/java/lang/package_getspecificationversion.htm
50
51 double java_version = Double.parseDouble(System.getProperty("java.specification.version"));
52 if(java_version >= 1.7) { // https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
53 NEWLINE = System.getProperty("line.separator");
54 } else {
55 NEWLINE = isWindows() ? "\r\n" : "\n";
56 }
57 }
58
59 public static void printHash(HashMap map) {
60 Set entries = map.entrySet();
61 Iterator i = entries.iterator();
62 while (i.hasNext()) {
63 Map.Entry m = (Map.Entry)i.next();
64 String name = (String)m.getKey();
65 String value = (String)m.getValue();
66 }
67 }
68
69 /** Method to determine if the host system is Windows based.
70 * @return a boolean which is true if the platform is Windows,
71 * false otherwise
72 */
73 public static boolean isWindows() {
74 Properties props = System.getProperties();
75 String os_name = props.getProperty("os.name","");
76 if(os_name.startsWith("Windows")) {
77 return true;
78 }
79 return false;
80 }
81
82 public static boolean isWindows9x() {
83 Properties props = System.getProperties();
84 String os_name = props.getProperty("os.name","");
85 if(os_name.startsWith("Windows") && os_name.indexOf("9") != -1) {
86 return true;
87 }
88 return false;
89 }
90
91 /** Method to determine if the host system is MacOS based.
92 * @return a boolean which is true if the platform is MacOS, false otherwise
93 */
94 public static boolean isMac() {
95 Properties props = System.getProperties();
96 String os_name = props.getProperty("os.name","");
97 if(os_name.startsWith("Mac OS")) {
98 return true;
99 }
100 return false;
101 }
102
103 public static String getGsdlOS() {
104 if (isWindows()) {
105 return "windows";
106 }
107 if (isMac()) {
108 return "darwin";
109 }
110 return "linux";
111 }
112
113 public static boolean isBigEndian() {
114
115 if (System.getProperty("sun.cpu.endian").equals("big")) {
116 return true;
117 }
118 return false;
119
120 }
121
122 public static BufferedReader makeHttpConnection(String url_string)
123 throws java.net.MalformedURLException, java.io.IOException {
124 BufferedReader reader = null;
125 URL url = new URL(url_string);
126 HttpURLConnection connection = (HttpURLConnection)url.openConnection();
127 InputStream input = connection.getInputStream();
128 reader = new BufferedReader(new InputStreamReader(input));
129 return reader;
130 }
131
132 public static void main(String [] args) {
133 isBigEndian();
134 }
135
136}
137
Note: See TracBrowser for help on using the repository browser.