blob: df85adf7d7650c6444f4ccf75b06029c8e2b7447 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
/*******************************************************************************
* Copyright (c) 2012 EclipseSource and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* EclipseSource - initial API and implementation
******************************************************************************/
package org.eclipse.rap.clientscripting.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Example for a utility that helps reading script file contents from the class path.
*/
public class ResourceLoaderUtil {
private static final String CHARSET = "UTF-8";
private static final ClassLoader CLASSLOADER = ResourceLoaderUtil.class.getClassLoader();
private ResourceLoaderUtil() {
// prevent instantiation
}
/**
* Reads the content of a given text resource.
*
* @param resource the fully qualified name of the resource, without leading slash
* @return the contents of the resource as string
* @throws IllegalArgumentException if the resource could not be read
*/
public static String readTextContent( String resource ) {
try {
return readTextContentChecked( resource );
} catch( IOException e ) {
throw new IllegalArgumentException( "Failed to read resource: " + resource );
}
}
private static String readTextContentChecked( String resource ) throws IOException {
InputStream stream = CLASSLOADER.getResourceAsStream( resource );
if( stream == null ) {
throw new IllegalArgumentException( "Resource not found: " + resource );
}
try {
BufferedReader reader = new BufferedReader( new InputStreamReader( stream, CHARSET ) );
return readLines( reader );
} finally {
stream.close();
}
}
private static String readLines( BufferedReader reader ) throws IOException {
StringBuilder builder = new StringBuilder();
String line = reader.readLine();
while( line != null ) {
builder.append( line );
builder.append( '\n' );
line = reader.readLine();
}
return builder.toString();
}
}
|