33import java .io .ByteArrayInputStream ;
44import java .io .ByteArrayOutputStream ;
55import java .io .File ;
6- import java .io .FileInputStream ;
7- import java .io .FileNotFoundException ;
86import java .io .IOException ;
97import java .io .InputStream ;
108import java .io .OutputStream ;
9+ import java .io .UncheckedIOException ;
1110import java .net .URL ;
11+ import java .nio .file .Files ;
1212import java .nio .file .Path ;
1313import java .util .Arrays ;
1414import java .util .Map ;
@@ -26,12 +26,19 @@ public enum SimpleDocumentFactoryImpl implements DocumentFactory {
2626
2727 private static final Document EMPTY_DOCUMENT = new EmptyDocumentImpl ();
2828
29+ /**
30+ * Returns a singleton instance of this SimpleDocumentFactoryImpl.
31+ *
32+ * @return - singleton instance of SimpleDocumentFactoryImpl
33+ */
2934 public static DocumentFactory getFactory () {
3035 return INSTANCE ;
3136 }
3237
3338 /* (non-Javadoc)
3439 * @see com._4point.aem.fluentforms.api.DocumentFactory#create(byte[])
40+ *
41+ * It holds a defensive copy of the array in memory.
3542 */
3643 @ Override
3744 public Document create (byte [] data ) {
@@ -42,6 +49,7 @@ public Document create(byte[] data) {
4249 * @see com._4point.aem.fluentforms.api.DocumentFactory#create(java.io.File, boolean)
4350 */
4451 @ Override
52+ @ Deprecated
4553 public Document create (File file , boolean ownFile ) {
4654 return new SimpleDocumentImpl (file , ownFile );
4755 }
@@ -50,6 +58,7 @@ public Document create(File file, boolean ownFile) {
5058 * @see com._4point.aem.fluentforms.api.DocumentFactory#create(java.io.File)
5159 */
5260 @ Override
61+ @ Deprecated
5362 public Document create (File file ) {
5463 return new SimpleDocumentImpl (file );
5564 }
@@ -127,20 +136,19 @@ private SimpleDocumentImpl(byte[] data) {
127136
128137 private SimpleDocumentImpl (File file , boolean ownFile ) {
129138 // We ignore the ownFile parameter because we don't care. We are going to just read it in anyway.
130- try {
131- this .inlineData = readToByteArray (new FileInputStream (file ));
132- } catch (FileNotFoundException e ) {
133- // Convert to runtime exception.
134- throw new IllegalArgumentException ("File not found. (" + file .toString () + ")." , e );
135- }
139+ this (file .toPath ());
136140 }
137141
138142 private SimpleDocumentImpl (File file ) {
143+ this (file .toPath ());
144+ }
145+
146+ private SimpleDocumentImpl (Path path ) {
139147 try {
140- this .inlineData = readToByteArray ( new FileInputStream ( file ) );
141- } catch (FileNotFoundException e ) {
148+ this .inlineData = Files . readAllBytes ( path );
149+ } catch (IOException e ) {
142150 // Convert to runtime exception.
143- throw new IllegalArgumentException ( "File not found. (" + file .toString () + ")." , e );
151+ throw new UncheckedIOException ( "Error reading file (" + path .toString () + ")." , e );
144152 }
145153 }
146154
0 commit comments