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
017package com.google.common.io;
018
019import static com.google.common.base.Preconditions.checkArgument;
020import static com.google.common.base.Preconditions.checkNotNull;
021
022import com.google.common.annotations.Beta;
023import com.google.common.base.Charsets;
024
025import java.io.IOException;
026import java.io.InputStream;
027import java.io.InputStreamReader;
028import java.io.OutputStream;
029import java.net.URL;
030import java.nio.charset.Charset;
031import 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 * @author Colin Decker
043 * @since 1.0
044 */
045@Beta
046public final class Resources {
047  private Resources() {}
048
049  /**
050   * Returns a factory that will supply instances of {@link InputStream} that
051   * read from the given URL.
052   *
053   * @param url the URL to read from
054   * @return the factory
055   */
056  public static InputSupplier<InputStream> newInputStreamSupplier(URL url) {
057    return ByteStreams.asInputSupplier(asByteSource(url));
058  }
059
060  /**
061   * Returns a {@link ByteSource} that reads from the given URL.
062   *
063   * @since 14.0
064   */
065  public static ByteSource asByteSource(URL url) {
066    return new UrlByteSource(url);
067  }
068
069  /**
070   * A byte source that reads from a URL using {@link URL#openStream()}.
071   */
072  private static final class UrlByteSource extends ByteSource {
073
074    private final URL url;
075
076    private UrlByteSource(URL url) {
077      this.url = checkNotNull(url);
078    }
079
080    @Override
081    public InputStream openStream() throws IOException {
082      return url.openStream();
083    }
084
085    @Override
086    public String toString() {
087      return "Resources.newByteSource(" + url + ")";
088    }
089  }
090
091  /**
092   * Returns a factory that will supply instances of
093   * {@link InputStreamReader} that read a URL using the given character set.
094   *
095   * @param url the URL to read from
096   * @param charset the charset used to decode the input stream; see {@link
097   *     Charsets} for helpful predefined constants
098   * @return the factory
099   */
100  public static InputSupplier<InputStreamReader> newReaderSupplier(
101      URL url, Charset charset) {
102    return CharStreams.asInputSupplier(asCharSource(url, charset));
103  }
104
105  /**
106   * Returns a {@link CharSource} that reads from the given URL using the given character set.
107   *
108   * @since 14.0
109   */
110  public static CharSource asCharSource(URL url, Charset charset) {
111    return asByteSource(url).asCharSource(charset);
112  }
113
114  /**
115   * Reads all bytes from a URL into a byte array.
116   *
117   * @param url the URL to read from
118   * @return a byte array containing all the bytes from the URL
119   * @throws IOException if an I/O error occurs
120   */
121  public static byte[] toByteArray(URL url) throws IOException {
122    return asByteSource(url).read();
123  }
124
125  /**
126   * Reads all characters from a URL into a {@link String}, using the given
127   * character set.
128   *
129   * @param url the URL to read from
130   * @param charset the charset used to decode the input stream; see {@link
131   *     Charsets} for helpful predefined constants
132   * @return a string containing all the characters from the URL
133   * @throws IOException if an I/O error occurs.
134   */
135  public static String toString(URL url, Charset charset) throws IOException {
136    return asCharSource(url, charset).read();
137  }
138
139  /**
140   * Streams lines from a URL, stopping when our callback returns false, or we
141   * have read all of the lines.
142   *
143   * @param url the URL to read from
144   * @param charset the charset used to decode the input stream; see {@link
145   *     Charsets} for helpful predefined constants
146   * @param callback the LineProcessor to use to handle the lines
147   * @return the output of processing the lines
148   * @throws IOException if an I/O error occurs
149   */
150  public static <T> T readLines(URL url, Charset charset,
151      LineProcessor<T> callback) throws IOException {
152    return CharStreams.readLines(newReaderSupplier(url, charset), callback);
153  }
154
155  /**
156   * Reads all of the lines from a URL. The lines do not include
157   * line-termination characters, but do include other leading and trailing
158   * whitespace.
159   *
160   * @param url the URL to read from
161   * @param charset the charset used to decode the input stream; see {@link
162   *     Charsets} for helpful predefined constants
163   * @return a mutable {@link List} containing all the lines
164   * @throws IOException if an I/O error occurs
165   */
166  public static List<String> readLines(URL url, Charset charset)
167      throws IOException {
168    return CharStreams.readLines(newReaderSupplier(url, charset));
169  }
170
171  /**
172   * Copies all bytes from a URL to an output stream.
173   *
174   * @param from the URL to read from
175   * @param to the output stream
176   * @throws IOException if an I/O error occurs
177   */
178  public static void copy(URL from, OutputStream to) throws IOException {
179    asByteSource(from).copyTo(to);
180  }
181  
182  /**
183   * Returns a {@code URL} pointing to {@code resourceName} if the resource is
184   * found in the class path. {@code Resources.class.getClassLoader()} is used
185   * to locate the resource.
186   * 
187   * @throws IllegalArgumentException if resource is not found
188   */
189  public static URL getResource(String resourceName) {
190    URL url = Resources.class.getClassLoader().getResource(resourceName);
191    checkArgument(url != null, "resource %s not found.", resourceName);
192    return url;
193  }
194
195  /**
196   * Returns a {@code URL} pointing to {@code resourceName} that is relative to
197   * {@code contextClass}, if the resource is found in the class path. 
198   * 
199   * @throws IllegalArgumentException if resource is not found
200   */
201  public static URL getResource(Class<?> contextClass, String resourceName) {
202    URL url = contextClass.getResource(resourceName);
203    checkArgument(url != null, "resource %s relative to %s not found.",
204        resourceName, contextClass.getName());
205    return url;
206  }
207}