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