View Javadoc

1   /*
2    *  Copyright 2011 Hippo.
3    * 
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    * 
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.onehippo.forge.cmisreplication.util;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.Serializable;
21  
22  import javax.jcr.Binary;
23  import javax.jcr.RepositoryException;
24  
25  import org.apache.chemistry.opencmis.client.api.Document;
26  import org.apache.chemistry.opencmis.commons.data.ContentStream;
27  import org.apache.commons.io.IOUtils;
28  
29  public class CmisDocumentBinary implements Binary, Serializable {
30      
31      private static final long serialVersionUID = 1L;
32      
33      private Document document;
34      private ContentStream contentStream;
35      private InputStream inputStream;
36      
37      public CmisDocumentBinary(Document document) {
38          this.document = document;
39      }
40      
41      public void dispose() {
42          if (inputStream != null) {
43              IOUtils.closeQuietly(inputStream);
44              inputStream = null;
45          }
46          
47          contentStream = null;
48      }
49      
50      public long getSize() throws RepositoryException {
51          if (document != null) {
52              return document.getContentStreamLength();
53          }
54          
55          return 0;
56      }
57      
58      public InputStream getStream() throws RepositoryException {
59          if (inputStream == null) {
60              contentStream = document.getContentStream();
61              inputStream = contentStream.getStream();
62          }
63          
64          return inputStream;
65      }
66      
67      public int read(byte[] b, long position) throws IOException, RepositoryException {
68          return inputStream.read(b, (int) position, b.length);
69      }
70      
71  }