Permalink
Browse files

Add optional parameter "encoding" to ResourceFactory.new*() methods

  • Loading branch information...
1 parent b014470 commit 8dd9ab3d8f7ed9c18229e1ed890be0bd39ddb11f @ge0ffrey committed Mar 29, 2012
@@ -46,7 +46,9 @@
implements
InternalResource,
Externalizable {
+
private String path;
+ private String encoding;
private ClassLoader classLoader;
private Class< ? > clazz;
private long lastRead;
@@ -58,12 +60,14 @@ public ClassPathResource() {
public ClassPathResource(String path) {
this( path,
null,
+ null,
null );
}
public ClassPathResource(String path,
Class<?> clazz) {
this( path,
+ null,
clazz,
null );
}
@@ -72,16 +76,45 @@ public ClassPathResource(String path,
ClassLoader classLoader) {
this( path,
null,
+ null,
classLoader );
}
public ClassPathResource(String path,
+ String encoding) {
+ this( path,
+ encoding,
+ null,
+ null );
+ }
+
+ public ClassPathResource(String path,
+ String encoding,
+ Class<?> clazz) {
+ this( path,
+ encoding,
+ clazz,
+ null );
+ }
+
+ public ClassPathResource(String path,
+ String encoding,
+ ClassLoader classLoader) {
+ this( path,
+ encoding,
+ null,
+ classLoader );
+ }
+
+ public ClassPathResource(String path,
+ String encoding,
Class<?> clazz,
ClassLoader classLoader) {
if ( path == null ) {
throw new IllegalArgumentException( "path cannot be null" );
}
this.path = path;
+ this.encoding = encoding;
this.clazz = clazz;
this.classLoader = ClassLoaderUtil.getClassLoader( classLoader == null ? null : new ClassLoader[] { classLoader },
clazz,
@@ -153,7 +186,11 @@ public long getLastRead() {
}
public Reader getReader() throws IOException {
- return new InputStreamReader( getInputStream() );
+ if ( this.encoding != null ) {
+ return new InputStreamReader( getInputStream(), encoding );
+ } else {
+ return new InputStreamReader( getInputStream() );
+ }
}
public boolean isDirectory() {
@@ -28,21 +28,33 @@
import org.drools.io.internal.InternalResource;
public class InputStreamResource extends BaseResource implements InternalResource {
+
private transient InputStream stream;
-
+ private String encoding;
+
public InputStreamResource(InputStream stream) {
+ this(stream, null);
+ }
+
+ public InputStreamResource(InputStream stream,
+ String encoding) {
if ( stream == null ) {
throw new IllegalArgumentException( "stream cannot be null" );
}
this.stream = stream;
+ this.encoding = encoding;
}
public InputStream getInputStream() throws IOException {
return stream;
}
public Reader getReader() throws IOException {
- return new InputStreamReader( getInputStream() );
+ if (encoding == null) {
+ return new InputStreamReader( getInputStream() );
+ } else {
+ return new InputStreamReader( getInputStream(), encoding );
+ }
}
public URL getURL() throws IOException {
@@ -72,6 +72,28 @@ public Resource newClassPathResource(String path,
return new ClassPathResource( path,
clazz );
}
+
+ public Resource newClassPathResource(String path,
+ String encoding) {
+ return new ClassPathResource( path,
+ encoding );
+ }
+
+ public Resource newClassPathResource(String path,
+ String encoding,
+ ClassLoader classLoader) {
+ return new ClassPathResource( path,
+ encoding,
+ classLoader );
+ }
+
+ public Resource newClassPathResource(String path,
+ String encoding,
+ Class<?> clazz) {
+ return new ClassPathResource( path,
+ encoding,
+ clazz );
+ }
public Resource newFileSystemResource(File file) {
return new FileSystemResource( file );
@@ -85,6 +107,12 @@ public Resource newInputStreamResource(InputStream stream) {
return new InputStreamResource( stream );
}
+ public Resource newInputStreamResource(InputStream stream,
+ String encoding) {
+ return new InputStreamResource( stream,
+ encoding);
+ }
+
public Resource newReaderResource(Reader reader) {
return new ReaderResource( reader );
}

0 comments on commit 8dd9ab3

Please sign in to comment.