【指令碼語言】【RINGO JS】-模組 io
模組 io
該模組提供讀取和寫入原始位元組流的功能。 它根據 CommonJS IO/A 提議實現 Stream 和 TextStream 類。
流與其他兩個模組密切相關。 低階位元組操作由二進位制模組提供,並使用用 ByteArray 或 ByteString 類。 fs 模組返回用於讀取和寫入檔案的 io 流。
Class MemoryStream
Instance Methods
- close()
- closed()
- flush()
- read(maxBytes)
- readInto(buffer, begin, end)
- readable()
- seekable()
- writable()
- write(source, begin, end)
Instance Properties
Class Stream
Instance Methods
- close()
- closed()
- copy(output)
- flush()
- forEach(fn, [thisObj])
- read(maxBytes)
- readInto(buffer, begin, end)
- seekable()
- skip(num)
- unwrap()
- writable()
- write(source, begin, end)
Instance Properties
Class TextStream
Instance Methods
- close()
- copy(output)
- flush()
- forEach(callback, [thisObj])
- iterator()
- next()
- print()
- read()
- readInto()
- readLine()
- readLines()
- readable()
- seekable()
- writable()
- write()
- writeLine(line)
- writeLines(lines)
Instance Properties
MemoryStream (binaryOrNumber)
從記憶體中讀取和/或寫入記憶體中位元組陣列的二進位制流。
如果使用 Number 引數呼叫建構函式,則會分配給定長度的 ByteArray,並將流的長度設定為零。
如果引數是一個 二進位制物件 ,它將被用作底層緩衝區,流長度被設定為二進位制物件的長度。如果引數是 ByteArray,則生成的流可讀,可寫和可搜尋。如果它是 ByteString,則生成的流是可讀且可查詢的,但不可寫。
如果不帶引數呼叫,則將長度為 1024 的 ByteArray 分配為緩衝區。
Parameters
Binary|Number | binaryOrNumber | the buffer to use, or the initial capacity of the buffer to allocate. |
MemoryStream.prototype. close ()
關閉流,釋放它所持有的資源。
MemoryStream.prototype. closed ()
如果流關閉,則返回 true,否則返回 false。
Returns
Boolean | true if the stream has been closed |
MemoryStream.prototype. content
包裝的緩衝區。
MemoryStream.prototype. flush ()
將寫入流的位元組重新整理到底層介質。
MemoryStream.prototype. length
流底層緩衝區中的位元組數。
MemoryStream.prototype. position
此流在包裝緩衝區中的當前位置。
MemoryStream.prototype. read (maxBytes)
從流中讀取最大位元組數,或者直到流的末尾達到。如果未指定 maxBytes,則會讀取完整的流直到達到其結尾。從已經到達結尾的流中讀取將返回一個空的 ByteString。
Parameters
Number | maxBytes | the maximum number of bytes to read |
Returns
ByteString |
See
MemoryStream.prototype. readInto (buffer, begin, end)
從此流讀取位元組到指定的緩衝區中。此方法不會增加緩衝區的長度。
Parameters
ByteArray | buffer | the buffer |
Number | begin | optional begin index, defaults to 0. |
Number | end | optional end index, defaults to buffer.length - 1. |
Returns
Number | The number of bytes read or -1 if the end of the stream has been reached |
See
MemoryStream.prototype. readable ()
如果流支援讀取,則返回 true,否則返回 false。 MemoryStreams 始終返回 true。
Returns
Boolean | true if stream is readable |
See
MemoryStream.prototype. seekable ()
如果流可隨機訪問並支援長度和位置屬性,則返回 true,否則返回 false。 MemoryStreams 始終返回 true。
Returns
Boolean | true if stream is seekable |
See
MemoryStream.prototype. writable ()
如果流支援寫入,則返回 true,否則返回 false。對於 MemoryStreams,如果包裝的二進位制檔案是 ByteArray 的一個例項,則返回 true。
Returns
Boolean | true if stream is writable |
See
MemoryStream.prototype. write (source, begin, end)
從 b 寫入位元組到這個流。如果指定了開始和結束,則只寫入從開始到結束之前的範圍。
Parameters
Binary | source | The source to be written from |
Number | begin | optional |
Number | end | optional |
See
Stream ()
該類實現用於讀取和寫入原始位元組的I / O流。
Stream.prototype. close ()
關閉流,釋放它所持有的資源。
Stream.prototype. closed ()
如果流已關閉則返回 true,否則返回 false。
Returns
Boolean | true if the stream has been closed |
Stream.prototype. copy (output)
讀取此流中可用的所有資料,並將結果寫入給定的輸出流,然後重新整理。請注意,此功能在複製後不會關閉此流或輸出流。
Parameters
Stream | output | The target Stream to be written to. |
Stream.prototype. flush ()
將寫入流的位元組重新整理到底層介質。
Stream.prototype. forEach (fn, [thisObj])
讀取此流中的所有資料併為讀取的每個資料塊呼叫函式 fn。回撥函式是用 ByteArray 作為單個引數呼叫的。請注意,閱讀後流不會關閉。
Parameters
Function | fn | the callback function |
Object | [thisObj] | optional this-object to use for callback |
Stream.prototype. inputStream
包裝的 java.io.InputStream
.
Stream.prototype. outputStream
包裝的 java.io.OutputStream
.
Stream.prototype. read (maxBytes)
從流中讀取最大位元組數,或者直到流的末尾達到。 如果未指定 maxBytes,則會讀取完整的流直到達到其結尾。 從已經到達結尾的流中讀取將返回一個空的 ByteString。
Parameters
Number | maxBytes | the maximum number of bytes to read |
Returns
ByteString |
Stream.prototype. readInto (buffer, begin, end)
從此流讀取位元組到指定的緩衝區中。此方法不會增加緩衝區的長度。
Parameters
ByteArray | buffer | the buffer |
Number | begin | optional begin index, defaults to 0. |
Number | end | optional end index, defaults to buffer.length - 1. |
Returns
Number | The number of bytes read or -1 if the end of the stream has been reached |
Stream.prototype. readable ()
如果流支援讀取,則返回 true,否則返回 false。
Returns
Boolean | true if stream is readable |
Stream.prototype. seekable ()
如果流可隨機訪問並支援長度和位置屬性,則返回 true,否則返回 false。
Returns
Boolean | true if stream is seekable |
Stream.prototype. skip (num)
嘗試跳過資料流中的數字位元組。如果操作無法完成,則返回跳過的acutal位元組數或丟擲錯誤。
Parameters
Number | num | bytes to skip |
Returns
Number | actual bytes skipped |
Stream.prototype. unwrap ()
獲取由此 Stream 封裝的 Java 輸入或輸出流例項。
Stream.prototype. writable ()
如果流支援寫入,則返回 true,否則返回 false。
Returns
Boolean | true if stream is writable |
Stream.prototype. write (source, begin, end)
W 從 b 寫入位元組到這個流。如果指定了開始和結束,則只寫入從開始到結束之前的範圍。
Parameters
Binary | source | The source to be written from |
Number | begin | optional |
Number | end | optional |
TextStream (io, options, buflen)
TextStream 實現用於讀寫字串的 I / O 流。它包裝了一個原始的 Stream 並公開了一個類似的介面。
Parameters
Stream | io | The raw Stream to be wrapped. |
Object | options | the options object. Supports the following properties:
|
Number | buflen | optional buffer size. Defaults to 8192. |
TextStream.prototype. close ()
See
TextStream.prototype. content
如果包裝的流是一個 MemoryStream,則包含將其內容解碼為具有此流編碼的字串。否則包含一個空字串。
TextStream.prototype. copy (output)
使用 readLine從此流讀取,將結果寫入目標流並重新整理,直到達到此流的末尾。
Parameters
Stream | output |
Returns
TextStream | this stream |
TextStream.prototype. flush ()
See
TextStream.prototype. forEach (callback, [thisObj])
使用輸入流中的每一行呼叫回撥。
Example
var txtStream = fs.open('./browserStats.csv', 'r');
txtStream.forEach(function(line) {
console.log(line); // Print one single line
});
Parameters
Function | callback | the callback function |
Object | [thisObj] | optional this-object to use for callback |
TextStream.prototype. iterator ()
返回此流。
Returns
TextStream | this stream |
TextStream.prototype. next ()
返回沒有換行符的下一行輸入。如果到達流的末尾,則會引發 StopIteration。
Example
var fs = require('fs');
var txtStream = fs.open('./browserStats.csv', 'r');
try {
while (true) {
console.log(txtStream.next());
}
} catch (e) {
console.log("EOF");
}
Returns
String | the next line |
TextStream.prototype. print ()
將所有引數值寫入一行,使用單個空白分隔值。
Example
>> var fs = require('fs');
>> var txtOutStream = fs.open('./demo.txt', 'w');
>> txtOutStream.print('foo', 'bar', 'baz');
// demo.txt content:
foo bar baz
Returns
TextStream | this stream |
TextStream.prototype. raw
包裝的二進位制流。
TextStream.prototype. read ()
閱讀完整的流,直到達到結尾並返回以字串形式讀取的資料。
Returns
String |
TextStream.prototype. readInto ()
沒有為 TextStream 實現。呼叫此方法將引發錯誤。
TextStream.prototype. readLine ()
從此流讀取一行。如果在收集任何資料之前到達流的末尾,則返回一個空字串。否則,返回僅包含換行符的行。回車將被丟棄。
Returns
String | the next line |
TextStream.prototype. readLines ()
返回一個字串陣列,通過呼叫 readLine 直到它返回一個空字串。返回的陣列不包含最終的空字串,但它在每行的末尾都包含尾隨的換行符。
Example
>> var fs = require('fs');
>> var txtStream = fs.open('./sampleData.csv', 'r');
>> var lines = txtStream.readLines();
>> console.log(lines.length + ' lines');
6628 lines
Returns
Array | an array of lines |
TextStream.prototype. readable ()
See
TextStream.prototype. seekable ()
始終返回 false,因為 TextStream 不可隨機訪問。
TextStream.prototype. writable ()
See
TextStream.prototype. write ()
將所有引數寫入流。
Example
>> var fs = require('fs');
>> var txtOutStream = fs.open('./demo.txt', 'w');
>> txtOutStream.write('foo', 'bar', 'baz');
// demo.txt content:
foobarbaz
Returns
TextStream | this stream |
TextStream.prototype. writeLine (line)
將給定的行寫入流中,然後寫入換行符。
Parameters
String | line |
Returns
TextStream | this stream |
TextStream.prototype. writeLines (lines)
將給定的行寫入流中,並用換行符終止每行。這是非標準擴充套件,不屬於 CommonJS IO/A。
Parameters
Array | lines |
Returns
TextStream | this stream |