This project has retired. For details please refer to its Attic page.
ContentStreamImpl xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.commons.impl.dataobjects;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.InputStream;
23  import java.io.UnsupportedEncodingException;
24  import java.math.BigInteger;
25  
26  import org.apache.chemistry.opencmis.commons.data.ContentStream;
27  
28  /**
29   * Content stream data implementation.
30   */
31  public class ContentStreamImpl extends AbstractExtensionData implements ContentStream {
32  
33      private static final long serialVersionUID = 1L;
34  
35      private String filename;
36      private BigInteger length;
37      private String mimeType;
38      private transient InputStream stream;
39  
40      /**
41       * Constructor.
42       */
43      public ContentStreamImpl() {
44      }
45  
46      /**
47       * Constructor.
48       */
49      public ContentStreamImpl(String filename, BigInteger length, String mimetype, InputStream stream) {
50          setLength(length);
51          setMimeType(mimetype);
52          setFileName(filename);
53          setStream(stream);
54      }
55  
56      /**
57       * Convenience constructor for tests.
58       */
59      public ContentStreamImpl(String filename, String mimetype, String string) {
60          byte[] bytes;
61          try {
62              bytes = string.getBytes("UTF-8");
63          } catch (UnsupportedEncodingException e) {
64              // cannot happen
65              bytes = string.getBytes();
66          }
67          setLength(BigInteger.valueOf(bytes.length));
68          setMimeType(mimetype);
69          setFileName(filename);
70          setStream(new ByteArrayInputStream(bytes));
71      }
72  
73      public String getFileName() {
74          return filename;
75      }
76  
77      public void setFileName(String filename) {
78          this.filename = filename;
79      }
80  
81      public long getLength() {
82          return length == null ? -1 : length.longValue();
83      }
84  
85      public BigInteger getBigLength() {
86          return length;
87      }
88  
89      public void setLength(BigInteger length) {
90          this.length = length;
91      }
92  
93      public String getMimeType() {
94          return mimeType;
95      }
96  
97      public void setMimeType(String mimeType) {
98          this.mimeType = mimeType;
99      }
100 
101     public InputStream getStream() {
102         return stream;
103     }
104 
105     public void setStream(InputStream stream) {
106         this.stream = stream;
107     }
108 
109     @Override
110     public String toString() {
111         return "ContentStream [filename=" + filename + ", length=" + length + ", MIME type=" + mimeType
112                 + ", has stream=" + (stream != null) + "]" + super.toString();
113     }
114 }