View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.common.content;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  
24  import org.mycore.datamodel.ifs.MCRContentInputStream;
25  
26  /**
27   * Reads MCRContent from an input stream. Typically, this content is not reusable, so that
28   * content can only be read once. Please be aware that an instance of this object contains an 
29   * open input stream. Thus one has to invoke {@link MCRStreamContent#getInputStream()}.close() when 
30   * finished with this object.
31   * 
32   * @author Frank L\u00FCtzenkichen
33   */
34  public class MCRStreamContent extends MCRContent {
35  
36      private InputStream in;
37  
38      public MCRStreamContent(InputStream in) {
39          if (in == null) {
40              throw new NullPointerException("Cannot instantiate MCRStreamContent without InputStream.");
41          }
42          this.in = in;
43      }
44  
45      /**
46       * @param systemId the systemID of this stream
47       */
48      public MCRStreamContent(InputStream in, String systemId) {
49          this(in);
50          setSystemId(systemId);
51      }
52  
53      /**
54       * @param systemId the systemID of this stream
55       */
56      public MCRStreamContent(InputStream in, String systemId, String docType) {
57          this(in, systemId);
58          super.docType = docType;
59      }
60  
61      @Override
62      public InputStream getInputStream() {
63          return in;
64      }
65  
66      @Override
67      public MCRContentInputStream getContentInputStream() throws IOException {
68          if (!(in instanceof MCRContentInputStream)) {
69              in = super.getContentInputStream();
70          }
71          return (MCRContentInputStream) in;
72      }
73  
74      /**
75       * Returns false, because input streams can only be read once. 
76       * Use getReusableCopy() to circumvent this.
77       */
78      @Override
79      public boolean isReusable() {
80          return false;
81      }
82  }