001    /*
002     * Copyright (C) 2007 The Guava Authors
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package com.google.common.io;
018    
019    import static com.google.common.base.Preconditions.checkArgument;
020    import static com.google.common.base.Preconditions.checkNotNull;
021    
022    import com.google.common.annotations.Beta;
023    import com.google.common.base.Charsets;
024    
025    import java.io.IOException;
026    import java.io.InputStream;
027    import java.io.InputStreamReader;
028    import java.io.OutputStream;
029    import java.net.URL;
030    import java.nio.charset.Charset;
031    import java.util.List;
032    
033    /**
034     * Provides utility methods for working with resources in the classpath.
035     * Note that even though these methods use {@link URL} parameters, they
036     * are usually not appropriate for HTTP or other non-classpath resources.
037     *
038     * <p>All method parameters must be non-null unless documented otherwise.
039     *
040     * @author Chris Nokleberg
041     * @author Ben Yu
042     * @since 1.0
043     */
044    @Beta
045    public final class Resources {
046      private Resources() {}
047    
048      /**
049       * Returns a factory that will supply instances of {@link InputStream} that
050       * read from the given URL.
051       *
052       * @param url the URL to read from
053       * @return the factory
054       */
055      public static InputSupplier<InputStream> newInputStreamSupplier(
056          final URL url) {
057        checkNotNull(url);
058        return new InputSupplier<InputStream>() {
059          public InputStream getInput() throws IOException {
060            return url.openStream();
061          }
062        };
063      }
064    
065      /**
066       * Returns a factory that will supply instances of
067       * {@link InputStreamReader} that read a URL using the given character set.
068       *
069       * @param url the URL to read from
070       * @param charset the charset used to decode the input stream; see {@link
071       *     Charsets} for helpful predefined constants
072       * @return the factory
073       */
074      public static InputSupplier<InputStreamReader> newReaderSupplier(
075          URL url, Charset charset) {
076        return CharStreams.newReaderSupplier(newInputStreamSupplier(url), charset);
077      }
078    
079      /**
080       * Reads all bytes from a URL into a byte array.
081       *
082       * @param url the URL to read from
083       * @return a byte array containing all the bytes from the URL
084       * @throws IOException if an I/O error occurs
085       */
086      public static byte[] toByteArray(URL url) throws IOException {
087        return ByteStreams.toByteArray(newInputStreamSupplier(url));
088      }
089    
090      /**
091       * Reads all characters from a URL into a {@link String}, using the given
092       * character set.
093       *
094       * @param url the URL to read from
095       * @param charset the charset used to decode the input stream; see {@link
096       *     Charsets} for helpful predefined constants
097       * @return a string containing all the characters from the URL
098       * @throws IOException if an I/O error occurs.
099       */
100      public static String toString(URL url, Charset charset) throws IOException {
101        return CharStreams.toString(newReaderSupplier(url, charset));
102      }
103    
104      /**
105       * Streams lines from a URL, stopping when our callback returns false, or we
106       * have read all of the lines.
107       *
108       * @param url the URL to read from
109       * @param charset the charset used to decode the input stream; see {@link
110       *     Charsets} for helpful predefined constants
111       * @param callback the LineProcessor to use to handle the lines
112       * @return the output of processing the lines
113       * @throws IOException if an I/O error occurs
114       */
115      public static <T> T readLines(URL url, Charset charset,
116          LineProcessor<T> callback) throws IOException {
117        return CharStreams.readLines(newReaderSupplier(url, charset), callback);
118      }
119    
120      /**
121       * Reads all of the lines from a URL. The lines do not include
122       * line-termination characters, but do include other leading and trailing
123       * whitespace.
124       *
125       * @param url the URL to read from
126       * @param charset the charset used to decode the input stream; see {@link
127       *     Charsets} for helpful predefined constants
128       * @return a mutable {@link List} containing all the lines
129       * @throws IOException if an I/O error occurs
130       */
131      public static List<String> readLines(URL url, Charset charset)
132          throws IOException {
133        return CharStreams.readLines(newReaderSupplier(url, charset));
134      }
135    
136      /**
137       * Copies all bytes from a URL to an output stream.
138       *
139       * @param from the URL to read from
140       * @param to the output stream
141       * @throws IOException if an I/O error occurs
142       */
143      public static void copy(URL from, OutputStream to) throws IOException {
144        ByteStreams.copy(newInputStreamSupplier(from), to);
145      }
146      
147      /**
148       * Returns a {@code URL} pointing to {@code resourceName} if the resource is
149       * found in the class path. {@code Resources.class.getClassLoader()} is used
150       * to locate the resource.
151       * 
152       * @throws IllegalArgumentException if resource is not found
153       */
154      public static URL getResource(String resourceName) {
155        URL url = Resources.class.getClassLoader().getResource(resourceName);
156        checkArgument(url != null, "resource %s not found.", resourceName);
157        return url;
158      }
159    
160      /**
161       * Returns a {@code URL} pointing to {@code resourceName} that is relative to
162       * {@code contextClass}, if the resource is found in the class path. 
163       * 
164       * @throws IllegalArgumentException if resource is not found
165       */
166      public static URL getResource(Class<?> contextClass, String resourceName) {
167        URL url = contextClass.getResource(resourceName);
168        checkArgument(url != null, "resource %s relative to %s not found.",
169            resourceName, contextClass.getName());
170        return url;
171      }
172    }