|
1 |
| package com.agical.rdoc.util; |
|
2 |
| |
|
3 |
| import java.io.IOException; |
|
4 |
| import java.io.InputStream; |
|
5 |
| import java.io.InputStreamReader; |
|
6 |
| import java.io.Reader; |
|
7 |
| import java.io.UnsupportedEncodingException; |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| public class Read { |
|
15 |
| |
|
16 |
1
| public static InputStream readAsStream( Class clazz, String fileName ) {
|
|
17 |
1
| return clazz.getResourceAsStream( fileName );
|
|
18 |
| } |
|
19 |
| |
|
20 |
0
| public static String read( Class clazz, String fileName ) throws IOException {
|
|
21 |
0
| return read( clazz, fileName, "UTF-8" );
|
|
22 |
| } |
|
23 |
| |
|
24 |
0
| public static String read( Class clazz, String fileName, String enc ) throws IOException {
|
|
25 |
0
| InputStream inputStream = null;
|
|
26 |
0
| try {
|
|
27 |
0
| inputStream = readAsStream( clazz, fileName );
|
|
28 |
0
| return readInputStream(enc, inputStream);
|
|
29 |
| } finally { |
|
30 |
0
| if( inputStream != null ) {
|
|
31 |
0
| try {
|
|
32 |
0
| inputStream.close();
|
|
33 |
| } catch (IOException e) { |
|
34 |
| |
|
35 |
| } |
|
36 |
| } |
|
37 |
| } |
|
38 |
| } |
|
39 |
| |
|
40 |
0
| public static String readInputStream(String enc, InputStream inputStream) throws UnsupportedEncodingException, IOException {
|
|
41 |
0
| StringBuffer stringBuffer = new StringBuffer();
|
|
42 |
0
| Reader reader = new InputStreamReader( inputStream, enc );
|
|
43 |
0
| char[] tmp = new char[1024];
|
|
44 |
0
| int read = 0;
|
|
45 |
0
| while( (read = reader.read(tmp)) != -1 ) {
|
|
46 |
0
| stringBuffer.append( tmp, 0, read );
|
|
47 |
| } |
|
48 |
0
| return stringBuffer.toString();
|
|
49 |
| } |
|
50 |
| |
|
51 |
| } |