11package org .bouncycastle .tsp .ers ;
22
3- import java .io .ByteArrayOutputStream ;
43import java .io .File ;
54import java .io .FileInputStream ;
65import java .io .FileNotFoundException ;
7- import java .io .InputStream ;
86import java .io .IOException ;
7+ import java .io .InputStream ;
98
109import org .bouncycastle .operator .DigestCalculator ;
1110import org .bouncycastle .util .io .Streams ;
1615public class ERSInputStreamData
1716 extends ERSCachingData
1817{
19- private final InputStream content ;
18+ private final File contentFile ;
19+ private final byte [] contentBytes ;
2020
2121 public ERSInputStreamData (File content )
2222 throws FileNotFoundException
@@ -25,25 +25,47 @@ public ERSInputStreamData(File content)
2525 {
2626 throw new IllegalArgumentException ("directory not allowed" );
2727 }
28- this .content = new FileInputStream (content );
28+ if (!content .exists ())
29+ {
30+ throw new FileNotFoundException (content + " not found" );
31+ }
32+ this .contentBytes = null ;
33+ this .contentFile = content ;
2934 }
3035
3136 public ERSInputStreamData (InputStream content )
3237 {
33- this . content = content ;
34- }
35-
36- protected ERSByteData toByteData ()
37- throws IOException
38- {
39- ByteArrayOutputStream baos = new ByteArrayOutputStream ( );
40- Streams . pipeAll ( this . content , baos );
41- return new ERSByteData ( baos . toByteArray ()) ;
38+ try
39+ {
40+ this . contentBytes = Streams . readAll ( content );
41+ }
42+ catch ( IOException e )
43+ {
44+ throw ExpUtil . createIllegalState ( "unable to open content: " + e . getMessage (), e );
45+ }
46+ this . contentFile = null ;
4247 }
4348
4449 protected byte [] calculateHash (DigestCalculator digestCalculator , byte [] previousChainHash )
4550 {
46- byte [] hash = ERSUtil .calculateDigest (digestCalculator , content );
51+ byte [] hash ;
52+ if (contentBytes != null )
53+ {
54+ hash = ERSUtil .calculateDigest (digestCalculator , contentBytes );
55+ }
56+ else
57+ {
58+ try
59+ {
60+ InputStream content = new FileInputStream (contentFile );
61+ hash = ERSUtil .calculateDigest (digestCalculator , content );
62+ content .close ();
63+ }
64+ catch (IOException e )
65+ {
66+ throw ExpUtil .createIllegalState ("unable to open content: " + e .getMessage (), e );
67+ }
68+ }
4769
4870 if (previousChainHash != null )
4971 {
0 commit comments