KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > webservice > util > ContentUtils


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.webservice.util;
18
19 import java.io.File JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.net.URLConnection JavaDoc;
26
27 import org.alfresco.webservice.content.Content;
28 import org.springframework.util.FileCopyUtils;
29
30 /**
31  * Content Utils Class
32  *
33  * @author Roy Wetherall
34  */

35 public class ContentUtils
36 {
37     public static final int BUFFER_SIZE = 4096;
38     
39     /**
40      * Convert an input stream to a byte array
41      *
42      * @param inputStream the input stream
43      * @return the byte array
44      * @throws Exception
45      */

46     public static byte[] convertToByteArray(InputStream JavaDoc inputStream) throws Exception JavaDoc
47     {
48         byte[] result = null;
49         
50         if (inputStream.available() > 0)
51         {
52             result = new byte[inputStream.available()];
53             inputStream.read(result);;
54         }
55         
56         return result;
57     }
58     
59     /**
60      * Get the content from the download servlet as a string
61      *
62      * @param content the content object
63      * @return the content as a string
64      */

65     public static String JavaDoc getContentAsString(Content content)
66     {
67         // Get the url and the ticket
68
String JavaDoc ticket = AuthenticationUtils.getCurrentTicket();
69         String JavaDoc strUrl = content.getUrl() + "?ticket=" + ticket;
70         
71         StringBuilder JavaDoc readContent = new StringBuilder JavaDoc();
72         try
73         {
74             // Connect to donwload servlet
75
URL JavaDoc url = new URL JavaDoc(strUrl);
76             URLConnection JavaDoc conn = url.openConnection();
77             InputStream JavaDoc is = conn.getInputStream();
78             int read = is.read();
79             while (read != -1)
80             {
81                readContent.append((char)read);
82                read = is.read();
83             }
84         }
85         catch (Exception JavaDoc exception)
86         {
87             throw new WebServiceException("Unable to get content as string.", exception);
88         }
89         
90         // return content as a string
91
return readContent.toString();
92     }
93     
94     /**
95      * Get the content as an imput stream
96      *
97      * @param content
98      * @return
99      */

100     public static InputStream JavaDoc getContentAsInputStream(Content content)
101     {
102         // Get the url and the ticket
103
String JavaDoc ticket = AuthenticationUtils.getCurrentTicket();
104         String JavaDoc strUrl = content.getUrl() + "?ticket=" + ticket;
105  
106         try
107         {
108             // Connect to donwload servlet
109
URL JavaDoc url = new URL JavaDoc(strUrl);
110             URLConnection JavaDoc conn = url.openConnection();
111             return conn.getInputStream();
112         }
113         catch (Exception JavaDoc exception)
114         {
115             throw new WebServiceException("Unable to get content as inputStream.", exception);
116         }
117     }
118     
119     /**
120      * Copy the content into a given file.
121      *
122      * @param content the content object
123      * @param file the file
124      */

125     public static void copyContentToFile(Content content, File JavaDoc file)
126     {
127         try
128         {
129             FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(file);
130             FileCopyUtils.copy(getContentAsInputStream(content), os);
131         }
132         catch (IOException JavaDoc exception)
133         {
134             throw new WebServiceException("Unable to copy content into file.", exception);
135         }
136     }
137     
138     /**
139      * Helper method to copy from one stream to another
140      *
141      * @param in
142      * @param out
143      * @return
144      * @throws IOException
145      */

146     public static int copy(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc
147     {
148         try
149         {
150             int byteCount = 0;
151             byte[] buffer = new byte[BUFFER_SIZE];
152             int bytesRead = -1;
153             while ((bytesRead = in.read(buffer)) != -1)
154             {
155                 out.write(buffer, 0, bytesRead);
156                 byteCount += bytesRead;
157             }
158             out.flush();
159             return byteCount;
160         }
161         finally
162         {
163             try
164             {
165                 in.close();
166             }
167             catch (IOException JavaDoc ex)
168             {
169                 // Could not close input stream
170
}
171             try
172             {
173                 out.close();
174             }
175             catch (IOException JavaDoc ex)
176             {
177                 // Could not close output stream
178
}
179         }
180     }
181 }
182
Popular Tags