view src/luan/modules/Utils.java @ 1367:836e00bf7ce2

add Lucene backup_to
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 18 Jun 2019 16:27:03 -0600
parents 1a68fc55a80c
children eb8b35dccd99
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();
	}

}