IO related functions.
Example
var ioLib = require('/lib/xp/io');
Classes
Methods
(static) getMimeType(name) → {string}
Returns the mime-type from a name or extension.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | Name of file or extension. |
Returns:
Mime-type of name or extension.
- Type
- string
Example
// Returns mime-type for a file name.
var type = ioLib.getMimeType('myfile.txt');
log.info('Mime type is %s', type);
(static) getResource(key) → {Resource}
Looks up a resource.
Parameters:
Name | Type | Description |
---|---|---|
key |
string | Resource key to look up. |
Returns:
Resource reference.
- Type
- Resource
Examples
// Returns a file by name.
var res1 = ioLib.getResource('/site/lib/xp/examples/io/sample.txt');
var exists = res1.exists();
var size = res1.getSize();
var stream = res1.getStream();
// Returns a file by reference.
var res2 = ioLib.getResource(resolve('./sample.txt'));
if (res2.exists()) {
log.info('Resource exists');
}
(static) getSize(stream) → {number}
Returns the size of a stream.
Parameters:
Name | Type | Description |
---|---|---|
stream |
Stream to get size of. |
Returns:
Returns the size of a stream.
- Type
- number
Example
// Returns the size of a stream.
var size = ioLib.getSize(stream);
log.info('Stream size is %s bytes', size);
(static) newStream(text) → {*}
Returns a new stream from a string.
Parameters:
Name | Type | Description |
---|---|---|
text |
string | String to create a stream of. |
Returns:
A new stream.
- Type
- *
Example
// Creates a new stream from a string.
var stream = ioLib.newStream('Hello World');
(static) processLines(stream, func)
Process lines from a stream.
Parameters:
Name | Type | Description |
---|---|---|
stream |
Stream to read lines from. |
|
func |
function | Callback function to be called for each line. |
Example
var num = 0;
// Process lines from stream.
ioLib.processLines(stream, function (line) {
num++;
log.info('Line %s: %s', num, line);
});
(static) readLines(stream) → {Array.<string>}
Read lines from a stream.
Parameters:
Name | Type | Description |
---|---|---|
stream |
Stream to read lines from. |
Returns:
Returns lines as an array.
- Type
- Array.<string>
Example
// Reads lines from stream.
var lines = ioLib.readLines(stream);
log.info('Num lines: %s', lines.length);
(static) readText(stream) → {string}
Read text from a stream.
Parameters:
Name | Type | Description |
---|---|---|
stream |
Stream to read text from. |
Returns:
Returns the text read from stream or string.
- Type
- string
Example
// Reads text from stream.
var text = ioLib.readText(stream);
log.info('Text: %s', text);