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;
024import com.google.common.base.MoreObjects;
025import com.google.common.collect.Lists;
026
027import java.io.IOException;
028import java.io.InputStream;
029import java.io.OutputStream;
030import java.net.URL;
031import java.nio.charset.Charset;
032import java.util.List;
033
034/**
035 * Provides utility methods for working with resources in the classpath.
036 * Note that even though these methods use {@link URL} parameters, they
037 * are usually not appropriate for HTTP or other non-classpath resources.
038 *
039 * <p>All method parameters must be non-null unless documented otherwise.
040 *
041 * @author Chris Nokleberg
042 * @author Ben Yu
043 * @author Colin Decker
044 * @since 1.0
045 */
046@Beta
047public final class Resources {
048  private Resources() {}
049
050  /**
051   * Returns a {@link ByteSource} that reads from the given URL.
052   *
053   * @since 14.0
054   */
055  public static ByteSource asByteSource(URL url) {
056    return new UrlByteSource(url);
057  }
058
059  /**
060   * A byte source that reads from a URL using {@link URL#openStream()}.
061   */
062  private static final class UrlByteSource extends ByteSource {
063
064    private final URL url;
065
066    private UrlByteSource(URL url) {
067      this.url = checkNotNull(url);
068    }
069
070    @Override
071    public InputStream openStream() throws IOException {
072      return url.openStream();
073    }
074
075    @Override
076    public String toString() {
077      return "Resources.asByteSource(" + url + ")";
078    }
079  }
080
081  /**
082   * Returns a {@link CharSource} that reads from the given URL using the given
083   * character set.
084   *
085   * @since 14.0
086   */
087  public static CharSource asCharSource(URL url, Charset charset) {
088    return asByteSource(url).asCharSource(charset);
089  }
090
091  /**
092   * Reads all bytes from a URL into a byte array.
093   *
094   * @param url the URL to read from
095   * @return a byte array containing all the bytes from the URL
096   * @throws IOException if an I/O error occurs
097   */
098  public static byte[] toByteArray(URL url) throws IOException {
099    return asByteSource(url).read();
100  }
101
102  /**
103   * Reads all characters from a URL into a {@link String}, using the given
104   * character set.
105   *
106   * @param url the URL to read from
107   * @param charset the charset used to decode the input stream; see {@link
108   *     Charsets} for helpful predefined constants
109   * @return a string containing all the characters from the URL
110   * @throws IOException if an I/O error occurs.
111   */
112  public static String toString(URL url, Charset charset) throws IOException {
113    return asCharSource(url, charset).read();
114  }
115
116  /**
117   * Streams lines from a URL, stopping when our callback returns false, or we
118   * have read all of the lines.
119   *
120   * @param url the URL to read from
121   * @param charset the charset used to decode the input stream; see {@link
122   *     Charsets} for helpful predefined constants
123   * @param callback the LineProcessor to use to handle the lines
124   * @return the output of processing the lines
125   * @throws IOException if an I/O error occurs
126   */
127  public static <T> T readLines(URL url, Charset charset,
128      LineProcessor<T> callback) throws IOException {
129    return asCharSource(url, charset).readLines(callback);
130  }
131
132  /**
133   * Reads all of the lines from a URL. The lines do not include
134   * line-termination characters, but do include other leading and trailing
135   * whitespace.
136   *
137   * <p>This method returns a mutable {@code List}. For an
138   * {@code ImmutableList}, use
139   * {@code Resources.asCharSource(url, charset).readLines()}.
140   *
141   * @param url the URL to read from
142   * @param charset the charset used to decode the input stream; see {@link
143   *     Charsets} for helpful predefined constants
144   * @return a mutable {@link List} containing all the lines
145   * @throws IOException if an I/O error occurs
146   */
147  public static List<String> readLines(URL url, Charset charset)
148      throws IOException {
149    // don't use asCharSource(url, charset).readLines() because that returns
150    // an immutable list, which would change the behavior of this method
151    return readLines(url, charset, new LineProcessor<List<String>>() {
152      final List<String> result = Lists.newArrayList();
153
154      @Override
155      public boolean processLine(String line) {
156        result.add(line);
157        return true;
158      }
159
160      @Override
161      public List<String> getResult() {
162        return result;
163      }
164    });
165  }
166
167  /**
168   * Copies all bytes from a URL to an output stream.
169   *
170   * @param from the URL to read from
171   * @param to the output stream
172   * @throws IOException if an I/O error occurs
173   */
174  public static void copy(URL from, OutputStream to) throws IOException {
175    asByteSource(from).copyTo(to);
176  }
177  
178  /**
179   * Returns a {@code URL} pointing to {@code resourceName} if the resource is
180   * found using the {@linkplain Thread#getContextClassLoader() context class
181   * loader}. In simple environments, the context class loader will find
182   * resources from the class path. In environments where different threads can
183   * have different class loaders, for example app servers, the context class
184   * loader will typically have been set to an appropriate loader for the
185   * current thread.
186   *
187   * <p>In the unusual case where the context class loader is null, the class
188   * loader that loaded this class ({@code Resources}) will be used instead.
189   * 
190   * @throws IllegalArgumentException if the resource is not found
191   */
192  public static URL getResource(String resourceName) {
193    ClassLoader loader = MoreObjects.firstNonNull(
194        Thread.currentThread().getContextClassLoader(),
195        Resources.class.getClassLoader());
196    URL url = loader.getResource(resourceName);
197    checkArgument(url != null, "resource %s not found.", resourceName);
198    return url;
199  }
200
201  /**
202   * Given a {@code resourceName} that is relative to {@code contextClass},
203   * returns a {@code URL} pointing to the named resource.
204   * 
205   * @throws IllegalArgumentException if the resource is not found
206   */
207  public static URL getResource(Class<?> contextClass, String resourceName) {
208    URL url = contextClass.getResource(resourceName);
209    checkArgument(url != null, "resource %s relative to %s not found.",
210        resourceName, contextClass.getName());
211    return url;
212  }
213}