Skip to content

URI

Purpose

Provides methods to manipulate URIs and the streams behind them.

Methods

Binding name: uri

  • File fileFromUrl( String url ): Returns a File from a String URL after attempting to normalize the URL to an URI. Returns null if the protocol is not the file protocol. On Windows, URLs with protocol file: work ‘better’ than file://
  • Map streamFrom( String url ): Opens an InputStream from an URI This method handles BOM headers correctly. The user must close the InputStream when done. Returns a Map made of inputStream: the InputStream, contentType: the content type inferred from the URI (String)
  • Map streamTo( InputStream is, String url = null, String contentType = ‘application/octet-stream”): Streams the content of an opened InputStream to an URI If the uri is Null, a temporary file is create. The inputStream will be closed when completed (successfully or in error) The URI protocols supported are: file, log, console, jcr, http contentType makes sense for HTTP only (and is not mandatory, will default to application/octet-stream for HTTP) Return a Map made of uri: the URI actually written to (String), totalBytes: the number of bytes written (Long)
  • Map copy(String sourceUri, String targetUri ): Copies the content from sourceUri to targetUri If the targetUri is Null, a temporary file is created The URI protocols supported for targetUri are: file, log, console, jcr, http contentType makes sense for HTTP only (and is not mandatory, will default to application/octet-stream for HTTP) Return a Map made of uri: the URI actually written to (String) totalBytes: the number of bytes written (Long) contentType: the content type inferred from the sourceUri (String)
  • String charsetFromContentType( String contentType ): Extracts the Charset from the content type and return it in Upper Case Will return UTF-8 if not found Example: text/plain;charset=iso-8859-1 => ISO-8859-1
  • String mimeTypeFromContentType( String contentType ): Extracts the Mime Type from the content type and return it in Lower Case Example: text/plain;charset=iso-8859-1 => text/plain

Examples

def f = uri.fileFromUrl("file:/opt/b2box5.data/conf/log4j.properties")
println f.getAbsolutePath()

def m = uri.streamFrom("file:/opt/b2box5.data/conf/log4j.properties")
println m