KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > api > metadata > common > domain > util > StreamUtils


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.api.metadata.common.domain.util;
22
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.sql.Blob JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 import com.jaspersoft.jasperserver.api.JSException;
33 import com.jaspersoft.jasperserver.api.JSExceptionWrapper;
34 import com.jaspersoft.jasperserver.api.metadata.common.domain.FileResourceData;
35
36 /**
37  * @author Lucian Chirita (lucianc@users.sourceforge.net)
38  * @version $Id: StreamUtils.java 3089 2006-04-14 14:13:09Z lucian $
39  */

40 public class StreamUtils {
41     private static final Log log = LogFactory.getLog(FileResourceData.class);
42     private static final int READ_STREAM_BUFFER_SIZE = 10000;
43
44     public static byte[] readData(InputStream JavaDoc is) {
45         if (is == null) {
46             return null;
47         }
48         
49         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
50
51         byte[] bytes = new byte[READ_STREAM_BUFFER_SIZE];
52         int ln = 0;
53         try {
54             while ((ln = is.read(bytes)) > 0) {
55                 baos.write(bytes, 0, ln);
56             }
57         } catch (IOException JavaDoc e) {
58             log.error("Error while reading data.", e);
59             throw new JSExceptionWrapper(e);
60         }
61
62         return baos.toByteArray();
63     }
64
65     public static byte[] readData(InputStream JavaDoc is, int size) {
66         if (is == null) {
67             return null;
68         }
69
70         try {
71             byte[] data = new byte[size];
72             int offset = 0;
73             while (size > 0) {
74                 int read = is.read(data, offset, size);
75                 if (read < 0) {
76                     throw new JSException("Input stream exhausted before reading " + size + " bytes");
77                 }
78                 offset += read;
79                 size -= read;
80             }
81             return data;
82         } catch (IOException JavaDoc e) {
83             log.error("Error while reading data.", e);
84             throw new JSExceptionWrapper(e);
85         }
86     }
87
88     public static byte[] readData(Blob JavaDoc blob) {
89         if (blob == null) {
90             return null;
91         }
92
93         try {
94             return readData(blob.getBinaryStream());
95         } catch (SQLException JavaDoc e) {
96             log.error("Error while reading blob data", e);
97             throw new JSExceptionWrapper(e);
98         }
99     }
100
101 }
102
Popular Tags