net.sf.uadetector.internal.util
Class UrlUtil

java.lang.Object
  extended by net.sf.uadetector.internal.util.UrlUtil

public final class UrlUtil
extends java.lang.Object

This class is intended to provide URL utility functions that encapsulate the checked exceptions like MalformedURLException during the construction of an URL or the IOException while opening a stream to an URL.

Author:
André Rouél

Method Summary
static java.net.URL build(java.lang.String url)
          Creates an URL instance from the given String representation.
static java.io.InputStream open(java.net.URL url)
          Tries to open an InputStream to the given URL.
static java.lang.String read(java.net.URL url, java.nio.charset.Charset charset)
          Reads the content of the passed URL as string representation.
static java.net.URL toUrl(java.io.File file)
          Gets the URL to a given File.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

build

public static java.net.URL build(@Nonnull
                                 java.lang.String url)
Creates an URL instance from the given String representation.

This method tunnels a MalformedURLException by an IllegalStateOfArgumentException.

Parameters:
url - String representation of an URL
Returns:
new URL instance
Throws:
net.sf.qualitycheck.exception.IllegalNullArgumentException - if the given argument is null
net.sf.qualitycheck.exception.IllegalStateOfArgumentException - if the string representation of the given URL is invalid and a MalformedURLException occurs

open

public static java.io.InputStream open(@Nonnull
                                       java.net.URL url)
Tries to open an InputStream to the given URL.

Parameters:
url - URL which should be opened
Returns:
opened stream
Throws:
net.sf.qualitycheck.exception.IllegalNullArgumentException - if the given argument is null
CanNotOpenStreamException - if no stream to the given URL can be established

read

public static java.lang.String read(@Nonnull
                                    java.net.URL url,
                                    @Nonnull
                                    java.nio.charset.Charset charset)
                             throws java.io.IOException
Reads the content of the passed URL as string representation.

Parameters:
url - URL to UAS data
charset - the character set in which the data should be read
Returns:
content as String
Throws:
net.sf.qualitycheck.exception.IllegalNullArgumentException - if any of the given arguments is null
CanNotOpenStreamException - if no stream to the given URL can be established
java.io.IOException - if an I/O error occurs

toUrl

public static java.net.URL toUrl(@Nonnull
                                 java.io.File file)
Gets the URL to a given File.

Parameters:
file - file to be converted to a URL
Returns:
an URL to the passed file
Throws:
java.lang.IllegalStateException - if no URL can be resolved to the given file


Copyright © 2011-2014. All Rights Reserved.