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
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
 
       final URL url) {
     checkNotNull(url);
     return new InputSupplier<InputStream>() {
       @Override
       public InputStream getInput() throws IOException {
         return url.openStream();
       }
     };
   }

  
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.newReaderSupplier(newInputStreamSupplier(url), 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 ByteStreams.toByteArray(newInputStreamSupplier(url));
   }

  
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 CharStreams.toString(newReaderSupplier(urlcharset));
  }

  
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.

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 {
    return CharStreams.readLines(newReaderSupplier(urlcharset));
  }

  
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 {
    ByteStreams.copy(newInputStreamSupplier(from), to);
  }
  
  
Returns a URL pointing to resourceName if the resource is found in the class path. Resources.class.getClassLoader() is used to locate the resource.

Throws:
java.lang.IllegalArgumentException if resource is not found
  public static URL getResource(String resourceName) {
    URL url = Resources.class.getClassLoader().getResource(resourceName);
    checkArgument(url != null"resource %s not found."resourceName);
    return url;
  }

  
Returns a URL pointing to resourceName that is relative to contextClass, if the resource is found in the class path.

Throws:
java.lang.IllegalArgumentException if 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