Skip to content

URI

Purpose

Provides methods to manipulate URIs and the streams behind them.

Methods

Binding name: p6.uri


fileFromUrl

Returns a File from a String URL after attempting to normalize the URL to an URI.

Syntax

File p6.uri.fileFromUrl( String url )

Info

Returns null if the protocol is not the file protocol.

Tip

On Windows, URLs with protocol file: work ‘better’ than file://

Example
def f = p6.uri.fileFromUrl("file:/opt/p6core.data/conf/log4j.properties")

streamFrom

Opens an InputStream from an URI This method handles BOM headers correctly

Syntax

Map p6.uri.streamFrom( String url )

Returns a Map made of:

  • inputStream: the InputStream,
  • contentType: the content type inferred from the URI (String)

Warning

The user must close the InputStream when done.

Example
def item = p6.uri.streamFrom("p6file://${P6_DATA}/resources/data.csv")

streamTo

Streams the content of an opened InputStream to an URI

Syntax

Map p6.uri.streamTo( InputStream is, String uri, String contentType)

Return a Map made of:

  • uri: the URI actually written to (String), totalBytes: the number of bytes written (Long)

Info

The URI protocols supported are: file, log, console, http contentType makes sense for HTTP only (and is not mandatory, will default to application/octet-stream for HTTP)

Tip

To use a temporary file, set null to the parameter uri

Warning

The inputStream will be closed when completed (successfully or in error)

Examples

Stream to specific file

def inputstream = // retrieve input stream
def item = p6.uri.streamTo(inputstream, "p6file://${P6_DATA}/resources/data.csv", "application/octet-stream")

Stream to temporary file

def inputstream = // retrieve input stream
def item = p6.uri.streamTo(inputstream, null, "application/octet-stream")


copy

Copies the content from sourceUri to targetUri

Syntax

Map p6.uri.copy(String sourceUri, String targetUri )

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)

Info

The URI protocols supported are: file, log, console, http contentType makes sense for HTTP only (and is not mandatory, will default to application/octet-stream for HTTP)

Tip

To use a temporary file, set null to the parameter targetUri

Example

Copy to specific file

def source = "p6file://${P6_DATA}/resources/data.csv"
def item = p6.uri.copy(inputstream, "p6file://${P6_DATA}/resources/data_copy.csv")

Copy to temporary file

def source = "p6file://${P6_DATA}/resources/data.csv"
def item = p6.uri.copy(inputstream, null)


charsetFromContentType

Extracts the Charset from the content type

Syntax

String p6.uri.charsetFromContentType( String contentType )

Info

The charset will be returned in Upper Case. If not found will return UTF-8.

Example
println p6.uri.charsetFromContentType("text/plain;charset=iso-8859-1")
// Output: ISO-8859-1

mimeTypeFromContentType

Extracts the Mime Type from the content type

Syntax

String p6.uri.mimeTypeFromContentType( String contentType )

Info

The mime type will be returned in Lower Case.

Example
println p6.uri.mimeTypeFromContentType("text/plain;charset=iso-8859-1")
// Output: text/plain