-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathFileStream.js
More file actions
40 lines (35 loc) · 1.14 KB
/
FileStream.js
File metadata and controls
40 lines (35 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* Copyright (c) 2012-2022 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
import InputStream from './InputStream.js';
import CharStream from './CharStream.js';
let isNode =
typeof process !== "undefined" &&
process.versions != null &&
process.versions.node != null;
import fs from 'fs';
/**
* This is an InputStream that is loaded from a file all at once
* when you construct the object.
*/
export default class FileStream extends InputStream {
static fromPath(path, encoding, callback) {
if(!isNode)
throw new Error("FileStream is only available when running in Node!");
fs.readFile(path, encoding, function(err, data) {
let is = null;
if (data !== null) {
is = new CharStream(data, true);
}
callback(err, is);
});
}
constructor(fileName, encoding, decodeToUnicodeCodePoints) {
if(!isNode)
throw new Error("FileStream is only available when running in Node!");
let data = fs.readFileSync(fileName, encoding || "utf-8" );
super(data, decodeToUnicodeCodePoints);
this.fileName = fileName;
}
}