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    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
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; // delete content
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         // not implemented
138     }
139 }