comparison src/luan/lib/Utils.java @ 115:eacf6ce1b47d

add IoLib git-svn-id: https://luan-java.googlecode.com/svn/trunk@116 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 29 May 2014 09:26:44 +0000
parents 6ca02b188dba
children 1ff1c32417eb
comparison
equal deleted inserted replaced
114:c599206448b9 115:eacf6ce1b47d
1 package luan.lib; 1 package luan.lib;
2 2
3 import java.io.File; 3 import java.io.File;
4 import java.io.FileReader; 4 import java.io.FileReader;
5 import java.io.FileWriter;
5 import java.io.InputStreamReader; 6 import java.io.InputStreamReader;
6 import java.io.Reader; 7 import java.io.Reader;
8 import java.io.Writer;
7 import java.io.IOException; 9 import java.io.IOException;
10 import java.io.ByteArrayInputStream;
11 import java.io.ByteArrayOutputStream;
12 import java.io.FileInputStream;
13 import java.io.FileOutputStream;
14 import java.io.InputStream;
15 import java.io.OutputStream;
8 import java.net.URL; 16 import java.net.URL;
9 import luan.LuanState; 17 import luan.LuanState;
10 import luan.LuanException; 18 import luan.LuanException;
11 import luan.LuanElement; 19 import luan.LuanElement;
12 20
13 21
14 public final class Utils { 22 public final class Utils {
15 private Utils() {} // never 23 private Utils() {} // never
16 24
25 private static final int bufSize = 8192;
26
17 public static void checkNotNull(LuanState luan,Object v,String expected) throws LuanException { 27 public static void checkNotNull(LuanState luan,Object v,String expected) throws LuanException {
18 if( v == null ) 28 if( v == null )
19 throw luan.JAVA.exception("bad argument #1 ("+expected+" expected, got nil)"); 29 throw luan.JAVA.exception("bad argument #1 ("+expected+" expected, got nil)");
20 } 30 }
21 31
22 public static String readAll(Reader in) 32 public static String readAll(Reader in)
23 throws IOException 33 throws IOException
24 { 34 {
25 char[] a = new char[8192]; 35 char[] a = new char[bufSize];
26 StringBuilder buf = new StringBuilder(); 36 StringBuilder buf = new StringBuilder();
27 int n; 37 int n;
28 while( (n=in.read(a)) != -1 ) { 38 while( (n=in.read(a)) != -1 ) {
29 buf.append(a,0,n); 39 buf.append(a,0,n);
30 } 40 }
47 String s = readAll(in); 57 String s = readAll(in);
48 in.close(); 58 in.close();
49 return s; 59 return s;
50 } 60 }
51 61
62 public static void copyAll(InputStream in,OutputStream out)
63 throws IOException
64 {
65 byte[] a = new byte[bufSize];
66 int n;
67 while( (n=in.read(a)) != -1 ) {
68 out.write(a,0,n);
69 }
70 }
71
72 public static byte[] readAll(File file)
73 throws IOException
74 {
75 int len = (int)file.length();
76 ByteArrayOutputStream out = new ByteArrayOutputStream(len) {
77 public byte[] toByteArray() {
78 return buf;
79 }
80 };
81 FileInputStream in = new FileInputStream(file);
82 copyAll(in,out);
83 in.close();
84 return out.toByteArray();
85 }
86
87 public static void write(File file,String s)
88 throws IOException
89 {
90 Writer out = new FileWriter(file);
91 out.write(s);
92 out.close();
93 }
94
95 public static void writeAll(byte[] a,OutputStream out)
96 throws IOException
97 {
98 copyAll(new ByteArrayInputStream(a),out);
99 }
100
101 public static void writeAll(File file,byte[] a)
102 throws IOException
103 {
104 FileOutputStream fos = new FileOutputStream(file);
105 writeAll(a,fos);
106 fos.close();
107 }
108
109 public static byte[] readAll(InputStream in)
110 throws IOException
111 {
112 ByteArrayOutputStream out = new ByteArrayOutputStream();
113 copyAll(in,out);
114 return out.toByteArray();
115 }
116
52 } 117 }