1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }