Mercurial Hosting > luan
comparison src/luan/lib/Utils.java @ 143:fcb81fa2df0d
handle urls and java resources as files
git-svn-id: https://luan-java.googlecode.com/svn/trunk@144 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 13 Jun 2014 19:04:05 +0000 |
parents | 0594c132888b |
children | d310ebf4d6e7 |
comparison
equal
deleted
inserted
replaced
142:05f8c21160ef | 143:fcb81fa2df0d |
---|---|
3 import java.io.Reader; | 3 import java.io.Reader; |
4 import java.io.IOException; | 4 import java.io.IOException; |
5 import java.io.ByteArrayOutputStream; | 5 import java.io.ByteArrayOutputStream; |
6 import java.io.InputStream; | 6 import java.io.InputStream; |
7 import java.io.OutputStream; | 7 import java.io.OutputStream; |
8 import java.io.File; | |
9 import java.net.URL; | |
10 import java.net.MalformedURLException; | |
8 import luan.LuanState; | 11 import luan.LuanState; |
9 import luan.LuanException; | 12 import luan.LuanException; |
10 | 13 |
11 | 14 |
12 public final class Utils { | 15 public final class Utils { |
47 ByteArrayOutputStream out = new ByteArrayOutputStream(); | 50 ByteArrayOutputStream out = new ByteArrayOutputStream(); |
48 copyAll(in,out); | 51 copyAll(in,out); |
49 return out.toByteArray(); | 52 return out.toByteArray(); |
50 } | 53 } |
51 | 54 |
55 public static boolean isFile(String path) { | |
56 return new File(path).exists(); | |
57 } | |
58 | |
59 public static String toUrl(String path) { | |
60 if( path.indexOf(':') == -1 ) | |
61 return null; | |
62 if( path.startsWith("java:") ) { | |
63 path = path.substring(5); | |
64 URL url = ClassLoader.getSystemResource(path); | |
65 return url==null ? null : url.toString(); | |
66 } | |
67 try { | |
68 new URL(path); | |
69 return path; | |
70 } catch(MalformedURLException e) {} | |
71 return null; | |
72 } | |
73 | |
74 public static boolean exists(String path) { | |
75 return isFile(path) || toUrl(path)!=null; | |
76 } | |
52 } | 77 } |