001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of the Granite Data Services Platform.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    package org.granite.util;
023    
024    import java.io.ByteArrayInputStream;
025    import java.io.ByteArrayOutputStream;
026    import java.io.FileNotFoundException;
027    import java.io.IOException;
028    import java.io.InputStream;
029    
030    /**
031     * @author Franck WOLFF
032     */
033    public class StreamUtil {
034    
035            public static byte[] getResourceAsBytes(String path, ClassLoader loader) throws IOException {
036                    if (loader == null)
037                            loader = Thread.currentThread().getContextClassLoader();
038            InputStream is = loader.getResourceAsStream(path);
039            if (is == null)
040                throw new FileNotFoundException("Resource not found: " + path);
041    
042            ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
043            try {
044                int b = -1;
045                while ((b = is.read()) != -1)
046                    baos.write(b);
047            } finally {
048                is.close();
049            }
050            return baos.toByteArray();
051        }
052            
053        public static ByteArrayInputStream getResourceAsStream(String path, ClassLoader loader) throws IOException {
054            return new ByteArrayInputStream(getResourceAsBytes(path, loader));
055        }
056    
057        public static String getResourceAsString(String path, ClassLoader loader) throws IOException {
058            return new String(getResourceAsBytes(path, loader));
059        }
060    
061        public static String getStreamAsString(InputStream is) throws IOException {
062            if (is == null)
063                return null;
064    
065            ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
066            try {
067                int b = -1;
068                while ((b = is.read()) != -1)
069                    baos.write(b);
070            } finally {
071                is.close();
072            }
073            return new String(baos.toByteArray());
074        }
075    }