Author: oheger
Date: Tue Apr 2 18:59:34 2013
New Revision: 1463672
URL: http://svn.apache.org/r1463672
Log:
FileHandler now deals with objects implementing the InputStreamSupport
interface correctly.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java?rev=1463672&r1=1463671&r2=1463672&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java
(original)
+++
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/io/FileHandler.java
Tue Apr 2 18:59:34 2013
@@ -810,6 +810,48 @@ public class FileHandler
{
checkContent();
injectFileLocator(url);
+
+ if (getContent() instanceof InputStreamSupport)
+ {
+ loadFromStreamDirectly(in);
+ }
+ else
+ {
+ loadFromTransformedStream(in, encoding);
+ }
+ }
+
+ /**
+ * Loads data from an input stream if the associated {@code FileBased}
+ * object implements the {@code InputStreamSupport} interface.
+ *
+ * @param in the input stream
+ * @throws ConfigurationException if an error occurs
+ */
+ private void loadFromStreamDirectly(InputStream in)
+ throws ConfigurationException
+ {
+ try
+ {
+ ((InputStreamSupport) getContent()).read(in);
+ }
+ catch (IOException e)
+ {
+ throw new ConfigurationException(e);
+ }
+ }
+
+ /**
+ * Internal helper method for transforming an input stream to a reader and
+ * reading its content.
+ *
+ * @param in the input stream
+ * @param encoding the encoding
+ * @throws ConfigurationException if an error occurs
+ */
+ private void loadFromTransformedStream(InputStream in, String encoding)
+ throws ConfigurationException
+ {
Reader reader = null;
if (encoding != null)
Modified:
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java?rev=1463672&r1=1463671&r2=1463672&view=diff
==============================================================================
---
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java
(original)
+++
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/io/TestFileHandler.java
Tue Apr 2 18:59:34 2013
@@ -31,6 +31,7 @@ import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
+import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
@@ -1134,6 +1135,49 @@ public class TestFileHandler
}
/**
+ * Tests whether data can be read from an input stream.
+ */
+ @Test
+ public void testLoadInputStreamSupport() throws ConfigurationException
+ {
+ FileBasedInputStreamSupportTestImpl content =
+ new FileBasedInputStreamSupportTestImpl();
+ FileHandler handler = new FileHandler(content);
+ ByteArrayInputStream bin = new
ByteArrayInputStream(CONTENT.getBytes());
+ handler.load(bin);
+ assertEquals("Wrong content", "InputStream = " + CONTENT,
+ content.getContent());
+ }
+
+ /**
+ * Tests whether an IOException is handled when reading from an input
+ * stream.
+ */
+ @Test
+ public void testLoadInputStreamSupportIOException()
+ throws ConfigurationException, IOException
+ {
+ FileBasedInputStreamSupportTestImpl content =
+ EasyMock.createMock(FileBasedInputStreamSupportTestImpl.class);
+ ByteArrayInputStream bin = new
ByteArrayInputStream(CONTENT.getBytes());
+ IOException ioex = new IOException();
+ content.read(bin);
+ EasyMock.expectLastCall().andThrow(ioex);
+ EasyMock.replay(content);
+ FileHandler handler = new FileHandler(content);
+ try
+ {
+ handler.load(bin);
+ fail("IOException not detected!");
+ }
+ catch (ConfigurationException cex)
+ {
+ assertEquals("Wrong cause", ioex, cex.getCause());
+ }
+ EasyMock.verify(content);
+ }
+
+ /**
* An implementation of the FileBased interface used for test purposes.
*/
private static class FileBasedTestImpl implements FileBased
@@ -1175,6 +1219,26 @@ public class TestFileHandler
}
/**
+ * A test implementation of FileBased which can also read from input
+ * streams.
+ */
+ private static class FileBasedInputStreamSupportTestImpl extends
+ FileBasedTestImpl implements InputStreamSupport
+ {
+ public void read(InputStream in) throws ConfigurationException,
+ IOException
+ {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ int c;
+ while ((c = in.read()) != -1)
+ {
+ bos.write(c);
+ }
+ setContent("InputStream = " + bos.toString());
+ }
+ }
+
+ /**
* A FileBased implementation which also implements FileLocatorAware. This
* class adds information about the current file locator to the content
read
* and written.