/cc.creativecomputing/src/cc/creativecomputing/CCSystem.java

http://creativecomputing.googlecode.com/ · Java · 510 lines · 331 code · 76 blank · 103 comment · 61 complexity · 20895adda2c4e8e0ded93b10d03ff8ca MD5 · raw file

  1. package cc.creativecomputing;
  2. import java.io.IOException;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.Method;
  5. import cc.creativecomputing.util.CCStringUtil;
  6. /**
  7. *
  8. * @author texone
  9. *
  10. */
  11. public class CCSystem{
  12. private static enum Platform{
  13. WINDOWS, MACOS9, MACOSX, MACOSX_INTEL,LINUX, OTHER;
  14. /**
  15. * Use this method to find out on which platform the application is
  16. * running
  17. *
  18. * @return
  19. */
  20. static Platform getPlatform(){
  21. if (platformName.toLowerCase().indexOf("mac") != -1){
  22. // can only check this property if running on a mac
  23. // on a pc it throws a security exception and kills the applet
  24. // (but on the mac it does just fine)
  25. if (System.getProperty("mrj.version") != null){ // running on a
  26. // mac
  27. if (platformName.equals("Mac OS X")){
  28. if(System.getProperty("os.arch").equals("i386"))return MACOSX_INTEL;
  29. return MACOSX;
  30. }else
  31. return MACOS9;
  32. }
  33. }
  34. final String myOsName = System.getProperty("os.name");
  35. if (myOsName.indexOf("Windows") != -1){
  36. return WINDOWS;
  37. }else if (myOsName.equals("Linux")){ // true for the ibm vm
  38. return LINUX;
  39. }
  40. return OTHER;
  41. }
  42. }
  43. public static final Platform WINDOWS = Platform.WINDOWS;
  44. public static final Platform MACOSX = Platform.MACOSX;
  45. public static final Platform MACOSX_INTEL = Platform.MACOSX_INTEL;
  46. public static final Platform MACOS9 = Platform.MACOS9;
  47. public static final Platform LINUX = Platform.LINUX;
  48. public static final Platform OTHER = Platform.OTHER;
  49. /**
  50. * Current platform in use, one of the PConstants WINDOWS, MACOSX, MACOS9,
  51. * LINUX or OTHER.
  52. */
  53. static public Platform PLATFORM;
  54. public static boolean isMACOS(){
  55. return PLATFORM == MACOSX || PLATFORM == MACOS9;
  56. }
  57. /**
  58. * Current platform in use.
  59. * <P>
  60. * Equivalent to System.getProperty("os.name"), just used internally.
  61. */
  62. static public String platformName = System.getProperty("os.name");
  63. static public void link(String here){
  64. link(here, null);
  65. }
  66. /**
  67. * Link to an external page without all the muss.
  68. * <P>
  69. * When run with an applet, uses the browser to open the url,
  70. * for applications, attempts to launch a browser with the url.
  71. * <P>
  72. * Works on Mac OS X and Windows. For Linux, use:
  73. * <PRE>open(new String[] { "firefox", url });</PRE>
  74. * or whatever you want as your browser, since Linux doesn't
  75. * yet have a standard method for launching URLs.
  76. */
  77. public static void link(String url, String frameTitle){
  78. try{
  79. if (PLATFORM == Platform.WINDOWS){
  80. // the following uses a shell execute to launch the .html file
  81. // note that under cygwin, the .html files have to be chmodded +x
  82. // after they're unpacked from the zip file. i don't know why,
  83. // and don't understand what this does in terms of windows
  84. // permissions. without the chmod, the command prompt says
  85. // "Access is denied" in both cygwin and the "dos" prompt.
  86. //Runtime.getRuntime().exec("cmd /c " + currentDir + "\\reference\\" +
  87. // referenceFile + ".html");
  88. // replace ampersands with control sequence for DOS.
  89. // solution contributed by toxi on the bugs board.
  90. url = url.replaceAll("&", "^&");
  91. // open dos prompt, give it 'start' command, which will
  92. // open the url properly. start by itself won't work since
  93. // it appears to need cmd
  94. Runtime.getRuntime().exec("cmd /c start " + url);
  95. }else if ((PLATFORM == Platform.MACOSX) || (PLATFORM == Platform.MACOS9)){
  96. //com.apple.mrj.MRJFileUtils.openURL(url);
  97. try{
  98. Class<?> mrjFileUtils = Class.forName("com.apple.mrj.MRJFileUtils");
  99. Method openMethod = mrjFileUtils.getMethod("openURL", new Class[] {String.class});
  100. openMethod.invoke(null, new Object[] {url});
  101. }catch (Exception e){
  102. e.printStackTrace();
  103. }
  104. }else{
  105. throw new RuntimeException("Can't open URLs for this platform");
  106. }
  107. }catch (IOException e){
  108. e.printStackTrace();
  109. throw new RuntimeException("Could not open " + url);
  110. }
  111. }
  112. /**
  113. * Attempt to open a file using the platform's shell.
  114. */
  115. static public void open(String filename){
  116. if (PLATFORM == Platform.WINDOWS){
  117. // just launching the .html file via the shell works
  118. // but make sure to chmod +x the .html files first
  119. // also place quotes around it in case there's a space
  120. // in the user.dir part of the url
  121. try{
  122. Runtime.getRuntime().exec("cmd /c \"" + filename + "\"");
  123. }catch (IOException e){
  124. e.printStackTrace();
  125. throw new RuntimeException("Could not open " + filename);
  126. }
  127. }else if (PLATFORM == Platform.MACOSX){
  128. // osx fix contributed by chandler for rev 0113
  129. try{
  130. // Java on OS X doesn't like to exec commands inside quotes
  131. // for some reason.. escape spaces with slashes just in case
  132. if (filename.indexOf(' ') != -1){
  133. StringBuffer sb = new StringBuffer();
  134. char c[] = filename.toCharArray();
  135. for (int i = 0; i < c.length; i++){
  136. if (c[i] == ' '){
  137. sb.append("\\\\ ");
  138. }else{
  139. sb.append(c[i]);
  140. }
  141. }
  142. filename = sb.toString();
  143. }
  144. Runtime.getRuntime().exec("open " + filename);
  145. }catch (IOException e){
  146. e.printStackTrace();
  147. throw new RuntimeException("Could not open " + filename);
  148. }
  149. }else if (PLATFORM == Platform.MACOS9){
  150. // prepend file:// on this guy since it's a file
  151. String url = "file://" + filename;
  152. // replace spaces with %20 for the file url
  153. // otherwise the mac doesn't like to open it
  154. // can't just use URLEncoder, since that makes slashes into
  155. // %2F characters, which is no good. some might say "useless"
  156. if (url.indexOf(' ') != -1){
  157. StringBuffer sb = new StringBuffer();
  158. char c[] = url.toCharArray();
  159. for (int i = 0; i < c.length; i++){
  160. if (c[i] == ' '){
  161. sb.append("%20");
  162. }else{
  163. sb.append(c[i]);
  164. }
  165. }
  166. url = sb.toString();
  167. }
  168. link(url);
  169. }else{ // give up and just pass it to Runtime.exec()
  170. open(new String[] {filename});
  171. }
  172. }
  173. /**
  174. * Launch a process using a platforms shell, and an array of
  175. * args passed on the command line.
  176. */
  177. static public Process open(String args[]){
  178. try{
  179. return Runtime.getRuntime().exec(args);
  180. }catch (Exception e){
  181. e.printStackTrace();
  182. throw new RuntimeException("Could not open " + CCStringUtil.join(args, ' '));
  183. }
  184. }/** Path to sketch folder */
  185. static public String applicationPath = System.getProperty("user.dir");
  186. //////////////////////////////////////////////////////////////////////////////////////////////////
  187. //////////////////////////////////////////////////////////////////////////////////////////////////
  188. //
  189. // PRINT
  190. //
  191. //////////////////////////////////////////////////////////////////////////////////////////////////
  192. //////////////////////////////////////////////////////////////////////////////////////////////////
  193. static public void print(final byte theByte){
  194. System.out.print(theByte);
  195. System.out.flush();
  196. }
  197. static public void print(final boolean theBoolean){
  198. System.out.print(theBoolean);
  199. System.out.flush();
  200. }
  201. static public void print(final char theChar){
  202. System.out.print(theChar);
  203. System.out.flush();
  204. }
  205. static public void print(int what){
  206. System.out.print(what);
  207. System.out.flush();
  208. }
  209. static public void print(float what){
  210. System.out.print(what);
  211. System.out.flush();
  212. }
  213. static public void print(double what){
  214. System.out.print(what);
  215. System.out.flush();
  216. }
  217. static public void print(String what){
  218. System.out.print(what);
  219. System.out.flush();
  220. }
  221. static public void print(Object what){
  222. if (what == null){
  223. // special case since this does fuggly things on > 1.1
  224. System.out.print("null");
  225. }else{
  226. String name = what.getClass().getName();
  227. if (name.charAt(0) == '['){
  228. switch (name.charAt(1)){
  229. case '[':
  230. // don't even mess with multi-dimensional arrays (case '[')
  231. // or anything else that's not int, float, boolean, char
  232. System.out.print(what);
  233. System.out.print(' ');
  234. break;
  235. case 'L':
  236. // print a 1D array of objects as individual elements
  237. Object poo[] = (Object[]) what;
  238. for (int i = 0; i < poo.length; i++){
  239. System.out.print(poo[i]);
  240. System.out.print(' ');
  241. }
  242. break;
  243. case 'Z': // boolean
  244. boolean zz[] = (boolean[]) what;
  245. for (int i = 0; i < zz.length; i++){
  246. System.out.print(zz[i]);
  247. System.out.print(' ');
  248. }
  249. break;
  250. case 'B': // byte
  251. byte bb[] = (byte[]) what;
  252. for (int i = 0; i < bb.length; i++){
  253. System.out.print(bb[i]);
  254. System.out.print(' ');
  255. }
  256. break;
  257. case 'C': // char
  258. char cc[] = (char[]) what;
  259. for (int i = 0; i < cc.length; i++){
  260. System.out.print(cc[i]);
  261. System.out.print(' ');
  262. }
  263. break;
  264. case 'I': // int
  265. int ii[] = (int[]) what;
  266. for (int i = 0; i < ii.length; i++){
  267. System.out.print(ii[i]);
  268. System.out.print(' ');
  269. }
  270. break;
  271. case 'F': // float
  272. float ff[] = (float[]) what;
  273. for (int i = 0; i < ff.length; i++){
  274. System.out.print(ff[i]);
  275. System.out.print(' ');
  276. }
  277. break;
  278. case 'D': // double
  279. double dd[] = (double[]) what;
  280. for (int i = 0; i < dd.length; i++){
  281. System.out.print(dd[i]);
  282. System.out.print(' ');
  283. }
  284. break;
  285. default:
  286. System.out.print(what);
  287. }
  288. }else{
  289. System.out.print(what); //.toString());
  290. }
  291. }
  292. }
  293. //
  294. static public void println(){
  295. System.out.println();
  296. }
  297. //
  298. static public void println(byte what){
  299. print(what);
  300. System.out.println();
  301. }
  302. static public void println(boolean what){
  303. print(what);
  304. System.out.println();
  305. }
  306. static public void println(char what){
  307. print(what);
  308. System.out.println();
  309. }
  310. static public void println(int what){
  311. print(what);
  312. System.out.println();
  313. }
  314. static public void println(float what){
  315. print(what);
  316. System.out.println();
  317. }
  318. static public void println(double what){
  319. print(what);
  320. System.out.println();
  321. }
  322. static public void println(String what){
  323. print(what);
  324. System.out.println();
  325. }
  326. static public void println(Object what){
  327. if (what == null){
  328. // special case since this does fuggly things on > 1.1
  329. System.out.println("null");
  330. }else{
  331. String name = what.getClass().getName();
  332. if (name.charAt(0) == '['){
  333. switch (name.charAt(1)){
  334. case '[':
  335. // don't even mess with multi-dimensional arrays (case '[')
  336. // or anything else that's not int, float, boolean, char
  337. System.out.println(what);
  338. break;
  339. case 'L':
  340. // print a 1D array of objects as individual elements
  341. Object poo[] = (Object[]) what;
  342. for (int i = 0; i < poo.length; i++){
  343. System.out.println(poo[i]);
  344. }
  345. break;
  346. case 'Z': // boolean
  347. boolean zz[] = (boolean[]) what;
  348. for (int i = 0; i < zz.length; i++){
  349. System.out.println(zz[i]);
  350. }
  351. break;
  352. case 'B': // byte
  353. byte bb[] = (byte[]) what;
  354. for (int i = 0; i < bb.length; i++){
  355. System.out.println(bb[i]);
  356. }
  357. break;
  358. case 'C': // char
  359. char cc[] = (char[]) what;
  360. for (int i = 0; i < cc.length; i++){
  361. System.out.println(cc[i]);
  362. }
  363. break;
  364. case 'I': // int
  365. int ii[] = (int[]) what;
  366. for (int i = 0; i < ii.length; i++){
  367. System.out.println(ii[i]);
  368. }
  369. break;
  370. case 'F': // float
  371. float ff[] = (float[]) what;
  372. for (int i = 0; i < ff.length; i++){
  373. System.out.println(ff[i]);
  374. }
  375. break;
  376. case 'D': // double
  377. double dd[] = (double[]) what;
  378. for (int i = 0; i < dd.length; i++){
  379. System.out.println(dd[i]);
  380. }
  381. break;
  382. default:
  383. System.out.println(what);
  384. }
  385. }else{
  386. System.out.println(what); //.toString());
  387. }
  388. }
  389. }
  390. static public void printObject(final Object theObject, String theSpace){
  391. try {
  392. final Field[] myFields = theObject.getClass().getDeclaredFields();
  393. System.out.println(theSpace + theObject.getClass().getName());
  394. theSpace = theSpace.concat(" ");
  395. for(Field myField:myFields){
  396. myField.setAccessible(true);
  397. if(myField.getType().isPrimitive() || myField.getType().isInstance("")){
  398. System.out.println(theSpace + myField.getType() + " " + myField.getName()+" "+myField.get(theObject));
  399. }else{
  400. System.out.println("noprimitiv:"+myField.getType().getName());
  401. // System.out.println(theSpace + myField.getType() + " " + myField.getName()+":");
  402. // printObject(myField.get(theObject),theSpace+" ");
  403. }
  404. }
  405. } catch (Exception e) {
  406. throw new RuntimeException("Unable to print object:",e);
  407. }
  408. }
  409. /**
  410. * Returns the total amount of memory in the Java virtual machine.
  411. * The value returned by this method may vary over time, depending
  412. * on the host environment. Note that the amount of memory required
  413. * to hold an object of any given type may be implementation-dependent.
  414. * @return the total amount of memory currently available for current and future objects, measured in bytes.
  415. */
  416. public static long memoryTotal(){
  417. return Runtime.getRuntime().totalMemory();
  418. }
  419. /**
  420. * Returns the amount of free memory in the Java Virtual Machine.
  421. * Calling the gc method may result in increasing the value returned by freeMemory.
  422. * @return an approximation to the total amount of memory currently available for future allocated objects, measured in bytes.
  423. */
  424. public static long memoryFree(){
  425. return Runtime.getRuntime().freeMemory();
  426. }
  427. /**
  428. * Returns the memory currently used by the program.
  429. * @return
  430. */
  431. public static long memoryInUse(){
  432. return memoryTotal() - memoryFree();
  433. }
  434. }