Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2007 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.io;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 
 import java.net.URL;
 import java.util.List;

Provides utility methods for working with resources in the classpath. Note that even though these methods use java.net.URL parameters, they are usually not appropriate for HTTP or other non-classpath resources.

All method parameters must be non-null unless documented otherwise.

Author(s):
Chris Nokleberg
Ben Yu
Colin Decker
Since:
1.0
 
 public final class Resources {
   private Resources() {}

  
Returns a factory that will supply instances of java.io.InputStream that read from the given URL.

Parameters:
url the URL to read from
Returns:
the factory
 
   public static InputSupplier<InputStreamnewInputStreamSupplier(URL url) {
     return ByteStreams.asInputSupplier(asByteSource(url));
   }

  
Returns a ByteSource that reads from the given URL.

Since:
14.0
 
   public static ByteSource asByteSource(URL url) {
     return new UrlByteSource(url);
   }

  
A byte source that reads from a URL using java.net.URL.openStream().
 
   private static final class UrlByteSource extends ByteSource {
 
     private final URL url;
 
     private UrlByteSource(URL url) {
       this. = checkNotNull(url);
     }
 
     @Override
     public InputStream openStream() throws IOException {
       return .openStream();
     }
 
     @Override
     public String toString() {
       return "Resources.asByteSource(" +  + ")";
     }
   }

  
Returns a factory that will supply instances of java.io.InputStreamReader that read a URL using the given character set.

Parameters:
url the URL to read from
charset the charset used to decode the input stream; see com.google.common.base.Charsets for helpful predefined constants
Returns:
the factory
      URL urlCharset charset) {
    return CharStreams.asInputSupplier(asCharSource(urlcharset));
  }

  
Returns a CharSource that reads from the given URL using the given character set.

Since:
14.0
  public static CharSource asCharSource(URL urlCharset charset) {
    return asByteSource(url).asCharSource(charset);
  }

  
Reads all bytes from a URL into a byte array.

Parameters:
url the URL to read from
Returns:
a byte array containing all the bytes from the URL
Throws:
java.io.IOException if an I/O error occurs
  public static byte[] toByteArray(URL urlthrows IOException {
    return asByteSource(url).read();
  }

  
Reads all characters from a URL into a java.lang.String, using the given character set.

Parameters:
url the URL to read from
charset the charset used to decode the input stream; see com.google.common.base.Charsets for helpful predefined constants
Returns:
a string containing all the characters from the URL
Throws:
java.io.IOException if an I/O error occurs.
  public static String toString(URL urlCharset charsetthrows IOException {
    return asCharSource(urlcharset).read();
  }

  
Streams lines from a URL, stopping when our callback returns false, or we have read all of the lines.

Parameters:
url the URL to read from
charset the charset used to decode the input stream; see com.google.common.base.Charsets for helpful predefined constants
callback the LineProcessor to use to handle the lines
Returns:
the output of processing the lines
Throws:
java.io.IOException if an I/O error occurs
  public static <T> T readLines(URL urlCharset charset,
      LineProcessor<T> callbackthrows IOException {
    return CharStreams.readLines(newReaderSupplier(urlcharset), callback);
  }

  
Reads all of the lines from a URL. The lines do not include line-termination characters, but do include other leading and trailing whitespace.

This method returns a mutable List. For an ImmutableList, use Resources.asCharSource(url, charset).readLines().

Parameters:
url the URL to read from
charset the charset used to decode the input stream; see com.google.common.base.Charsets for helpful predefined constants
Returns:
a mutable java.util.List containing all the lines
Throws:
java.io.IOException if an I/O error occurs
  public static List<StringreadLines(URL urlCharset charset)
      throws IOException {
    // don't use asCharSource(url, charset).readLines() because that returns
    // an immutable list, which would change the behavior of this method
    return readLines(urlcharsetnew LineProcessor<List<String>>() {
      final List<Stringresult = Lists.newArrayList();
      @Override
      public boolean processLine(String line) {
        .add(line);
        return true;
      }
      @Override
      public List<StringgetResult() {
        return ;
      }
    });
  }

  
Copies all bytes from a URL to an output stream.

Parameters:
from the URL to read from
to the output stream
Throws:
java.io.IOException if an I/O error occurs
  public static void copy(URL fromOutputStream tothrows IOException {
    asByteSource(from).copyTo(to);
  }
  
  
Returns a URL pointing to resourceName if the resource is found using the context class loader. In simple environments, the context class loader will find resources from the class path. In environments where different threads can have different class loaders, for example app servers, the context class loader will typically have been set to an appropriate loader for the current thread.

In the unusual case where the context class loader is null, the class loader that loaded this class (Resources) will be used instead.

Throws:
java.lang.IllegalArgumentException if the resource is not found
  public static URL getResource(String resourceName) {
    ClassLoader loader = Objects.firstNonNull(
        Thread.currentThread().getContextClassLoader(),
        Resources.class.getClassLoader());
    URL url = loader.getResource(resourceName);
    checkArgument(url != null"resource %s not found."resourceName);
    return url;
  }

  
Given a resourceName that is relative to contextClass, returns a URL pointing to the named resource.

Throws:
java.lang.IllegalArgumentException if the resource is not found
  public static URL getResource(Class<?> contextClassString resourceName) {
    URL url = contextClass.getResource(resourceName);
    checkArgument(url != null"resource %s relative to %s not found.",
        resourceNamecontextClass.getName());
    return url;
  }
New to GrepCode? Check out our FAQ X