- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstringToStream.js
More file actions
Latest commit
42 lines (39 loc) · 1.36 KB
/
Copy pathstringToStream.js
File metadata and controls
42 lines (39 loc) · 1.36 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
41
42
/*
** Convert a string to a COM stream object (IStream),
** as UTF-8 or using the specified charset.
**
** See HKEY_CLASSES_ROOT\MIME\Database\Charset for a list of supported charsets on your system.
**
** Dependencies:
** - Microsoft ActiveX Data Objects ("ADODB.Stream")
*/
functionstringToBinary(text/*, charset*/){
vartxtstm=newActiveXObject("ADODB.Stream");
txtstm.open();
txtstm.type=2/*adTypeText*/;
varcharset=arguments[1]||"utf-8";
try{
txtstm.charset=charset;
}catch(ex){
if(ex.number===-2146825287){// Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
// charset requested isn't recognized
vare=newTypeError("Unknown charset requested: "+charset);
e.description=e.message;
throwe;
}else{
throwex;
}
}
varcharset=txtstm.charset;// retrieve standardized charset name
txtstm.writeText(String(text));
// switch stream to binary mode and return its contents
txtstm.position=0;
txtstm.type=1/*adTypeBinary*/;
if(charset.toLowerCase()==="utf-8"){
txtstm.position=3;// skip UTF-8 BOM
}elseif(charset==="Unicode"){
// This also handles "UTF-16" and "utf-16", they automatically get changed to "Unicode".
txtstm.position=2;// skip UTF-16 BOM
}// No BOM is added for UTF-16BE
returntxtstm;
}