This project has retired. For details please refer to its
Attic page.
ContentStreamDataImpl xref
1 package org.apache.chemistry.opencmis.inmemory.storedobj.impl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.math.BigInteger;
29 import java.util.List;
30
31 import org.apache.chemistry.opencmis.commons.data.CmisExtensionElement;
32 import org.apache.chemistry.opencmis.commons.data.ContentStream;
33 import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
34
35 public class ContentStreamDataImpl implements ContentStream {
36
37 private int fLength;
38
39 private String fMimeType;
40
41 private String fFileName;
42
43 private byte[] fContent;
44
45 private long fStreamLimitOffset;
46
47 private long fStreamLimitLength;
48
49 private final long sizeLimitKB;
50
51 public ContentStreamDataImpl(long maxAllowedContentSizeKB) {
52 sizeLimitKB = maxAllowedContentSizeKB;
53 }
54
55 public void setContent(InputStream in) throws IOException {
56 fStreamLimitOffset = fStreamLimitLength = -1;
57 if (null == in) {
58 fContent = null;
59 fLength = 0;
60 } else {
61 byte[] buffer = new byte[0xFFFF];
62 ByteArrayOutputStream contentStream = new ByteArrayOutputStream();
63 for (int len = 0; (len = in.read(buffer)) != -1;) {
64 contentStream.write(buffer, 0, len);
65 fLength += len;
66 if (sizeLimitKB > 0 && fLength > sizeLimitKB * 1024) {
67 throw new CmisInvalidArgumentException("Content size exceeds max. allowed size of " + sizeLimitKB
68 + "KB.");
69 }
70 }
71 fContent = contentStream.toByteArray();
72 fLength = contentStream.size();
73 contentStream.close();
74 in.close();
75 }
76 }
77
78 public long getLength() {
79 return fLength;
80 }
81
82 public BigInteger getBigLength() {
83 return BigInteger.valueOf(fLength);
84 }
85
86 public String getMimeType() {
87 return fMimeType;
88 }
89
90 public void setMimeType(String fMimeType) {
91 this.fMimeType = fMimeType;
92 }
93
94 public String getFileName() {
95 return fFileName;
96 }
97
98 public void setFileName(String fileName) {
99 this.fFileName = fileName;
100 }
101
102 public String getFilename() {
103 return fFileName;
104 }
105
106 public InputStream getStream() {
107 if (null == fContent) {
108 return null;
109 } else if (fStreamLimitOffset <= 0 && fStreamLimitLength < 0) {
110 return new ByteArrayInputStream(fContent);
111 } else {
112 return new ByteArrayInputStream(fContent, (int) (fStreamLimitOffset < 0 ? 0 : fStreamLimitOffset),
113 (int) (fStreamLimitLength < 0 ? fLength : fStreamLimitLength));
114 }
115 }
116
117 public ContentStream getCloneWithLimits(long offset, long length) {
118 ContentStreamDataImpl clone = new ContentStreamDataImpl(0);
119 clone.fFileName = fFileName;
120 clone.fLength = fLength;
121 clone.fContent = fContent;
122 clone.fMimeType = fMimeType;
123 clone.fStreamLimitOffset = offset;
124 clone.fStreamLimitLength = length;
125 return clone;
126 }
127
128 public final byte[] getBytes() {
129 return fContent;
130 }
131
132 public List<CmisExtensionElement> getExtensions() {
133 return null;
134 }
135
136 public void setExtensions(List<CmisExtensionElement> extensions) {
137
138 }
139 }