View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.surf.util;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.InputStreamReader;
25  import java.io.Reader;
26  import java.io.Serializable;
27  
28  import org.springframework.util.FileCopyUtils;
29  
30  
31  /**
32   * Input Stream based Content
33   */
34  public class InputStreamContent implements Content, Serializable
35  {
36      private static final long serialVersionUID = -7729633986840536282L;
37      
38      private InputStream stream;    
39      private String mimetype;
40      private String encoding;
41      
42      /** cached result - to ensure we only read it once */
43      private String content;
44      
45      
46      /**
47       * Constructor
48       * 
49       * @param stream    content input stream
50       * @param mimetype  content mimetype
51       */
52      public InputStreamContent(InputStream stream, String mimetype, String encoding)
53      {
54          this.stream = stream;
55          this.mimetype = mimetype;
56          this.encoding = encoding;
57      }
58  
59      /* (non-Javadoc)
60       * @see org.springframework.extensions.surf.util.Content#getContent()
61       */
62      public String getContent()
63          throws IOException
64      {
65          // ensure we only try to read the content once - as this method may be called several times
66          // but the inputstream can only be processed a single time
67          if (this.content == null)
68          {
69              ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
70              FileCopyUtils.copy(stream, os);  // both streams are closed
71              byte[] bytes = os.toByteArray();
72              // get the encoding for the string
73              String encoding = getEncoding();
74              // create the string from the byte[] using encoding if necessary
75              this.content = (encoding == null) ? new String(bytes) : new String(bytes, encoding);
76          }
77          return this.content;
78      }
79      
80      /* (non-Javadoc)
81       * @see org.springframework.extensions.surf.util.Content#getInputStream()
82       */
83      public InputStream getInputStream()
84      {
85          return stream;
86      }
87  
88      
89      /* (non-Javadoc)
90       * @see org.springframework.extensions.surf.util.Content#getReader()
91       */
92      public Reader getReader()
93          throws IOException
94      {
95          return (encoding == null) ? new InputStreamReader(stream) : new InputStreamReader(stream, encoding);
96      }
97      
98      /* (non-Javadoc)
99       * @see org.springframework.extensions.surf.util.Content#getSize()
100      */
101     public long getSize()
102     {
103         return -1;
104     }
105     
106     /* (non-Javadoc)
107      * @see org.springframework.extensions.surf.util.Content#getMimetype()
108      */
109     public String getMimetype()
110     {
111         return mimetype;
112     }
113     
114     /* (non-Javadoc)
115      * @see org.springframework.extensions.surf.util.Content#getEncoding()
116      */
117     public String getEncoding()
118     {
119         return encoding;
120     }
121 
122 }