comparison src/luan/lib/Utils.java @ 125:0cd559a16758

add sockets git-svn-id: https://luan-java.googlecode.com/svn/trunk@126 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 06 Jun 2014 05:59:11 +0000
parents 1ff1c32417eb
children 0594c132888b
comparison
equal deleted inserted replaced
124:f537ff5e511d 125:0cd559a16758
1 package luan.lib; 1 package luan.lib;
2 2
3 import java.io.File;
4 import java.io.FileReader;
5 import java.io.FileWriter;
6 import java.io.InputStreamReader;
7 import java.io.Reader; 3 import java.io.Reader;
8 import java.io.Writer;
9 import java.io.IOException; 4 import java.io.IOException;
10 import java.io.ByteArrayInputStream;
11 import java.io.ByteArrayOutputStream; 5 import java.io.ByteArrayOutputStream;
12 import java.io.FileInputStream;
13 import java.io.FileOutputStream;
14 import java.io.InputStream; 6 import java.io.InputStream;
15 import java.io.OutputStream; 7 import java.io.OutputStream;
16 import java.net.URL;
17 import luan.LuanState; 8 import luan.LuanState;
18 import luan.LuanException; 9 import luan.LuanException;
19 import luan.LuanElement;
20 10
21 11
22 public final class Utils { 12 public final class Utils {
23 private Utils() {} // never 13 private Utils() {} // never
24 14
39 buf.append(a,0,n); 29 buf.append(a,0,n);
40 } 30 }
41 return buf.toString(); 31 return buf.toString();
42 } 32 }
43 33
44 public static String read(File file)
45 throws IOException
46 {
47 Reader in = new FileReader(file);
48 String s = readAll(in);
49 in.close();
50 return s;
51 }
52
53 public static String read(URL url)
54 throws IOException
55 {
56 Reader in = new InputStreamReader(url.openStream());
57 String s = readAll(in);
58 in.close();
59 return s;
60 }
61
62 public static void copyAll(InputStream in,OutputStream out) 34 public static void copyAll(InputStream in,OutputStream out)
63 throws IOException 35 throws IOException
64 { 36 {
65 byte[] a = new byte[bufSize]; 37 byte[] a = new byte[bufSize];
66 int n; 38 int n;
67 while( (n=in.read(a)) != -1 ) { 39 while( (n=in.read(a)) != -1 ) {
68 out.write(a,0,n); 40 out.write(a,0,n);
69 } 41 }
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 } 42 }
108 43
109 public static byte[] readAll(InputStream in) 44 public static byte[] readAll(InputStream in)
110 throws IOException 45 throws IOException
111 { 46 {