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.BufferedInputStream;
024    import java.io.BufferedReader;
025    import java.io.ByteArrayOutputStream;
026    import java.io.File;
027    import java.io.FileInputStream;
028    import java.io.IOException;
029    import java.io.InputStream;
030    import java.io.InputStreamReader;
031    import java.net.JarURLConnection;
032    import java.net.URI;
033    import java.net.URL;
034    import java.net.URLConnection;
035    import java.nio.charset.Charset;
036    import java.util.jar.JarEntry;
037    
038    /**
039     * @author Franck WOLFF
040     */
041    public class URIUtil {
042    
043        public static final String CLASSPATH_SCHEME = "class";
044    
045        public static String normalize(String uri) {
046            
047            if (uri == null)
048                    return null;
049            
050            if (File.separatorChar == '\\' && uri.startsWith("file://"))
051                    uri = uri.replace('\\', '/');
052    
053            uri = uri.replace(" ", "%20");
054            
055            return uri;
056        }
057        
058        public static InputStream getInputStream(URI uri, ClassLoader loader) throws IOException {
059            InputStream is = null;
060    
061            String scheme = uri.getScheme();
062            if (CLASSPATH_SCHEME.equals(scheme)) {
063                    if (loader != null)
064                            is = loader.getResourceAsStream(uri.getSchemeSpecificPart());
065                    if (is == null) {
066                            is = Thread.currentThread().getContextClassLoader().getResourceAsStream(uri.getSchemeSpecificPart());
067                        if (is == null)
068                            throw new IOException("Resource not found exception: " + uri);
069                    }
070            }
071            else if (scheme == null || scheme.length() <= 1) // scheme.length() == 1 -> assume drive letter.
072                is = new FileInputStream(uri.toString());
073            else
074                is = uri.toURL().openStream();
075    
076            return is;
077        }
078    
079        public static InputStream getInputStream(URI uri) throws IOException {
080            return getInputStream(uri, null);
081        }
082    
083        public static String getContentAsString(URI uri) throws IOException {
084            return getContentAsString(uri, Charset.defaultCharset());
085        }
086        public static String getContentAsString(URI uri, Charset charset) throws IOException {
087            InputStream is = null;
088            try {
089                is = getInputStream(uri);
090                BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset));
091    
092                StringBuilder sb = new StringBuilder(1024);
093    
094                char[] chars = new char[256];
095                int count = -1;
096                while ((count = reader.read(chars)) != -1)
097                    sb.append(chars, 0, count);
098    
099                return sb.toString();
100            } finally {
101                if (is != null)
102                    is.close();
103            }
104        }
105    
106        public static byte[] getContentAsBytes(URI uri) throws IOException {
107            InputStream is = null;
108            try {
109                is = new BufferedInputStream(getInputStream(uri));
110    
111                ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
112    
113                int b = 0;
114                while ((b = is.read()) != -1)
115                    baos.write(b);
116    
117                return baos.toByteArray();
118            } finally {
119                if (is != null)
120                    is.close();
121            }
122        }
123    
124        public static long lastModified(URI uri) throws IOException {
125            if (uri == null)
126                return -1L;
127    
128            String scheme = uri.getScheme();
129            if (scheme == null || scheme.length() <= 1)
130                return new File(uri).lastModified();
131            return lastModified(uri.toURL());
132        }
133    
134        public static long lastModified(URL url) throws IOException {
135            long lastModified = -1L;
136    
137            if (url != null) {
138                URLConnection connection = url.openConnection();
139                if (connection instanceof JarURLConnection) {
140                    JarEntry entry = ((JarURLConnection)connection).getJarEntry();
141                    if (entry != null)
142                        lastModified = entry.getTime();
143                }
144                if (lastModified == -1L)
145                    lastModified = connection.getLastModified();
146            }
147    
148            return (lastModified == 0L ? -1L : lastModified);
149        }
150    }