view src/luan/modules/Utils.java @ 889:5731a7707466

remove _forwarded
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 06 Oct 2016 23:24:30 -0600
parents 1a68fc55a80c
children 836e00bf7ce2
line wrap: on
line source

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.io.InputStreamReader;
import java.net.URL;
import java.net.MalformedURLException;
import luan.LuanException;
import luan.LuanTable;
import luan.LuanFunction;


public final class Utils {
	private Utils() {}  // never

	static final int bufSize = 8192;

	private static void checkNotNull(Object v,String expected,int pos) throws LuanException {
		if( v == null )
			throw new LuanException("bad argument #"+pos+" ("+expected+" expected, got nil)");
	}

	public static void checkNotNull(String s,int pos) throws LuanException {
		checkNotNull(s,"string",pos);
	}

	public static void checkNotNull(String s) throws LuanException {
		checkNotNull(s,1);
	}

	public static void checkNotNull(byte[] b,int pos) throws LuanException {
		checkNotNull(b,"binary",pos);
	}

	public static void checkNotNull(byte[] b) throws LuanException {
		checkNotNull(b,1);
	}

	public static void checkNotNull(LuanTable t,int pos) throws LuanException {
		checkNotNull(t,"table",pos);
	}

	public static void checkNotNull(LuanTable t) throws LuanException {
		checkNotNull(t,1);
	}

	public static void checkNotNull(Number n,int pos) throws LuanException {
		checkNotNull(n,"number",pos);
	}

	public static void checkNotNull(Number n) throws LuanException {
		checkNotNull(n,1);
	}

	public static void checkNotNull(LuanFunction fn,int pos) throws LuanException {
		checkNotNull(fn,"function",pos);
	}

	public static void checkNotNull(LuanFunction fn) throws LuanException {
		checkNotNull(fn,1);
	}

	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);
		}
	}
*/
/*
	private static File toFile(String path) {
		if( path.contains("//") )
			return null;
		File file = new File(path);
		return file.exists() ? file : null;
	}

	private static URL toUrl(String path) {
		if( path.indexOf(':') == -1 )
			return null;
		if( path.startsWith("classpath:") ) {
			path = path.substring(10);
			if( path.contains("//") )
				return null;
			URL url;
			if( !path.contains("#") ) {
				url = ClassLoader.getSystemResource(path);
			} else {
				String[] a = path.split("#");
				url = ClassLoader.getSystemResource(a[0]);
				if( url==null ) {
					for( int i=1; i<a.length; i++ ) {
						url = ClassLoader.getSystemResource(a[0]+"/"+a[i]);
						if( url != null ) {
							try {
								url = new URL(url,".");
							} catch(MalformedURLException e) {
								throw new RuntimeException(e);
							}
							break;
						}
					}
				}
			}
			return url==null ? null : url;
		}
		try {
			return new URL(path);
		} catch(MalformedURLException e) {}
		return null;
	}

	static boolean exists(String path) {
		return toFile(path)!=null || toUrl(path)!=null;
	}
*/



/*	replace by uri"os:..."

	// process

	public static class ProcessException extends IOException {
		private ProcessException(String msg) {
			super(msg);
		}
	}

	public static void checkProcess(Process proc)
		throws IOException
	{
		try {
			proc.waitFor();
		} catch(InterruptedException e) {
			throw new RuntimeException(e);
		}
		int exitVal = proc.exitValue();
		if( exitVal != 0 ) {
			Reader err = new InputStreamReader(proc.getErrorStream());
			String error = readAll(err);
			err.close();
			throw new ProcessException(error);
		}
	}

	public static String getOutput(Process proc)
		throws IOException
	{
		Reader in = new InputStreamReader(proc.getInputStream());
		String s = readAll(in);
		in.close();
		return s;
	}

	public static String execProcess(String... cmd)
		throws IOException
	{
		Process proc = Runtime.getRuntime().exec(cmd);
		String s = getOutput(proc);
		checkProcess(proc);
		return s;
	}
*/
}