diff core/src/luan/modules/Utils.java @ 171:3dcb0f9bee82

add core component git-svn-id: https://luan-java.googlecode.com/svn/trunk@172 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 05:41:22 +0000
parents src/luan/modules/Utils.java@4c0131c2b650
children 1cb298d918b2
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/src/luan/modules/Utils.java	Sun Jun 22 05:41:22 2014 +0000
@@ -0,0 +1,85 @@
+package luan.modules;
+
+import java.io.Reader;
+import java.io.IOException;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.File;
+import java.net.URL;
+import java.net.MalformedURLException;
+import luan.LuanState;
+import luan.LuanException;
+
+
+public final class Utils {
+	private Utils() {}  // never
+
+	static final int bufSize = 8192;
+
+	public static void checkNotNull(LuanState luan,Object v,String expected) throws LuanException {
+		if( v == null )
+			throw luan.exception("bad argument #1 ("+expected+" expected, got nil)");
+	}
+
+	public static String readAll(Reader in)
+		throws IOException
+	{
+		char[] a = new char[bufSize];
+		StringBuilder buf = new StringBuilder();
+		int n;
+		while( (n=in.read(a)) != -1 ) {
+			buf.append(a,0,n);
+		}
+		return buf.toString();
+	}
+
+	public static void copyAll(InputStream in,OutputStream out)
+		throws IOException
+	{
+		byte[] a = new byte[bufSize];
+		int n;
+		while( (n=in.read(a)) != -1 ) {
+			out.write(a,0,n);
+		}
+	}
+
+	public static byte[] readAll(InputStream in)
+		throws IOException
+	{
+		ByteArrayOutputStream out = new ByteArrayOutputStream();
+		copyAll(in,out);
+		return out.toByteArray();
+	}
+
+	public static boolean exists(File file) {
+		try {
+			return file.exists() && file.getName().equals(file.getCanonicalFile().getName());
+		} catch(IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
+	public static boolean isFile(String path) {
+		return exists(new File(path));
+	}
+
+	public static String toUrl(String path) {
+		if( path.indexOf(':') == -1 )
+			return null;
+		if( path.startsWith("java:") ) {
+			path = path.substring(5);
+			URL url = ClassLoader.getSystemResource(path);
+			return url==null ? null : url.toString();
+		}
+		try {
+			new URL(path);
+			return path;
+		} catch(MalformedURLException e) {}
+		return null;
+	}
+
+	public static boolean exists(String path) {
+		return isFile(path) || toUrl(path)!=null;
+	}
+}